uniqueItems, $ref inlined

master
Evgeny Poberezkin 2015-05-31 18:39:12 +01:00
parent dcf20e93dc
commit 99bec2b8c4
10 changed files with 42 additions and 61 deletions

View File

@ -1,16 +1,13 @@
function (data, dataPath) {
{{? it.resolveRef(it.schema) }}
var result = validateRef('{{= it.schema }}', data, dataPath);
if (!result.valid) validate.errors.push.apply(validate.errors, result.errors);
return result.valid;
{{??}}
validate.errors.push({
keyword: '$ref',
dataPath: dataPath,
message: 'can\'t resolve reference {{= it.schema}}'
{{? it.opts.verbose }}, schema: '{{= it.schema }}', data: data{{?}}
});
return false;
{{?}}
}
{{? it.resolveRef(it.schema.$ref) }}
var result = validateRef('{{= it.schema.$ref }}', data, dataPath);
if (!result.valid) validate.errors.push.apply(validate.errors, result.errors);
var valid = result.valid;
{{??}}
validate.errors.push({
keyword: '$ref',
dataPath: dataPath,
message: 'can\'t resolve reference {{= it.schema.$ref}}'
{{? it.opts.verbose }}, schema: '{{= it.schema.$ref }}', data: data{{?}}
});
var valid = false;
{{?}}

View File

@ -1,7 +1,7 @@
{{ var $enum_itemsHash = it.toHash(it.schema.enum, it.stableStringify); }}
{{ var $itemsHash = it.toHash(it.schema.enum, it.stableStringify); }}
var itemsHash = {{= JSON.stringify($enum_itemsHash) }};
var valid = itemsHash[stableStringify(data)] || false;
var req_itemsHash = {{= JSON.stringify($itemsHash) }};
var valid = req_itemsHash[stableStringify(data)] || false;
if (!valid) validate.errors.push({
keyword: 'enum',

View File

@ -10,13 +10,13 @@ var RULES = module.exports = [
{ type: 'string',
inline: [ 'maxLength', 'minLength', 'pattern', 'format' ] },
{ type: 'array',
inline: [ 'maxItems', 'minItems' ],
func: [ 'items', 'uniqueItems' ] },
inline: [ 'maxItems', 'minItems', 'uniqueItems' ],
func: [ 'items' ] },
{ type: 'object',
inline: [ 'maxProperties', 'minProperties', 'required' ],
func: [ 'dependencies', 'properties' ] },
{ inline: [ 'type', 'enum' ],
func: [ '$ref', 'not', 'anyOf', 'oneOf', 'allOf' ] }
{ inline: [ '$ref', 'type', 'enum' ],
func: [ 'not', 'anyOf', 'oneOf', 'allOf' ] }
];

View File

@ -11,8 +11,6 @@
#}}
function (data, dataPath) {
'use strict';
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
{{? Array.isArray(it.schema) }}

View File

@ -1,6 +1,4 @@
function (data, dataPath) {
'use strict';
var errs = validate.errors.length;
var valid = ({{= it.validate(it) }})(data, dataPath);

View File

@ -1,6 +1,4 @@
function (data, dataPath) {
'use strict';
var foundValid = false
, errs = validate.errors.length;

View File

@ -9,13 +9,10 @@
#}}
function (data, dataPath) {
'use strict';
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
{{
var $propertyKeys = Object.keys(it.schema || {})
, $pProperties = it.parentSchema.patternProperties || {}
var $pProperties = it.parentSchema.patternProperties || {}
, $pPropertyKeys = $pProperties && Object.keys($pProperties)
, $aProperties = it.parentSchema.additionalProperties
, $noAdditional = $aProperties === false
@ -99,7 +96,7 @@ function (data, dataPath) {
}
{{?}}
{{~ $propertyKeys:$propertyKey }}
{{ if (it.schema) for (var $propertyKey in it.schema) { }}
{{ var $schema = it.schema[$propertyKey]; }}
{{? Object.keys($schema).length }}
@ -115,7 +112,7 @@ function (data, dataPath) {
{{# def.validateProperty:useKey }}
}
{{?}}
{{~}}
{{ } }}
{{~ $pPropertyKeys:$propertyKey }}

View File

@ -1,8 +1,8 @@
{{ var $req_schema = it.schema.required; }}
{{? $req_schema.length <= 100 }}
var valid = {{~ $req_schema:$req_property:$i }}
{{ var $schema = it.schema.required; }}
{{? $schema.length <= 100 }}
var valid = {{~ $schema:$property:$i }}
{{? $i}} && {{?}}
data.hasOwnProperty('{{= it.escapeQuotes($req_property) }}')
data.hasOwnProperty('{{= it.escapeQuotes($property) }}')
{{~}};
{{??}}
var valid = true;
@ -19,6 +19,6 @@
if (!valid) validate.errors.push({
keyword: 'required',
dataPath: dataPath,
message: 'properties {{= $req_schema.slice(0, 7).join(",") }}{{? $req_schema.length > 7}}...{{?}} are required'
message: 'properties {{= $schema.slice(0, 7).join(",") }}{{? $schema.length > 7}}...{{?}} are required'
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath + '.required' }}, data: data{{?}}
});

View File

@ -1,33 +1,26 @@
function (data, dataPath) {
'use strict';
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
/* TODO change to inline ??? with break in the loop */
var valid = true;
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
{{? it.schema && it.opts.uniqueItems !== false }}
if (data.length <= 1) return true;
var itemsHash = {}, errors = [];
{{? it.schema.uniqueItems && it.opts.uniqueItems !== false }}
if (data.length > 1) {
var unique_itemsHash = {};
for (var i = 0; i < data.length; i++) {
var itemStr = stableStringify(data[i]);
var isDuplicate = itemsHash[itemStr];
if (isDuplicate) {
var valid = valid && !unique_itemsHash[itemStr];
if (valid)
unique_itemsHash[itemStr] = true;
else {
validate.errors.push({
keyword: 'uniqueItems',
dataPath: dataPath,
message: 'item #' + i + 'is duplicate'
{{? it.opts.verbose }}, schema: {{= it.schema }}, data: data{{?}}
{{? it.opts.verbose }}, schema: {{= it.schema.uniqueItems }}, data: data{{?}}
});
{{? !it.opts.allErrors }} return false; {{?}}
} else {
itemsHash[itemStr] = true;
{{? !it.opts.allErrors }} break; {{?}}
}
}
return {{? it.opts.allErrors }} errs == validate.errors.length {{??}} true {{?}};
{{??}}
return true;
{{?}}
}
}
{{?}}

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.1.5",
"version": "0.1.6",
"description": "Another JSON schema Validator",
"main": "lib/ajv.js",
"scripts": {