From e82a1e2e0f6f4886adeab69e1af750dea473d1b8 Mon Sep 17 00:00:00 2001 From: artem-zakharchenko Date: Wed, 27 Nov 2019 11:28:36 +0100 Subject: [PATCH] refactor: stores JSON Schema Draft versions as "draftV{x}" --- lib/validators/json-schema-legacy.js | 2 +- lib/validators/json-schema-next.js | 18 ++++++++---------- lib/validators/json-schema.js | 2 +- .../unit/validators/json-schema-legacy.test.js | 6 +++--- test/unit/validators/json-schema-next.test.js | 6 +++--- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/validators/json-schema-legacy.js b/lib/validators/json-schema-legacy.js index 51c36d2e..3d543130 100644 --- a/lib/validators/json-schema-legacy.js +++ b/lib/validators/json-schema-legacy.js @@ -57,7 +57,7 @@ const jsonSchemaOptions = { class JsonSchemaLegacy extends JsonSchemaValidator { validateSchema() { const { jsonSchema, jsonMetaSchema } = this; - const metaSchema = jsonMetaSchema || META_SCHEMA.v3; + const metaSchema = jsonMetaSchema || META_SCHEMA.draftV3; tv4.reset(); tv4.addSchema('', metaSchema); diff --git a/lib/validators/json-schema-next.js b/lib/validators/json-schema-next.js index 5ade9bbe..82ddbbcd 100644 --- a/lib/validators/json-schema-next.js +++ b/lib/validators/json-schema-next.js @@ -7,17 +7,17 @@ const metaSchemaV3 = require('../meta-schema-v3'); const errors = require('../errors'); const SCHEMA_VERSIONS = { - v3: 'http://json-schema.org/draft-03/schema', - v4: 'http://json-schema.org/draft-04/schema', - v6: 'http://json-schema.org/draft-06/schema', - v7: 'http://json-schema.org/draft-07/schema' + draftV3: 'http://json-schema.org/draft-03/schema', + draftV4: 'http://json-schema.org/draft-04/schema', + draftV6: 'http://json-schema.org/draft-06/schema', + draftV7: 'http://json-schema.org/draft-07/schema' }; const META_SCHEMA = { - v3: metaSchemaV3, - v4: metaSchemaV4, - v6: metaSchemaV6, - v7: metaSchemaV7 + draftV3: metaSchemaV3, + draftV4: metaSchemaV4, + draftV6: metaSchemaV6, + draftV7: metaSchemaV7 }; /** @@ -70,8 +70,6 @@ class JsonSchemaValidator { validateSchema() { const { jsonSchemaVersion, jsonSchema } = this; let isSchemaValid = true; - - // use AJV to validate modern schema (6/7) const ajv = new Ajv(); const metaSchema = META_SCHEMA[jsonSchemaVersion]; diff --git a/lib/validators/json-schema.js b/lib/validators/json-schema.js index bd6537b1..0395dcda 100644 --- a/lib/validators/json-schema.js +++ b/lib/validators/json-schema.js @@ -4,7 +4,7 @@ const { JsonSchemaValidator, getSchemaVersion } = require('./json-schema-next'); class JsonSchema { constructor(jsonSchema) { const jsonSchemaVersion = getSchemaVersion(jsonSchema); - const isLegacy = ['v3', 'v4'].includes(jsonSchemaVersion); + const isLegacy = ['draftV3', 'draftV4'].includes(jsonSchemaVersion); // Instantiate different JSON Schema validators // based on the JSON Schema Draft version. diff --git a/test/unit/validators/json-schema-legacy.test.js b/test/unit/validators/json-schema-legacy.test.js index 7fc92fc4..1ee9cf84 100644 --- a/test/unit/validators/json-schema-legacy.test.js +++ b/test/unit/validators/json-schema-legacy.test.js @@ -41,7 +41,7 @@ describe('JSON Schema (legacy)', () => { }); it('should recognize schema version as v3', () => { - expect(validator).to.have.property('jsonSchemaVersion', 'v3'); + expect(validator).to.have.property('jsonSchemaVersion', 'draftV3'); }); describe('given validating data', () => { @@ -128,14 +128,14 @@ describe('JSON Schema (legacy)', () => { }); it('should recognize schema version as v4', () => { - expect(validator).to.have.property('jsonSchemaVersion', 'v4'); + expect(validator).to.have.property('jsonSchemaVersion', 'draftV4'); }); describe('given validating data', () => { describe('and the data is invalid', () => { let errors; const data = { - foo: 'wrong value type', + foo: 'should be number', b: 'z' // DOES NOT RESPECT ENUM HERE. THINKS THIS IS VALID. }; diff --git a/test/unit/validators/json-schema-next.test.js b/test/unit/validators/json-schema-next.test.js index dd3fcbb1..20214dc1 100644 --- a/test/unit/validators/json-schema-next.test.js +++ b/test/unit/validators/json-schema-next.test.js @@ -59,7 +59,7 @@ describe('JSON Schema (next)', () => { }); it('should recognize schema version as v6', () => { - expect(validator).to.have.property('jsonSchemaVersion', 'v6'); + expect(validator).to.have.property('jsonSchemaVersion', 'draftV6'); }); describe('given validating data', () => { @@ -81,7 +81,7 @@ describe('JSON Schema (next)', () => { describe('and the data is invalid', () => { let errors; const data = { - foo: 'invalid-value' + foo: 'should be number' }; before(() => { @@ -147,7 +147,7 @@ describe('JSON Schema (next)', () => { }); it('should recognize schema version as v7', () => { - expect(validator).to.have.property('jsonSchemaVersion', 'v7'); + expect(validator).to.have.property('jsonSchemaVersion', 'draftV7'); }); describe('given validating data', () => {