optimize & tidy up

master
Evgeny Poberezkin 2015-06-06 02:07:50 +01:00
parent 38ff8565c8
commit d0089f82e5
13 changed files with 94 additions and 85 deletions

View File

@ -24,6 +24,7 @@ function compile(schema) {
validate: validateGenerator,
copy: copy,
toHash: toHash,
property: property,
resolveRef: resolveRef,
checkDataType: checkDataType,
checkDataTypes: checkDataTypes,
@ -33,7 +34,7 @@ function compile(schema) {
});
// console.log('\n\n\n *** \n', validateCode);
var validate;
eval('validate = ' + validateCode);
eval(validateCode);
validate.schema = schema;
validate.errors = [];
@ -64,6 +65,14 @@ function copy(o, to) {
}
var IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
function property(key) {
return IDENTIFIER.test(key)
? '.' + key
: "['" + key + "']";
}
function checkDataType(dataType, lvl) {
var data = 'data' + lvl;
switch (dataType) {
@ -131,5 +140,5 @@ function toHash(arr, func) {
function escapeQuotes(str) {
return str.replace(/"/g, '\\"');
return str.replace(/'/g, "\\'");
}

View File

@ -12,7 +12,6 @@ var {{=$valid}} = true;
$it.schemaPath = $schemaPath + '[' + $i + ']';
}}
{{ $it.inline = true; }}
{{= it.validate($it) }}
{{=$valid}} = {{=$valid}} && valid{{=$it.level}};

View File

@ -2,7 +2,7 @@
{{# def.setup:'anyOf' }}
{{# def.setupNextLevel }}
var errs{{=$lvl}} = validate.errors.length;
var {{=$errs}} = validate.errors.length;
var {{=$valid}} = false;
{{~ $schema:$sch:$i }}
@ -16,7 +16,6 @@ var {{=$valid}} = false;
$it.schemaPath = $schemaPath + '[' + $i + ']';
}}
{{ $it.inline = true; }}
{{= it.validate($it) }}
{{=$valid}} = {{=$valid}} || valid{{=$it.level}};
@ -24,4 +23,4 @@ var {{=$valid}} = false;
{{= $closingBraces }}
if ({{=$valid}}) validate.errors.length = errs{{=$lvl}};
if ({{=$valid}}) validate.errors.length = {{=$errs}};

View File

@ -6,7 +6,8 @@
, $schemaPath = it.schemaPath + '.' + keyword;
var $data = 'data' + $dataLvl
, $valid = 'valid' + $lvl;
, $valid = 'valid' + $lvl
, $errs = 'errs' + $lvl;
}}
#}}
@ -29,6 +30,14 @@
#}}
{{## def.ifResultValid:
{{? $breakOnError }}
if (valid{{=$it.level}}) {
{{ $closingBraces += '}'; }}
{{?}}
#}}
{{## def.elseIfValid:
{{? $breakOnError }}
{{ $closingBraces += '}'; }}
@ -62,9 +71,7 @@
{{## def.checkError:rule:
if (!{{=$valid}}) {
{{# def.error:rule }}
}
if (!{{=$valid}}) {{# def.error:rule }}
#}}
@ -89,7 +96,7 @@
pattern: "'should match pattern \"{{=$schema}}\"'",
required: "'properties {{=$schema.slice(0,7).join(\", \") }}{{? $schema.length > 7}}...{{?}} are required'",
type: "'should be {{? $isArray }}{{= $schema.join(\",\") }}{{??}}{{=$schema}}{{?}}'",
uniqueItems: "'items ## ' + j{{=$lvl}} + ' and ' + i{{=$lvl}} + ' are duplicate'"
uniqueItems: "'items ## ' + j + ' and ' + i + ' are duplicate'"
} #}}

View File

@ -14,14 +14,14 @@
}
}}
var errs{{=$lvl}} = validate.errors.length;
var {{=$errs}} = validate.errors.length;
var {{=$valid}};
{{ 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' }}
{{# def.elseIfValid }}
}
@ -31,24 +31,23 @@ var {{=$valid}};
{{ for ($property in $schemaDeps) { }}
{{ var $sch = $schemaDeps[$property]; }}
{{? 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 }}
{{?}}
{{ } }}
{{? $breakOnError }}{{= $closingBraces }}{{?}}
{{=$valid}} = errs{{=$lvl}} == validate.errors.length;
{{=$valid}} = {{=$errs}} == validate.errors.length;
{{# def.cleanUp }}

View File

@ -4,23 +4,21 @@
{{## 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;
{{?}}
}
#}}
{{ var $dataNxt = $it.dataLevel = it.dataLevel + 1; }}
var errs{{=$lvl}} = validate.errors.length;
var {{=$errs}} = validate.errors.length;
var {{=$valid}};
{{? Array.isArray($schema) }}
@ -34,7 +32,7 @@ var {{=$valid}};
{{~ $schema:$sch:$i }}
{{? Object.keys($sch).length }}
{{=$valid}} = true;
valid{{=$it.level}} = true;
if ({{=$data}}.length > {{=$i}}) {
{{
@ -45,12 +43,10 @@ var {{=$valid}};
var data{{=$dataNxt}} = {{=$data}}[{{= $i }}]
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + '[{{=$i}}]';
{{ $it.inline = true; }}
{{= it.validate($it) }}
{{=$valid}} = valid{{=$it.level}};
}
{{# def.ifValid }}
{{# def.ifResultValid }}
{{?}}
{{~}}
@ -59,12 +55,13 @@ var {{=$valid}};
$it.schema = $additionalItems;
$it.schemaPath = it.schemaPath + '.additionalItems';
}}
valid{{=$it.level}} = true;
if ({{=$data}}.length > {{= $schema.length }}) {
{{# def.validateItems: $schema.length }}
}
{{# def.ifValid }}
{{# def.ifResultValid }}
{{?}}
{{?? Object.keys($schema).length }}
@ -74,11 +71,11 @@ var {{=$valid}};
$it.schemaPath = $schemaPath;
}}
{{# def.validateItems: 0 }}
{{# def.ifValid }}
{{# def.ifResultValid }}
{{?}}
{{? $breakOnError }} {{= $closingBraces }} {{?}}
{{=$valid}} = errs{{=$lvl}} == validate.errors.length;
{{=$valid}} = {{=$errs}} == validate.errors.length;
{{# def.cleanUp }}

View File

@ -5,15 +5,14 @@
{{
$it.schema = $schema;
$it.schemaPath = $schemaPath;
$it.inline = true;
}}
var errs{{=$lvl}} = validate.errors.length;
var {{=$errs}} = validate.errors.length;
{{= it.validate($it) }}
var {{=$valid}} = valid{{=$it.level}};
{{=$valid}} = !{{=$valid}};
if ({{=$valid}}) validate.errors.length = errs{{=$lvl}};
if ({{=$valid}}) validate.errors.length = {{=$errs}};
else {{# def.error:'not' }}

View File

@ -3,7 +3,7 @@
{{# def.setupNextLevel }}
var errs{{=$lvl}} = validate.errors.length;
var {{=$errs}} = validate.errors.length;
var validCount{{=$lvl}} = 0;
{{~ $schema:$sch:$i }}
@ -15,7 +15,6 @@ var validCount{{=$lvl}} = 0;
{{
$it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']';
$it.inline = true;
}}
{{= it.validate($it) }}
@ -28,5 +27,5 @@ var validCount{{=$lvl}} = 0;
var {{=$valid}} = validCount{{=$lvl}} == 1;
if ({{=$valid}}) validate.errors.length = errs{{=$lvl}};
if ({{=$valid}}) validate.errors.length = {{=$errs}};
else {{# def.error:'oneOf' }}

View File

@ -3,15 +3,6 @@
{{# def.setupNextLevel }}
{{## def.validateProperty:useKey:
var data{{=$dataNxt}} = {{=$data}}[{{= useKey }}]
, dataPath{{=$dataNxt}} = dataPath{{=$dataLvl}} + '.' + {{= useKey }};
{{ $it.inline = true; }}
{{= it.validate($it) }};
{{=$valid}} = valid{{=$it.level}};
#}}
{{
var $dataNxt = $it.dataLevel = it.dataLevel + 1;
@ -25,15 +16,15 @@
}}
var errs{{=$lvl}} = validate.errors.length;
var {{=$valid}} = true;
var {{=$errs}} = validate.errors.length;
var valid{{=$it.level}} = true;
{{? $checkAdditional }}
var propertiesSchema{{=$lvl}} = validate.schema{{=$schemaPath}} || {};
{{?}}
{{? $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' }}
{{# def.elseIfValid }}
{{?}}
@ -49,7 +40,7 @@ var {{=$valid}} = true;
{{? $checkAdditional }}
for (var key{{=$lvl}} in {{=$data}}) {
var isAdditional{{=$lvl}} = !propertiesSchema{{=$lvl}}.hasOwnProperty(key{{=$lvl}});
var isAdditional{{=$lvl}} = propertiesSchema{{=$lvl}}[key{{=$lvl}}] === undefined;
{{? $pPropertyKeys.length }}
if (isAdditional{{=$lvl}}) {
@ -65,7 +56,7 @@ var {{=$valid}} = true;
if (isAdditional{{=$lvl}}) {
{{? $noAdditional }}
{{=$valid}} = false;
valid{{=$it.level}} = false;
{{# def.error:'additionalProperties' }}
{{? $breakOnError }} break; {{?}}
{{??}}
@ -74,14 +65,17 @@ var {{=$valid}} = true;
$it.schemaPath = it.schemaPath + '.additionalProperties';
}}
{{ 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 }}
{{?}}
{{? $schema }}
@ -91,18 +85,22 @@ var {{=$valid}} = true;
{{? Object.keys($sch).length }}
{{
$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 }}
{{ } }}
{{?}}
@ -115,22 +113,26 @@ var {{=$valid}} = true;
$it.schemaPath = it.schemaPath + '.patternProperties.' + $propertyKey;
}}
valid{{=$it.level}} = true;
for (var key{{=$lvl}} in {{=$data}}) {
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 }}
{{?}}
{{~}}
{{? $breakOnError }}{{= $closingBraces }}{{?}}
{{=$valid}} = errs{{=$lvl}} == validate.errors.length;
var {{=$valid}} = {{=$errs}} == validate.errors.length;
{{# def.cleanUp }}

View File

@ -9,8 +9,8 @@
{{??}}
var schema{{=$lvl}} = validate.schema{{=$schemaPath}};
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;
}
{{?}}

View File

@ -5,11 +5,11 @@ var {{=$valid}} = true;
{{? $schema && it.opts.uniqueItems !== false }}
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;
break outer;
}

View File

@ -13,14 +13,13 @@
{{
var $root = it.isRoot;
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;
{{??}}
{{
var $lvl = it.level
@ -67,7 +66,7 @@ var {{=$valid}} = true;
{{? $breakOnErrors }} {{= $closingBraces2 }} {{?}}
{{? $root }}
return errs{{=$lvl}} == validate.errors.length;
return validate.errors.length === 0;
}
{{??}}
{{=$valid}} = errs{{=$lvl}} == validate.errors.length;
@ -82,7 +81,7 @@ var {{=$valid}} = true;
}
function $shouldUseRule($rule) {
var $use = it.schema.hasOwnProperty($rule.keyword);
var $use = it.schema[$rule.keyword];
if (!$use && $rule.keyword == 'properties') {
var $pProperties = it.schema.patternProperties
, $aProperties = it.schema.additionalProperties;

View File

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