refactor: patternGroups deprecated, requires option to use, #371

master
Evgeny Poberezkin 2016-12-20 20:33:51 +00:00
parent d19bc92c8b
commit afd02f1f72
11 changed files with 54 additions and 36 deletions

View File

@ -30,7 +30,7 @@ The keywords and their values define what rules the data should satisfy to be va
- [patternProperties](#patternproperties)
- [additionalProperties](#additionalproperties)
- [dependencies](#dependencies)
- [patternGroups](#patterngroups-v5-proposal) (v5)
- [patternGroups](#patterngroups-deprecated) (deprecated)
- [patternRequired](#patternrequired-v5-proposal) (v5)
- [Keywords for all types](#keywords-for-all-types)
- [enum](#enum)
@ -579,7 +579,9 @@ __Examples__
### `patternGroups` (v5 proposal)
### `patternGroups` (deprecated)
This keyword is only provided for backward compatibility, it will be removed in the next major version. To use it, pass option `patternGroups: true`.
The value of this keyword should be a map where keys should be regular expressions and the values should be objects with the following properties:

View File

@ -8,6 +8,7 @@ var compileSchema = require('./compile')
, formats = require('./compile/formats')
, rules = require('./compile/rules')
, v5 = require('./v5')
, patternGroups = require('./patternGroups')
, util = require('./compile/util')
, async = require('./async')
, co = require('co');
@ -72,6 +73,7 @@ function Ajv(opts) {
if (opts.formats) addInitialFormats(this);
addDraft4MetaSchema(this);
if (opts.v5) v5.enable(this);
if (opts.patternGroups) patternGroups(this);
if (typeof opts.meta == 'object') this.addMetaSchema(opts.meta);
addInitialSchemas(this);
}

View File

@ -295,8 +295,12 @@ function compile(schema, root, localRefs, baseId) {
validate = inline.call(self, it, rule.keyword, schema, parentSchema);
} else {
validate = rule.definition.validate;
if (!validate) return;
}
if (validate === undefined)
throw new Error('custom keyword "' + rule.keyword + '"failed to compile');
var index = customRules.length;
customRules[index] = validate;

View File

@ -25,6 +25,7 @@
{{??}}
{{
$ruleValidate = it.useCustomRule($rule, $schema, it.schema, it);
if (!$ruleValidate) return;
$schemaValue = 'validate.schema' + $schemaPath;
$validateCode = $ruleValidate.code;
$compile = $rDef.compile;

View File

@ -43,7 +43,7 @@
if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired)
var $requiredHash = it.util.toHash($required);
if (it.opts.v5) {
if (it.opts.patternGroups) {
var $pgProperties = it.schema.patternGroups || {}
, $pgPropertyKeys = Object.keys($pgProperties);
}
@ -72,7 +72,7 @@ var valid{{=$it.level}} = true;
|| {{= it.usePattern($pProperty) }}.test({{=$key}})
{{~}}
{{?}}
{{? it.opts.v5 && $pgPropertyKeys && $pgPropertyKeys.length }}
{{? it.opts.patternGroups && $pgPropertyKeys.length }}
{{~ $pgPropertyKeys:$pgProperty:$i }}
|| {{= it.usePattern($pgProperty) }}.test({{=$key}})
{{~}}
@ -239,7 +239,7 @@ var valid{{=$it.level}} = true;
{{?}}
{{? it.opts.v5 && $pgPropertyKeys.length }}
{{? it.opts.patternGroups && $pgPropertyKeys.length }}
{{~ $pgPropertyKeys:$pgProperty }}
{{
var $pgSchema = $pgProperties[$pgProperty]

View File

@ -127,9 +127,12 @@
{{?}}
{{~ $rulesGroup.rules:$rule }}
{{? $shouldUseRule($rule) }}
{{= $rule.code(it, $rule.keyword) }}
{{? $breakOnError }}
{{ $closingBraces1 += '}'; }}
{{ var $code = $rule.code(it, $rule.keyword); }}
{{? $code }}
{{= $code }}
{{? $breakOnError }}
{{ $closingBraces1 += '}'; }}
{{?}}
{{?}}
{{?}}
{{~}}

28
lib/patternGroups.js Normal file
View File

@ -0,0 +1,28 @@
'use strict';
module.exports = function (ajv) {
console.warn('keyword "patternGroups" is deprecated. It will be removed in v6.0.0 (unless it is added to JSON-schema standard).');
ajv.addKeyword('patternGroups', {
// implemented in properties.jst
metaSchema: {
type: 'object',
additionalProperties: {
type: 'object',
required: [ 'schema' ],
properties: {
maximum: {
type: 'integer',
minimum: 0
},
minimum: {
type: 'integer',
minimum: 0
},
schema: { $ref: 'http://json-schema.org/draft-04/schema#' }
},
additionalProperties: false
}
}
});
ajv.RULES.all.properties.implements.push('patternGroups');
};

View File

@ -271,30 +271,6 @@
]
},
"contains": { "$ref": "#" },
"patternGroups": {
"type": "object",
"additionalProperties": {
"type": "object",
"required": [ "schema" ],
"properties": {
"maximum": {
"anyOf": [
{ "$ref": "#/definitions/positiveInteger" },
{ "$ref": "#/definitions/$data" }
]
},
"minimum": {
"anyOf": [
{ "$ref": "#/definitions/positiveIntegerDefault0" },
{ "$ref": "#/definitions/$data" }
]
},
"schema": { "$ref": "#" }
},
"additionalProperties": false
},
"default": {}
},
"switch": {
"type": "array",
"items": {

View File

@ -24,6 +24,4 @@ function enableV5(ajv) {
return { not: { items: { not: schema } } };
}
});
ajv.addKeyword('patternGroups'); // implemented in properties.jst
ajv.RULES.all.properties.implements.push('patternGroups');
}

View File

@ -117,7 +117,7 @@ describe('Ajv Options', function () {
});
it('should only validate against own properties when using patternGroups', function() {
var ajv = new Ajv({ v5: true, allErrors: true, ownProperties: true });
var ajv = new Ajv({ allErrors: true, ownProperties: true, patternGroups: true });
var validate = ajv.compile({
patternGroups: {
'f.*o': { schema: { type: 'integer' } }

View File

@ -6,7 +6,11 @@ var jsonSchemaTest = require('json-schema-test')
, suite = require('./browser_test_suite')
, after = require('./after_test');
var instances = getAjvInstances(options, { v5: true, unknownFormats: ['allowedUnknown'] });
var instances = getAjvInstances(options, {
v5: true,
patternGroups: true,
unknownFormats: ['allowedUnknown']
});
jsonSchemaTest(instances, {