From ab841b462ec4baff37d2a7319cef13820b53d963 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 27 Apr 2019 10:32:39 +0100 Subject: [PATCH] fix: addKeyword and schema without ID, closes #1001 --- lib/definition_schema.js | 37 +++++++++++++++++++ lib/keyword.js | 34 +---------------- spec/JSON-Schema-Test-Suite | 2 +- ...1_addKeyword_and_schema_without_id.spec.js | 20 ++++++++++ 4 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 lib/definition_schema.js create mode 100644 spec/issues/1001_addKeyword_and_schema_without_id.spec.js diff --git a/lib/definition_schema.js b/lib/definition_schema.js new file mode 100644 index 000000000..e5f6b911c --- /dev/null +++ b/lib/definition_schema.js @@ -0,0 +1,37 @@ +'use strict'; + +var metaSchema = require('./refs/json-schema-draft-07.json'); + +module.exports = { + $id: 'https://github.com/epoberezkin/ajv/blob/master/lib/definition_schema.js', + definitions: { + simpleTypes: metaSchema.definitions.simpleTypes + }, + type: 'object', + dependencies: { + schema: ['validate'], + $data: ['validate'], + statements: ['inline'], + valid: {not: {required: ['macro']}} + }, + properties: { + type: metaSchema.properties.type, + schema: {type: 'boolean'}, + statements: {type: 'boolean'}, + dependencies: { + type: 'array', + items: {type: 'string'} + }, + metaSchema: {type: 'object'}, + modifying: {type: 'boolean'}, + valid: {type: 'boolean'}, + $data: {type: 'boolean'}, + async: {type: 'boolean'}, + errors: { + anyOf: [ + {type: 'boolean'}, + {const: 'full'} + ] + } + } +}; diff --git a/lib/keyword.js b/lib/keyword.js index cf4d699b3..5fec19a67 100644 --- a/lib/keyword.js +++ b/lib/keyword.js @@ -2,7 +2,7 @@ var IDENTIFIER = /^[a-z_$][a-z0-9_$-]*$/i; var customRuleCode = require('./dotjs/custom'); -var metaSchema = require('./refs/json-schema-draft-07.json'); +var definitionSchema = require('./definition_schema'); module.exports = { add: addKeyword, @@ -11,38 +11,6 @@ module.exports = { validate: validateKeyword }; -var definitionSchema = { - definitions: { - simpleTypes: metaSchema.definitions.simpleTypes - }, - type: 'object', - dependencies: { - schema: ['validate'], - $data: ['validate'], - statements: ['inline'], - valid: {not: {required: ['macro']}} - }, - properties: { - type: metaSchema.properties.type, - schema: {type: 'boolean'}, - statements: {type: 'boolean'}, - dependencies: { - type: 'array', - items: {type: 'string'} - }, - metaSchema: {type: 'object'}, - modifying: {type: 'boolean'}, - valid: {type: 'boolean'}, - $data: {type: 'boolean'}, - async: {type: 'boolean'}, - errors: { - anyOf: [ - {type: 'boolean'}, - {const: 'full'} - ] - } - } -}; /** * Define custom keyword diff --git a/spec/JSON-Schema-Test-Suite b/spec/JSON-Schema-Test-Suite index 15ba997f9..eadeacb04 160000 --- a/spec/JSON-Schema-Test-Suite +++ b/spec/JSON-Schema-Test-Suite @@ -1 +1 @@ -Subproject commit 15ba997f9b937150a0ab88244d1d0fbf58526c48 +Subproject commit eadeacb04209a18fc81f1a1959e83eef72dcc97a diff --git a/spec/issues/1001_addKeyword_and_schema_without_id.spec.js b/spec/issues/1001_addKeyword_and_schema_without_id.spec.js new file mode 100644 index 000000000..bc3d0d7d0 --- /dev/null +++ b/spec/issues/1001_addKeyword_and_schema_without_id.spec.js @@ -0,0 +1,20 @@ +'use strict'; + +var Ajv = require('../ajv'); +require('../chai').should(); + + +describe('issue #1001: addKeyword breaks schema without ID', function() { + it('should allow using schemas without ID with addKeyword', function() { + var schema = { + definitions: { + foo: {} + } + }; + + var ajv = new Ajv(); + ajv.addSchema(schema); + ajv.addKeyword('myKeyword', {}); + ajv.getSchema('#/definitions/foo') .should.be.a('function'); + }); +});