Skip to content

Commit

Permalink
feat: infers JSON Schema Draft 7 when schema is boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Dec 4, 2019
1 parent 9bccf12 commit 593ea41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
23 changes: 17 additions & 6 deletions lib/validators/json-schema-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,26 @@ 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];
return currentVersion && currentVersion.includes(jsonSchemaAnnotation);
});
};

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]
Expand All @@ -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('/');
Expand Down Expand Up @@ -168,6 +180,5 @@ class JsonSchemaValidator {
module.exports = {
JsonSchemaValidator,
getSchemaVersion,
getImplicitSchemaVersion,
META_SCHEMA
};
10 changes: 2 additions & 8 deletions lib/validators/json-schema.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/valid-schema-v7-boolean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
16 changes: 16 additions & 0 deletions test/unit/validators/json-schema-next.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
/**
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 593ea41

Please sign in to comment.