Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
refactor: stores JSON Schema Draft versions as "draftV{x}"
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Nov 28, 2019
1 parent 57f1b05 commit e82a1e2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/validators/json-schema-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 8 additions & 10 deletions lib/validators/json-schema-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion lib/validators/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions test/unit/validators/json-schema-legacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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.
};

Expand Down
6 changes: 3 additions & 3 deletions test/unit/validators/json-schema-next.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit e82a1e2

Please sign in to comment.