From 593ea41dc86af6b935f7e45645a8bf13f728944f Mon Sep 17 00:00:00 2001 From: artem-zakharchenko Date: Wed, 4 Dec 2019 13:45:51 +0100 Subject: [PATCH] feat: infers JSON Schema Draft 7 when schema is boolean --- lib/validators/json-schema-next.js | 23 ++++++++++++++----- lib/validators/json-schema.js | 10 ++------ test/fixtures/valid-schema-v7-boolean.json | 1 + test/unit/validators/json-schema-next.test.js | 16 +++++++++++++ 4 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 test/fixtures/valid-schema-v7-boolean.json diff --git a/lib/validators/json-schema-next.js b/lib/validators/json-schema-next.js index 6f350599..908febca 100644 --- a/lib/validators/json-schema-next.js +++ b/lib/validators/json-schema-next.js @@ -26,7 +26,7 @@ const META_SCHEMA = { /** * Returns a JSON Schema Draft version of the given JSON Schema. */ -const getSchemaVersion = (jsonSchema) => { +const getExplicitSchemaVersion = (jsonSchema) => { const currentVersion = jsonSchema.$schema && jsonSchema.$schema; return Object.keys(SCHEMA_VERSIONS).find((version) => { const jsonSchemaAnnotation = SCHEMA_VERSIONS[version]; @@ -34,12 +34,18 @@ const getSchemaVersion = (jsonSchema) => { }); }; +const getImplicitSchemaVersion = (jsonSchema) => { + if (typeof jsonSchema === 'boolean') { + return 'draftV7'; + } +}; + /** * @deprecate * Attempts to resolve a schema version for a JSON Schema * without the explicit version. */ -const getImplicitSchemaVersion = (jsonSchema) => { +const getImplicitLegacySchemaVersion = (jsonSchema) => { const [schemaVersion] = [ ['draftV3', metaSchemaV3], ['draftV4', metaSchemaV4] @@ -54,12 +60,18 @@ const getImplicitSchemaVersion = (jsonSchema) => { return schemaVersion; }; +const getSchemaVersion = (jsonSchema) => { + return ( + getExplicitSchemaVersion(jsonSchema) || + getImplicitSchemaVersion(jsonSchema) || + getImplicitLegacySchemaVersion(jsonSchema) + ); +}; + class JsonSchemaValidator { constructor(jsonSchema) { this.jsonSchema = jsonSchema; - this.jsonSchemaVersion = - getSchemaVersion(this.jsonSchema) || - getImplicitSchemaVersion(this.jsonSchema); + this.jsonSchemaVersion = getSchemaVersion(jsonSchema); if (this.jsonSchemaVersion == null) { const supportedVersions = Object.keys(SCHEMA_VERSIONS).join('/'); @@ -168,6 +180,5 @@ class JsonSchemaValidator { module.exports = { JsonSchemaValidator, getSchemaVersion, - getImplicitSchemaVersion, META_SCHEMA }; diff --git a/lib/validators/json-schema.js b/lib/validators/json-schema.js index 04b26187..178f9622 100644 --- a/lib/validators/json-schema.js +++ b/lib/validators/json-schema.js @@ -1,9 +1,5 @@ const { JsonSchemaLegacy } = require('./json-schema-legacy'); -const { - JsonSchemaValidator, - getSchemaVersion, - getImplicitSchemaVersion -} = require('./json-schema-next'); +const { JsonSchemaValidator, getSchemaVersion } = require('./json-schema-next'); const errors = require('../errors'); const parseJson = require('../utils/parseJson'); @@ -32,9 +28,7 @@ function resolveJsonSchema(jsonSchema) { class JsonSchema { constructor(jsonSchema) { const resolvedJsonSchema = resolveJsonSchema(jsonSchema); - const jsonSchemaVersion = - getSchemaVersion(resolvedJsonSchema) || - getImplicitSchemaVersion(resolvedJsonSchema); + const jsonSchemaVersion = getSchemaVersion(resolvedJsonSchema); const isLegacySchema = ['draftV3', 'draftV4'].includes(jsonSchemaVersion); // Instantiate different JSON Schema validators diff --git a/test/fixtures/valid-schema-v7-boolean.json b/test/fixtures/valid-schema-v7-boolean.json new file mode 100644 index 00000000..27ba77dd --- /dev/null +++ b/test/fixtures/valid-schema-v7-boolean.json @@ -0,0 +1 @@ +true diff --git a/test/unit/validators/json-schema-next.test.js b/test/unit/validators/json-schema-next.test.js index f7219d01..5521f07e 100644 --- a/test/unit/validators/json-schema-next.test.js +++ b/test/unit/validators/json-schema-next.test.js @@ -8,6 +8,7 @@ const invalidJsonSchema6 = require('../../fixtures/invalid-schema-v6'); const validJsonSchema6 = require('../../fixtures/valid-schema-v6'); const invalidJsonSchema7 = require('../../fixtures/invalid-schema-v7'); const validJsonSchema7 = require('../../fixtures/valid-schema-v7'); +const validJsonSchema7Boolean = require('../../fixtures/valid-schema-v7-boolean'); describe('JSON Schema (next)', () => { /** @@ -206,4 +207,19 @@ describe('JSON Schema (next)', () => { }); }); }); + + /** + * Inferred JSON Schema version for boolean value schema. + */ + describe('given a JSON Schema with a boolean value', () => { + let validator; + + before(() => { + validator = new JsonSchemaValidator(validJsonSchema7Boolean); + }); + + it('should infer schema version as "draft7"', () => { + expect(validator).to.have.property('jsonSchemaVersion', 'draftV7'); + }); + }); });