Skip to content

Commit

Permalink
fix: v5 compatibility mode when draft-06 meta schema is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jan 22, 2017
1 parent a6ea946 commit 1567814
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
15 changes: 15 additions & 0 deletions keywords/_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
metaSchemaRef: metaSchemaRef
};

var META_SCHEMA_ID = 'http://json-schema.org/draft-06/schema';

function metaSchemaRef(ajv) {
var defaultMeta = ajv._opts.defaultMeta;
if (typeof defaultMeta == 'string') return { $ref: defaultMeta };
if (ajv.getSchema(META_SCHEMA_ID)) return { $ref: META_SCHEMA_ID };
console.warn('meta schema not defined');
return {};
}
6 changes: 3 additions & 3 deletions keywords/deepProperties.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var util = require('./_util');

module.exports = function defFunc(ajv) {
defFunc.definition = {
type: 'object',
Expand All @@ -12,9 +14,7 @@ module.exports = function defFunc(ajv) {
metaSchema: {
type: 'object',
patternProperties: {
'^(\\/([^~\\/]|~0|~1)*)*(\\/)?$': {
$ref: 'http://json-schema.org/draft-06/schema#'
}
'^(\\/([^~\\/]|~0|~1)*)*(\\/)?$': util.metaSchemaRef(ajv)
},
additionalProperties: false
}
Expand Down
8 changes: 5 additions & 3 deletions keywords/switch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var util = require('./_util');

module.exports = function defFunc(ajv) {
if (ajv.RULES.keywords.switch && ajv.RULES.keywords.if) return;

var metaSchemaUri = 'http://json-schema.org/draft-06/schema#';
var metaSchemaRef = util.metaSchemaRef(ajv);

defFunc.definition = {
inline: require('./dotjs/switch'),
Expand All @@ -14,11 +16,11 @@ module.exports = function defFunc(ajv) {
items: {
required: [ 'then' ],
properties: {
'if': { $ref: metaSchemaUri },
'if': metaSchemaRef,
'then': {
anyOf: [
{ type: 'boolean' },
{ $ref: metaSchemaUri }
metaSchemaRef
]
},
'continue': { type: 'boolean' }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv-keywords",
"version": "2.0.0-beta.0",
"version": "2.0.0-beta.1",
"description": "Custom JSON-Schema keywords for Ajv validator",
"main": "index.js",
"scripts": {
Expand Down
32 changes: 31 additions & 1 deletion spec/schema-tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ var ajvs = [
['switch', 'patternRequired', 'formatMinimum', 'formatMaximum',
'if', 'prohibited', 'deepRequired', 'deepProperties']),
defineKeywords(getAjv()),
defineKeywords(getAjv(true))
defineKeywords(getAjv(true)),
defineKeywords(getAjvNoMeta()),
defineKeywords(getAjvV5())
];


Expand All @@ -33,3 +35,31 @@ function getAjv(extras) {
unknownFormats: ['allowedUnknown']
});
}


function getAjvNoMeta() {
return new Ajv({
$data: true,
unknownFormats: ['allowedUnknown'],
meta: false,
validateSchema: false
});
}


function getAjvV5() {
var ajv = new Ajv({
$data: true,
meta: false,
unknownFormats: 'ignore'
});

ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

var metaSchema = require('ajv/lib/refs/json-schema-v5.json');
ajv.addMetaSchema(metaSchema);
ajv._opts.defaultMeta = metaSchema.id;

ajv.removeKeyword('propertyNames');
return ajv;
}

0 comments on commit 1567814

Please sign in to comment.