ajv
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -27,2 +27,3 @@ 'use strict'; | ||
toHash: toHash, | ||
property: property, | ||
resolveRef: resolveRef, | ||
@@ -37,3 +38,3 @@ checkDataType: checkDataType, | ||
var validate; | ||
eval('validate = ' + validateCode); | ||
eval(validateCode); | ||
@@ -69,2 +70,10 @@ validate.schema = schema; | ||
var IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i; | ||
function property(key) { | ||
return IDENTIFIER.test(key) | ||
? '.' + key | ||
: "['" + key + "']"; | ||
} | ||
function checkDataType(dataType, lvl) { | ||
@@ -137,3 +146,3 @@ var data = 'data' + lvl; | ||
function escapeQuotes(str) { | ||
return str.replace(/"/g, '\\"'); | ||
return str.replace(/'/g, "\\'"); | ||
} |
@@ -15,3 +15,2 @@ {{# def.definitions }} | ||
{{ $it.inline = true; }} | ||
{{= it.validate($it) }} | ||
@@ -18,0 +17,0 @@ |
@@ -5,3 +5,3 @@ {{# def.definitions }} | ||
var errs{{=$lvl}} = validate.errors.length; | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$valid}} = false; | ||
@@ -20,3 +20,2 @@ | ||
{{ $it.inline = true; }} | ||
{{= it.validate($it) }} | ||
@@ -29,2 +28,2 @@ | ||
if ({{=$valid}}) validate.errors.length = errs{{=$lvl}}; | ||
if ({{=$valid}}) validate.errors.length = {{=$errs}}; |
@@ -9,3 +9,4 @@ {{## def.setup:keyword: | ||
var $data = 'data' + $dataLvl | ||
, $valid = 'valid' + $lvl; | ||
, $valid = 'valid' + $lvl | ||
, $errs = 'errs' + $lvl; | ||
}} | ||
@@ -33,2 +34,10 @@ #}} | ||
{{## def.ifResultValid: | ||
{{? $breakOnError }} | ||
if (valid{{=$it.level}}) { | ||
{{ $closingBraces += '}'; }} | ||
{{?}} | ||
#}} | ||
{{## def.elseIfValid: | ||
@@ -67,5 +76,3 @@ {{? $breakOnError }} | ||
{{## def.checkError:rule: | ||
if (!{{=$valid}}) { | ||
{{# def.error:rule }} | ||
} | ||
if (!{{=$valid}}) {{# def.error:rule }} | ||
#}} | ||
@@ -95,3 +102,3 @@ | ||
type: "'should be {{? $isArray }}{{= $schema.join(\",\") }}{{??}}{{=$schema}}{{?}}'", | ||
uniqueItems: "'items ## ' + j{{=$lvl}} + ' and ' + i{{=$lvl}} + ' are duplicate'" | ||
uniqueItems: "'items ## ' + j + ' and ' + i + ' are duplicate'" | ||
} #}} | ||
@@ -98,0 +105,0 @@ |
@@ -17,3 +17,3 @@ {{# def.definitions }} | ||
var errs{{=$lvl}} = validate.errors.length; | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$valid}}; | ||
@@ -23,5 +23,5 @@ | ||
{{ for ($property in $propertyDeps) { }} | ||
if ({{=$data}}.hasOwnProperty('{{= $property }}')) { | ||
if ({{=$data}}['{{= $property }}'] !== undefined) { | ||
{{ $deps = $propertyDeps[$property]; }} | ||
{{=$valid}} = {{~ $deps:$dep:$i }}{{?$i}} && {{?}}{{=$data}}.hasOwnProperty('{{= $dep}}'){{~}}; | ||
{{=$valid}} = {{~ $deps:$dep:$i }}{{?$i}} && {{?}}{{=$data}}['{{= $dep}}'] !== undefined{{~}}; | ||
{{# def.checkError:'dependencies' }} | ||
@@ -36,15 +36,14 @@ {{# def.elseIfValid }} | ||
{{? Object.keys($sch).length }} | ||
if ({{=$data}}.hasOwnProperty('{{= $property }}')) { | ||
valid{{=$it.level}} = true; | ||
if ({{=$data}}['{{= $property }}'] !== undefined) { | ||
{{ | ||
$it.schema = $sch; | ||
$it.schemaPath = $schemaPath + '[\'' + it.escapeQuotes($property) + '\']'; | ||
$it.schemaPath = $schemaPath + "['" + it.escapeQuotes($property) + "']"; | ||
}} | ||
{{ $it.inline = true; }} | ||
{{= it.validate($it) }} | ||
{{=$valid}} = valid{{=$it.level}}; | ||
} | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{?}} | ||
@@ -55,4 +54,4 @@ {{ } }} | ||
{{=$valid}} = errs{{=$lvl}} == validate.errors.length; | ||
{{=$valid}} = {{=$errs}} == validate.errors.length; | ||
{{# def.cleanUp }} |
@@ -7,12 +7,10 @@ {{# def.definitions }} | ||
{{## def.validateItems:startFrom: | ||
for (var i = {{=startFrom}}; i < {{=$data}}.length; i++) { | ||
var data{{=$dataNxt}} = {{=$data}}[i] | ||
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + '[' + i + ']'; | ||
for (var i{{=$lvl}} = {{=startFrom}}; i{{=$lvl}} < {{=$data}}.length; i{{=$lvl}}++) { | ||
var data{{=$dataNxt}} = {{=$data}}[i{{=$lvl}}] | ||
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + '[' + i{{=$lvl}} + ']'; | ||
{{ $it.inline = true; }} | ||
{{= it.validate($it) }}; | ||
{{? $breakOnError }} | ||
{{=$valid}} = valid{{=$it.level}}; | ||
if (!{{=$valid}}) break; | ||
if (!valid{{=$it.level}}) break; | ||
{{?}} | ||
@@ -24,3 +22,3 @@ } | ||
var errs{{=$lvl}} = validate.errors.length; | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$valid}}; | ||
@@ -39,3 +37,3 @@ | ||
{{? Object.keys($sch).length }} | ||
{{=$valid}} = true; | ||
valid{{=$it.level}} = true; | ||
@@ -51,8 +49,6 @@ if ({{=$data}}.length > {{=$i}}) { | ||
{{ $it.inline = true; }} | ||
{{= it.validate($it) }} | ||
{{=$valid}} = valid{{=$it.level}}; | ||
} | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{?}} | ||
@@ -66,2 +62,3 @@ {{~}} | ||
}} | ||
valid{{=$it.level}} = true; | ||
@@ -72,3 +69,3 @@ if ({{=$data}}.length > {{= $schema.length }}) { | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{?}} | ||
@@ -83,3 +80,3 @@ | ||
{{# def.validateItems: 0 }} | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{?}} | ||
@@ -89,4 +86,4 @@ | ||
{{=$valid}} = errs{{=$lvl}} == validate.errors.length; | ||
{{=$valid}} = {{=$errs}} == validate.errors.length; | ||
{{# def.cleanUp }} |
@@ -8,6 +8,5 @@ {{# def.definitions }} | ||
$it.schemaPath = $schemaPath; | ||
$it.inline = true; | ||
}} | ||
var errs{{=$lvl}} = validate.errors.length; | ||
var {{=$errs}} = validate.errors.length; | ||
@@ -19,3 +18,3 @@ {{= it.validate($it) }} | ||
if ({{=$valid}}) validate.errors.length = errs{{=$lvl}}; | ||
if ({{=$valid}}) validate.errors.length = {{=$errs}}; | ||
else {{# def.error:'not' }} |
@@ -6,3 +6,3 @@ {{# def.definitions }} | ||
var errs{{=$lvl}} = validate.errors.length; | ||
var {{=$errs}} = validate.errors.length; | ||
var validCount{{=$lvl}} = 0; | ||
@@ -19,3 +19,2 @@ | ||
$it.schemaPath = $schemaPath + '[' + $i + ']'; | ||
$it.inline = true; | ||
}} | ||
@@ -33,3 +32,3 @@ | ||
if ({{=$valid}}) validate.errors.length = errs{{=$lvl}}; | ||
if ({{=$valid}}) validate.errors.length = {{=$errs}}; | ||
else {{# def.error:'oneOf' }} |
@@ -6,11 +6,2 @@ {{# def.definitions }} | ||
{{## def.validateProperty:useKey: | ||
var data{{=$dataNxt}} = {{=$data}}[{{= useKey }}] | ||
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + '.' + {{= useKey }}; | ||
{{ $it.inline = true; }} | ||
{{= it.validate($it) }}; | ||
{{=$valid}} = valid{{=$it.level}}; | ||
#}} | ||
{{ | ||
@@ -29,4 +20,4 @@ var $dataNxt = $it.dataLevel = it.dataLevel + 1; | ||
var errs{{=$lvl}} = validate.errors.length; | ||
var {{=$valid}} = true; | ||
var {{=$errs}} = validate.errors.length; | ||
var valid{{=$it.level}} = true; | ||
@@ -38,3 +29,3 @@ {{? $checkAdditional }} | ||
{{? $noAdditional }} | ||
var {{=$valid}} = Object.keys({{=$data}}).length <= Object.keys(propertiesSchema{{=$lvl}}).length; | ||
valid{{=$it.level}} = Object.keys({{=$data}}).length <= Object.keys(propertiesSchema{{=$lvl}}).length; | ||
{{# def.checkError:'additionalProperties' }} | ||
@@ -55,3 +46,3 @@ {{# def.elseIfValid }} | ||
for (var key{{=$lvl}} in {{=$data}}) { | ||
var isAdditional{{=$lvl}} = !propertiesSchema{{=$lvl}}.hasOwnProperty(key{{=$lvl}}); | ||
var isAdditional{{=$lvl}} = propertiesSchema{{=$lvl}}[key{{=$lvl}}] === undefined; | ||
@@ -72,3 +63,3 @@ {{? $pPropertyKeys.length }} | ||
{{? $noAdditional }} | ||
{{=$valid}} = false; | ||
valid{{=$it.level}} = false; | ||
{{# def.error:'additionalProperties' }} | ||
@@ -82,5 +73,8 @@ {{? $breakOnError }} break; {{?}} | ||
{{ var $useKey = 'key' + $lvl; }} | ||
{{# def.validateProperty:$useKey }} | ||
{{? $breakOnError }} if (!{{=$valid}}) break; {{?}} | ||
var data{{=$dataNxt}} = {{=$data}}[key{{=$lvl}}] | ||
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + property(key{{=$lvl}}); | ||
{{= it.validate($it) }}; | ||
{{? $breakOnError }} if (!valid{{=$it.level}}) break; {{?}} | ||
{{?}} | ||
@@ -90,3 +84,3 @@ } | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{?}} | ||
@@ -101,14 +95,18 @@ | ||
$it.schema = $sch; | ||
$it.schemaPath = $schemaPath + '["' + it.escapeQuotes($propertyKey) + '"]'; | ||
$it.schemaPath = $schemaPath + "['" + it.escapeQuotes($propertyKey) + "']"; | ||
}} | ||
{{? $breakOnError }} {{=$valid}} = true; {{?}} | ||
if ({{=$data}}['{{= $propertyKey }}'] !== undefined) { | ||
{{ /* TODO cache data types and paths by keys for patternProperties */ }} | ||
{{ var $useKey = '"' + $propertyKey + '"'; }} | ||
{{# def.validateProperty:$useKey }} | ||
{{? $breakOnError }} valid{{=$it.level}} = true; {{?}} | ||
{{ var $prop = it.property($propertyKey); }} | ||
var data{{=$dataNxt}} = {{=$data}}{{=$prop}}; | ||
if (data{{=$dataNxt}} !== undefined) { | ||
var dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + "{{=$prop}}"; | ||
{{= it.validate($it) }}; | ||
} | ||
{{?}} | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{ } }} | ||
@@ -126,2 +124,4 @@ {{?}} | ||
valid{{=$it.level}} = true; | ||
for (var key{{=$lvl}} in {{=$data}}) { | ||
@@ -131,9 +131,11 @@ var keyMatches{{=$lvl}} = pPropertiesRegexps{{=$lvl}}['{{= $propertyKey }}'].test(key{{=$lvl}}); | ||
if (keyMatches{{=$lvl}}) { | ||
{{ var $useKey = 'key' + $lvl; }} | ||
{{# def.validateProperty:$useKey }} | ||
{{? $breakOnError }} if (!{{=$valid}}) break; {{?}} | ||
var data{{=$dataNxt}} = {{=$data}}[key{{=$lvl}}] | ||
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + property(key{{=$lvl}}); | ||
{{= it.validate($it) }}; | ||
{{? $breakOnError }} if (!valid{{=$it.level}}) break; {{?}} | ||
} | ||
} | ||
{{# def.ifValid }} | ||
{{# def.ifResultValid }} | ||
{{?}} | ||
@@ -144,4 +146,4 @@ {{~}} | ||
{{=$valid}} = errs{{=$lvl}} == validate.errors.length; | ||
var {{=$valid}} = {{=$errs}} == validate.errors.length; | ||
{{# def.cleanUp }} |
@@ -12,4 +12,4 @@ {{# def.definitions }} | ||
for (var i{{=$lvl}} = 0; i{{=$lvl}} < schema{{=$lvl}}.length; i{{=$lvl}}++) { | ||
{{=$valid}} = data[schema{{=$lvl}}[i{{=$lvl}}]] !== undefined; | ||
for (var i = 0; i < schema{{=$lvl}}.length; i++) { | ||
{{=$valid}} = data[schema{{=$lvl}}[i]] !== undefined; | ||
if (!{{=$valid}}) break; | ||
@@ -16,0 +16,0 @@ } |
@@ -8,7 +8,7 @@ {{# def.definitions }} | ||
if ({{=$data}}.length > 1) { | ||
var i{{=$lvl}} = {{=$data}}.length, j{{=$lvl}}; | ||
var i = {{=$data}}.length, j; | ||
outer: | ||
for (;i{{=$lvl}}--;) { | ||
for (j{{=$lvl}} = i{{=$lvl}}; j{{=$lvl}}--;) { | ||
if (equal({{=$data}}[i{{=$lvl}}], {{=$data}}[j{{=$lvl}}])) { | ||
for (;i--;) { | ||
for (j = i; j--;) { | ||
if (equal({{=$data}}[i], {{=$data}}[j])) { | ||
{{=$valid}} = false; | ||
@@ -15,0 +15,0 @@ break outer; |
@@ -16,10 +16,9 @@ {{ /** | ||
delete it.isRoot; | ||
var $lvl = 0; | ||
it.level = 1; | ||
var $lvl = it.level = 0; | ||
var $dataLvl = it.dataLevel = 0; | ||
}} | ||
function ( data0 ) { | ||
var dataPath0 = ''; | ||
var errs{{=$lvl}} = validate.errors.length = 0; | ||
validate = function (data0, dataPath0) { | ||
dataPath0 = dataPath0 || ''; | ||
validate.errors.length = 0; | ||
{{??}} | ||
@@ -71,3 +70,3 @@ {{ | ||
{{? $root }} | ||
return errs{{=$lvl}} == validate.errors.length; | ||
return validate.errors.length === 0; | ||
} | ||
@@ -87,3 +86,3 @@ {{??}} | ||
function $shouldUseRule($rule) { | ||
var $use = it.schema.hasOwnProperty($rule.keyword); | ||
var $use = it.schema[$rule.keyword]; | ||
if (!$use && $rule.keyword == 'properties') { | ||
@@ -90,0 +89,0 @@ var $pProperties = it.schema.patternProperties |
{ | ||
"name": "ajv", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
190828