ajv
Advanced tools
Comparing version 0.3.12 to 0.4.0
@@ -5,5 +5,9 @@ {{# def.definitions }} | ||
{{? $schema == '#' || $schema == '#/' }} | ||
{{? $breakOnError }} if ( {{?}} | ||
validate({{=$data}}, (dataPath || '') + {{= it.errorPath }}, true) | ||
{{? $breakOnError }} ) { {{??}};{{?}} | ||
var errors{{=$lvl}} = validate.errors; | ||
if (!validate({{=$data}}, (dataPath || '') + {{= it.errorPath }})) { | ||
if (errors{{=$lvl}} !== null) { | ||
validate.errors = errors{{=$lvl}}.concat(validate.errors); | ||
} | ||
errors = validate.errors.length; | ||
} {{? $breakOnError }} else { {{?}} | ||
{{??}} | ||
@@ -16,6 +20,8 @@ {{ $id = it.resolveRef(it.baseId, $schema); }} | ||
if (!refVal[{{=$id}}]({{=$data}}, (dataPath || '') + {{= it.errorPath }})) { | ||
validate.errors.push.apply(validate.errors, refVal[{{=$id}}].errors); | ||
} | ||
{{? $breakOnError }} else { {{?}} | ||
validate.errors = validate.errors === null | ||
? refVal[{{=$id}}].errors | ||
: validate.errors.concat(refVal[{{=$id}}].errors); | ||
errors = validate.errors.length; | ||
} {{? $breakOnError }} else { {{?}} | ||
{{?}} | ||
{{?}} |
@@ -11,3 +11,3 @@ {{# def.definitions }} | ||
{{? $noEmptySchema }} | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$errs}} = errors; | ||
var {{=$valid}} = false; | ||
@@ -32,3 +32,7 @@ | ||
if ({{=$valid}}) { | ||
validate.errors.length = {{=$errs}}; | ||
errors = {{=$errs}}; | ||
if (validate.errors !== null) { | ||
if ({{=$errs}}) validate.errors.length = {{=$errs}}; | ||
else validate.errors = null; | ||
} | ||
{{? it.opts.allErrors }} } {{?}} | ||
@@ -35,0 +39,0 @@ |
@@ -87,13 +87,7 @@ {{## def.setup:_keyword: | ||
{{## def.cleanUp: | ||
{{ | ||
out = out.replace(/if\s*\([^)]+\)\s*\{\s*\}\s*else\s*{\s*}/g, '') | ||
.replace(/if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g, '') | ||
.replace(/if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g, 'if (!($1))'); | ||
}} | ||
#}} | ||
{{## def.cleanUp: {{ out = it.util.cleanUpCode(out); }} #}} | ||
{{## def.error:_rule: | ||
validate.errors.push({ | ||
var err = { | ||
keyword: '{{=_rule}}', | ||
@@ -103,3 +97,6 @@ dataPath: {{= it.errorPath }}, | ||
{{? it.opts.verbose }}, schema: {{# def._errorSchemas[_rule] }}, data: {{=$data}}{{?}} | ||
}); | ||
}; | ||
if (validate.errors === null) validate.errors = [err]; | ||
else validate.errors.push(err); | ||
errors++; | ||
#}} | ||
@@ -109,3 +106,5 @@ | ||
{{## def.checkError:_rule: | ||
if (!{{=$valid}}) {{# def.error:_rule }} | ||
if (!{{=$valid}}) { | ||
{{# def.error:_rule }} | ||
} | ||
#}} | ||
@@ -112,0 +111,0 @@ |
@@ -17,3 +17,3 @@ {{# def.definitions }} | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$errs}} = errors; | ||
@@ -30,5 +30,5 @@ {{## def.checkPropertyDeps: | ||
{{ $deps = $propertyDeps[$property]; }} | ||
if ({{# def.checkPropertyDeps }}) | ||
if ({{# def.checkPropertyDeps }}) { | ||
{{# def.error:'dependencies' }} | ||
{{# def.elseIfValid }} | ||
} {{# def.elseIfValid }} | ||
} | ||
@@ -58,5 +58,5 @@ {{ } }} | ||
{{= $closingBraces }} | ||
if ({{=$errs}} == validate.errors.length) { | ||
if ({{=$errs}} == errors) { | ||
{{?}} | ||
{{# def.cleanUp }} |
@@ -20,7 +20,7 @@ {{# def.definitions }} | ||
{{? it.opts.format !== false && $format }} | ||
if (! {{# def.checkFormat }}) | ||
if (! {{# def.checkFormat }}) { | ||
{{# def.error:'format' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} | ||
{{??}} | ||
{{? $breakOnError }} if (true) { {{?}} | ||
{{?}} |
@@ -28,3 +28,3 @@ {{# def.definitions }} | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$errs}} = errors; | ||
var {{=$valid}}; | ||
@@ -88,5 +88,5 @@ | ||
{{= $closingBraces }} | ||
if ({{=$errs}} == validate.errors.length) { | ||
if ({{=$errs}} == errors) { | ||
{{?}} | ||
{{# def.cleanUp }} |
@@ -9,4 +9,4 @@ {{# def.definitions }} | ||
if (! ({{=$data}} {{=$op}} {{=$schema}}) ) | ||
if (! ({{=$data}} {{=$op}} {{=$schema}}) ) { | ||
{{# def.error:'maximum' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
{{# def.definitions }} | ||
{{# def.setup:'maxItems' }} | ||
if ({{=$data}}.length > {{=$schema}}) | ||
if ({{=$data}}.length > {{=$schema}}) { | ||
{{# def.error:'maxItems' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
{{# def.definitions }} | ||
{{# def.setup:'maxLength' }} | ||
if ({{# def.strLength }} > {{=$schema}}) | ||
if ({{# def.strLength }} > {{=$schema}}) { | ||
{{# def.error:'maxLength' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
{{# def.definitions }} | ||
{{# def.setup:'maxProperties' }} | ||
if (Object.keys({{=$data}}).length > {{=$schema}}) | ||
if (Object.keys({{=$data}}).length > {{=$schema}}) { | ||
{{# def.error:'maxProperties' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
@@ -9,4 +9,4 @@ {{# def.definitions }} | ||
if (!( {{=$data}} {{=$op}} {{=$schema}} )) | ||
if (!( {{=$data}} {{=$op}} {{=$schema}} )) { | ||
{{# def.error:'minimum' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
{{# def.definitions }} | ||
{{# def.setup:'minItems' }} | ||
if ({{=$data}}.length < {{=$schema}}) | ||
if ({{=$data}}.length < {{=$schema}}) { | ||
{{# def.error:'minItems' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
{{# def.definitions }} | ||
{{# def.setup:'minLength' }} | ||
if ({{# def.strLength }} < {{=$schema}}) | ||
if ({{# def.strLength }} < {{=$schema}}) { | ||
{{# def.error:'minLength' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
{{# def.definitions }} | ||
{{# def.setup:'minProperties' }} | ||
if (Object.keys({{=$data}}).length < {{=$schema}}) | ||
if (Object.keys({{=$data}}).length < {{=$schema}}) { | ||
{{# def.error:'minProperties' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
@@ -5,4 +5,4 @@ {{# def.definitions }} | ||
var division{{=$lvl}} = {{=$data}} / {{=$schema}}; | ||
if ({{=$data}} / {{=$schema}} !== parseInt(division{{=$lvl}})) | ||
if ({{=$data}} / {{=$schema}} !== parseInt(division{{=$lvl}})) { | ||
{{# def.error:'multipleOf' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
@@ -11,10 +11,14 @@ {{# def.definitions }} | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$errs}} = errors; | ||
{{= it.validate($it) }} | ||
if (valid{{=$it.level}}) | ||
if (valid{{=$it.level}}) { | ||
{{# def.error:'not' }} | ||
else { | ||
validate.errors.length = {{=$errs}}; | ||
} else { | ||
errors = {{=$errs}}; | ||
if (validate.errors !== null) { | ||
if ({{=$errs}}) validate.errors.length = {{=$errs}}; | ||
else validate.errors = null; | ||
} | ||
@@ -21,0 +25,0 @@ {{? it.opts.allErrors }} } {{?}} |
@@ -6,3 +6,3 @@ {{# def.definitions }} | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$errs}} = errors; | ||
var prevValid{{=$lvl}} = false; | ||
@@ -35,7 +35,11 @@ var {{=$valid}} = false; | ||
if (!{{=$valid}}) | ||
if (!{{=$valid}}) { | ||
{{# def.error:'oneOf' }} | ||
else { | ||
validate.errors.length = {{=$errs}}; | ||
} else { | ||
errors = {{=$errs}}; | ||
if (validate.errors !== null) { | ||
if ({{=$errs}}) validate.errors.length = {{=$errs}}; | ||
else validate.errors = null; | ||
} | ||
{{? it.opts.allErrors }} } {{?}} |
@@ -5,4 +5,4 @@ {{# def.definitions }} | ||
{{ new RegExp($schema); /* test if regexp is valid to fail at compile time rather than in eval */}} | ||
if (! /{{= it.util.escapeRegExp($schema) }}/.test({{=$data}}) ) | ||
if (! /{{= it.util.escapeRegExp($schema) }}/.test({{=$data}}) ) { | ||
{{# def.error:'pattern' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} |
@@ -20,3 +20,3 @@ {{# def.definitions }} | ||
var {{=$errs}} = validate.errors.length; | ||
var {{=$errs}} = errors; | ||
var valid{{=$it.level}} = true; | ||
@@ -142,5 +142,5 @@ | ||
{{= $closingBraces }} | ||
if ({{=$errs}} == validate.errors.length) { | ||
if ({{=$errs}} == errors) { | ||
{{?}} | ||
{{# def.cleanUp }} |
@@ -12,5 +12,5 @@ {{# def.definitions }} | ||
{{? $schema.length <= 100 }} | ||
if ({{# def.checkRequired }}) | ||
if ({{# def.checkRequired }}) { | ||
{{# def.error:'required' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} | ||
{{??}} | ||
@@ -17,0 +17,0 @@ var schema{{=$lvl}} = validate.schema{{=$schemaPath}}; |
@@ -20,5 +20,5 @@ {{# def.definitions }} | ||
if (!{{=$valid}}) | ||
if (!{{=$valid}}) { | ||
{{# def.error:'uniqueItems' }} | ||
{{? $breakOnError }} else { {{?}} | ||
} {{? $breakOnError }} else { {{?}} | ||
{{??}} | ||
@@ -25,0 +25,0 @@ {{? $breakOnError }} if (true) { {{?}} |
@@ -15,2 +15,3 @@ 'use strict'; | ||
varReplace: varReplace, | ||
cleanUpCode: cleanUpCode, | ||
nonEmptySchema: nonEmptySchema, | ||
@@ -126,4 +127,14 @@ stableStringify: require('json-stable-stringify') | ||
var EMPTY_ELSE = /else\s*{\s*}/g | ||
, EMPTY_IF_NO_ELSE = /if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g | ||
, EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g; | ||
function cleanUpCode(str) { | ||
return str.replace(EMPTY_ELSE, '') | ||
.replace(EMPTY_IF_NO_ELSE, '') | ||
.replace(EMPTY_IF_WITH_ELSE, 'if (!($1))'); | ||
} | ||
function nonEmptySchema(schema, rules) { | ||
for (var key in schema) if (rules[key]) return true; | ||
} |
@@ -24,9 +24,7 @@ {{# def.definitions }} | ||
validate = function (data, dataPath, recursive) { | ||
if (!recursive) validate.errors.length = 0; | ||
var errors{{=$lvl}} = validate.errors.length; | ||
validate = function (data, dataPath) { | ||
validate.errors = null; | ||
var errors = 0; | ||
{{??}} | ||
{{? it.opts._debug }} | ||
console.log('validate dataPath:', dataPath); | ||
{{?}} | ||
{{? it.opts._debug }} console.log('validate dataPath:', dataPath); {{?}} | ||
@@ -40,6 +38,6 @@ {{ | ||
}} | ||
var errors{{=$lvl}} = validate.errors.length; | ||
{{?}} | ||
var errs_{{=$lvl}} = errors; | ||
{{ | ||
@@ -77,4 +75,5 @@ var $valid = 'valid' + $lvl | ||
{{ var $typeChecked = true; }} | ||
else | ||
else { | ||
{{# def.error:'type' }} | ||
} | ||
{{?}} | ||
@@ -84,3 +83,3 @@ {{?}} | ||
{{? $breakOnErrors }} | ||
if (errors{{=$lvl}} === validate.errors.length) { | ||
if (errs_{{=$lvl}} === errors) { | ||
{{ $closingBraces2 += '}'; }} | ||
@@ -98,3 +97,5 @@ {{?}} | ||
if (!({{= it.util[$method]($typeSchema, $data) }})) {{# def.error:'type' }} | ||
if (!({{= it.util[$method]($typeSchema, $data) }})) { | ||
{{# def.error:'type' }} | ||
} | ||
{{?}} | ||
@@ -105,6 +106,6 @@ | ||
{{? $root }} | ||
return errors{{=$lvl}} === validate.errors.length; | ||
return errs_{{=$lvl}} === errors; | ||
} | ||
{{??}} | ||
{{=$valid}} = errors{{=$lvl}} === validate.errors.length; | ||
{{=$valid}} = errs_{{=$lvl}} === errors; | ||
{{?}} | ||
@@ -111,0 +112,0 @@ |
{ | ||
"name": "ajv", | ||
"version": "0.3.12", | ||
"version": "0.4.0", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -5,2 +5,3 @@ # ajv - Another JSON Schema Validator | ||
[![Build Status](https://travis-ci.org/epoberezkin/ajv.svg?branch=master)](https://travis-ci.org/epoberezkin/ajv) | ||
@@ -76,3 +77,3 @@ ## JSON Schema standard | ||
Validating function returns boolean and has properties `errors` with the errors from the last validation and `schema` with the reference to the original schema. | ||
Validating function returns boolean and has properties `errors` with the errors from the last validation (`null` if there were no errors) and `schema` with the reference to the original schema. | ||
@@ -86,3 +87,3 @@ | ||
Validation errors will be available in the `errors` property of ajv instance. | ||
Validation errors will be available in the `errors` property of ajv instance (`null` if there were no errors). | ||
@@ -126,1 +127,7 @@ | ||
``` | ||
## Changes history | ||
##### 0.4.0 | ||
To improve performance, errors are set to `null` if there are no errors (previously empty array). |
@@ -81,5 +81,5 @@ 'use strict'; | ||
var valid = validate(test.data); | ||
// console.log('result', valid, validate.errors, ajv._refs); | ||
// console.log('result', valid, test.valid, validate.errors); | ||
assert.equal(valid, test.valid); | ||
if (valid) assert(validate.errors.length == 0); | ||
if (valid) assert(validate.errors === null); | ||
else assert(validate.errors.length > 0); | ||
@@ -90,3 +90,3 @@ | ||
assert.equal(valid, test.valid); | ||
if (valid) assert(fullValidate.errors.length == 0); | ||
if (valid) assert(fullValidate.errors === null); | ||
else assert(fullValidate.errors.length > 0); | ||
@@ -93,0 +93,0 @@ }); |
244900
114
7300
130