ajv
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -7,8 +7,8 @@ 'use strict'; | ||
, formats = require('./formats') | ||
, resolve = require('./resolve'); | ||
, resolve = require('./resolve') | ||
, equal = require('./equal'); | ||
var RULES = require('./rules') | ||
// , validateGenerator = require('./validate'); | ||
, validateTemplateStr = fs.readFileSync(__dirname + '/validate.dot.js') | ||
, validateGenerator = doT.compile(validateTemplateStr); | ||
, validateTemplate = fs.readFileSync(__dirname + '/validate.dot.js') | ||
, validateGenerator = doT.compile(validateTemplate); | ||
@@ -15,0 +15,0 @@ module.exports = compile; |
@@ -1,49 +0,58 @@ | ||
function (data, dataPath) { | ||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}} | ||
var errs = validate.errors.length; | ||
var valid; | ||
{{ | ||
var $schemaDeps = {} | ||
, $propertyDeps = {}; | ||
{{ | ||
var $breakOnError = !it.opts.allErrors | ||
, $closingBraces = '' | ||
, $schemaDeps = {} | ||
, $propertyDeps = {}; | ||
for ($property in it.schema) { | ||
var $schema = it.schema[$property]; | ||
var $deps = Array.isArray($schema) ? $propertyDeps : $schemaDeps; | ||
$deps[$property] = $schema; | ||
for ($property in it.schema.dependencies) { | ||
var $schema = it.schema.dependencies[$property]; | ||
var $deps = Array.isArray($schema) ? $propertyDeps : $schemaDeps; | ||
$deps[$property] = $schema; | ||
} | ||
}} | ||
{{ for ($property in $propertyDeps) { }} | ||
if (data.hasOwnProperty('{{= $property }}')) { | ||
{{ $deps = $propertyDeps[$property]; }} | ||
valid = {{~ $deps:$dep:$i }}{{?$i}} && {{?}}data.hasOwnProperty('{{= $dep}}'){{~}}; | ||
if (!valid) { | ||
validate.errors.push({ | ||
keyword: 'dependencies', | ||
dataPath: dataPath, | ||
message: '{{? $deps.length == 1 }}property {{= $deps[0] }} is{{??}}properties {{= $deps.join(",") }} are{{?}} required when property {{= $property }} is present' | ||
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath + '.dependencies' }}, data: data{{?}} | ||
}); | ||
} | ||
}} | ||
{{? $breakOnError }} | ||
{{ $closingBraces += '}'; }} | ||
else { | ||
{{?}} | ||
} | ||
{{ } }} | ||
{{ for ($property in $propertyDeps) { }} | ||
if (data.hasOwnProperty('{{= $property }}')) { | ||
{{ $deps = $propertyDeps[$property]; }} | ||
var valid = {{~ $deps:$dep:$i }}{{?$i}} && {{?}}data.hasOwnProperty('{{= $dep}}'){{~}}; | ||
if (!valid) { | ||
validate.errors.push({ | ||
keyword: 'dependencies', | ||
dataPath: dataPath, | ||
message: '{{? $deps.length == 1 }}property {{= $deps[0] }} is{{??}}properties {{= $deps.join(",") }} are{{?}} required when property {{= $property }} is present' | ||
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}} | ||
}); | ||
{{ var $it = it.copy(it); }} | ||
{{? !it.opts.allErrors }} return false; {{?}} | ||
} | ||
} | ||
{{ } }} | ||
{{ for ($property in $schemaDeps) { }} | ||
if (data.hasOwnProperty('{{= $property }}')) { | ||
{{ | ||
var $schema = $schemaDeps[$property]; | ||
$it.schema = $schema; | ||
$it.schemaPath = it.schemaPath + '.dependencies["' + it.escapeQuotes($property) + '"]'; | ||
}} | ||
{{ for ($property in $schemaDeps) { }} | ||
if (data.hasOwnProperty('{{= $property }}')) { | ||
{{ | ||
var $schema = $schemaDeps[$property]; | ||
var $it = it.copy(it); | ||
$it.schema = $schema; | ||
$it.schemaPath = it.schemaPath + '["' + it.escapeQuotes($property) + '"]'; | ||
}} | ||
{{? $breakOnError }} valid = {{?}} | ||
({{= it.validate($it) }})(data, dataPath); | ||
} | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= it.validate($it) }})(data, dataPath); | ||
{{? $breakOnError }} | ||
{{ $closingBraces += '}'; }} | ||
if (valid) { | ||
{{?}} | ||
{{ } }} | ||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}} | ||
} | ||
{{ } }} | ||
{{? $breakOnError }}{{= $closingBraces }}{{?}} | ||
return {{? it.opts.allErrors }} errs == validate.errors.length {{??}} true {{?}}; | ||
} | ||
valid = errs == validate.errors.length; |
@@ -9,11 +9,10 @@ 'use strict'; | ||
{ type: 'number', | ||
inline: [ 'maximum', 'minimum', 'multipleOf'] }, | ||
rules: [ 'maximum', 'minimum', 'multipleOf'] }, | ||
{ type: 'string', | ||
inline: [ 'maxLength', 'minLength', 'pattern', 'format' ] }, | ||
rules: [ 'maxLength', 'minLength', 'pattern', 'format' ] }, | ||
{ type: 'array', | ||
inline: [ 'maxItems', 'minItems', 'uniqueItems', 'items' ] }, | ||
rules: [ 'maxItems', 'minItems', 'uniqueItems', 'items' ] }, | ||
{ type: 'object', | ||
inline: [ 'maxProperties', 'minProperties', 'required', 'properties' ], | ||
func: [ 'dependencies' ] }, | ||
{ inline: [ '$ref', 'type', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] } | ||
rules: [ 'maxProperties', 'minProperties', 'required', 'dependencies', 'properties' ] }, | ||
{ rules: [ '$ref', 'type', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] } | ||
]; | ||
@@ -23,17 +22,9 @@ | ||
RULES.forEach(function (group) { | ||
group.rules = []; | ||
addRules('inline', true); | ||
addRules('func'); | ||
function addRules(mode, isInline) { | ||
var keywords = group[mode]; | ||
if (keywords) keywords.forEach(function (keyword) { | ||
var template = fs.readFileSync(__dirname + '/' + keyword + '.dot.js'); | ||
group.rules.push({ | ||
keyword: keyword, | ||
code: doT.compile(template), | ||
inline: isInline | ||
}); | ||
}); | ||
} | ||
group.rules = group.rules.map(function (keyword) { | ||
var template = fs.readFileSync(__dirname + '/' + keyword + '.dot.js'); | ||
return { | ||
keyword: keyword, | ||
code: doT.compile(template) | ||
}; | ||
}); | ||
}); |
@@ -1,2 +0,8 @@ | ||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}} | ||
{{## def.checkUnique: | ||
if (unique_itemsHash[item]) { | ||
valid = false; | ||
break outer; | ||
} | ||
unique_itemsHash[item] = true; | ||
#}} | ||
@@ -9,19 +15,35 @@ var valid = true; | ||
outer: | ||
for (var i = 0; i < data.length; i++) { | ||
var itemStr = stableStringify(data[i]); | ||
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.uniqueItems }}, data: data{{?}} | ||
}); | ||
{{? !it.opts.allErrors }} break; {{?}} | ||
var item = data[i]; | ||
switch (typeof item) { | ||
case 'object': | ||
if (item === null) { | ||
{{# def.checkUnique }} | ||
} else { | ||
for (var j = i+1; j < data.length; j++) { | ||
if (equal(item, data[j])) { | ||
valid = false; | ||
break outer; | ||
} | ||
} | ||
} | ||
break; | ||
case 'string': | ||
item = '"' + item; | ||
/* fall through */ | ||
default: | ||
{{# def.checkUnique }} | ||
} | ||
} | ||
if (!valid) { | ||
validate.errors.push({ | ||
keyword: 'uniqueItems', | ||
dataPath: dataPath, | ||
message: 'item #' + i + 'is duplicate' | ||
{{? it.opts.verbose }}, schema: {{= it.schema.uniqueItems }}, data: data{{?}} | ||
}); | ||
} | ||
} | ||
{{?}} |
@@ -25,21 +25,10 @@ {{ /** | ||
{{? $shouldUseGroup($rulesGroup) }} | ||
{{? $rulesGroup.type }} if ({{= it.checkDataType($rulesGroup.type) }}) { {{?}} | ||
{{? $rulesGroup.type }} if ({{= it.checkDataType($rulesGroup.type) }}) { {{?}} | ||
{{~ $rulesGroup.rules:$rule }} | ||
{{? $shouldUseRule($rule) }} | ||
{{? $rule.inline }} | ||
{{= $rule.code(it) }} | ||
{{??}} | ||
{{ | ||
var $it = it.copy(it); | ||
$it.schema = it.schema[$rule.keyword]; | ||
$it.schemaPath = it.schemaPath + '.' + $rule.keyword; | ||
}} | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= $rule.code($it) }})(data, dataPath); | ||
{{?}} | ||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}} | ||
{{= $rule.code(it) }} | ||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}} | ||
{{?}} | ||
{{~}} | ||
{{? $rulesGroup.type }} } {{?}} | ||
{{? $rulesGroup.type }} } {{?}} | ||
{{?}} | ||
@@ -46,0 +35,0 @@ {{~}} |
{ | ||
"name": "ajv", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
188617
106
5797