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, validate: validateGenerator,
copy: copy, copy: copy,
toHash: toHash, toHash: toHash,
property: property,
resolveRef: resolveRef, resolveRef: resolveRef,
checkDataType: checkDataType, checkDataType: checkDataType,
checkDataTypes: checkDataTypes, checkDataTypes: checkDataTypes,
@ -33,7 +34,7 @@ function compile(schema) {
}); });
// console.log('\n\n\n *** \n', validateCode); // console.log('\n\n\n *** \n', validateCode);
var validate; var validate;
eval('validate = ' + validateCode); eval(validateCode);
validate.schema = schema; validate.schema = schema;
validate.errors = []; 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) { function checkDataType(dataType, lvl) {
var data = 'data' + lvl; var data = 'data' + lvl;
switch (dataType) { switch (dataType) {
@ -131,5 +140,5 @@ function toHash(arr, func) {
function escapeQuotes(str) { 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.schemaPath = $schemaPath + '[' + $i + ']';
}} }}
{{ $it.inline = true; }}
{{= it.validate($it) }} {{= it.validate($it) }}
{{=$valid}} = {{=$valid}} && valid{{=$it.level}}; {{=$valid}} = {{=$valid}} && valid{{=$it.level}};

View File

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

View File

@ -6,7 +6,8 @@
, $schemaPath = it.schemaPath + '.' + keyword; , $schemaPath = it.schemaPath + '.' + keyword;
var $data = 'data' + $dataLvl 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: {{## def.elseIfValid:
{{? $breakOnError }} {{? $breakOnError }}
{{ $closingBraces += '}'; }} {{ $closingBraces += '}'; }}
@ -62,9 +71,7 @@
{{## def.checkError:rule: {{## def.checkError:rule:
if (!{{=$valid}}) { if (!{{=$valid}}) {{# def.error:rule }}
{{# def.error:rule }}
}
#}} #}}
@ -89,7 +96,7 @@
pattern: "'should match pattern \"{{=$schema}}\"'", pattern: "'should match pattern \"{{=$schema}}\"'",
required: "'properties {{=$schema.slice(0,7).join(\", \") }}{{? $schema.length > 7}}...{{?}} are required'", required: "'properties {{=$schema.slice(0,7).join(\", \") }}{{? $schema.length > 7}}...{{?}} are required'",
type: "'should be {{? $isArray }}{{= $schema.join(\",\") }}{{??}}{{=$schema}}{{?}}'", 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}}; var {{=$valid}};
{{ for ($property in $propertyDeps) { }} {{ for ($property in $propertyDeps) { }}
if ({{=$data}}.hasOwnProperty('{{= $property }}')) { if ({{=$data}}['{{= $property }}'] !== undefined) {
{{ $deps = $propertyDeps[$property]; }} {{ $deps = $propertyDeps[$property]; }}
{{=$valid}} = {{~ $deps:$dep:$i }}{{?$i}} && {{?}}{{=$data}}.hasOwnProperty('{{= $dep}}'){{~}}; {{=$valid}} = {{~ $deps:$dep:$i }}{{?$i}} && {{?}}{{=$data}}['{{= $dep}}'] !== undefined{{~}};
{{# def.checkError:'dependencies' }} {{# def.checkError:'dependencies' }}
{{# def.elseIfValid }} {{# def.elseIfValid }}
} }
@ -31,24 +31,23 @@ var {{=$valid}};
{{ for ($property in $schemaDeps) { }} {{ for ($property in $schemaDeps) { }}
{{ var $sch = $schemaDeps[$property]; }} {{ var $sch = $schemaDeps[$property]; }}
{{? Object.keys($sch).length }} {{? Object.keys($sch).length }}
if ({{=$data}}.hasOwnProperty('{{= $property }}')) { valid{{=$it.level}} = true;
if ({{=$data}}['{{= $property }}'] !== undefined) {
{{ {{
$it.schema = $sch; $it.schema = $sch;
$it.schemaPath = $schemaPath + '[\'' + it.escapeQuotes($property) + '\']'; $it.schemaPath = $schemaPath + "['" + it.escapeQuotes($property) + "']";
}} }}
{{ $it.inline = true; }}
{{= it.validate($it) }} {{= it.validate($it) }}
{{=$valid}} = valid{{=$it.level}};
} }
{{# def.ifValid }} {{# def.ifResultValid }}
{{?}} {{?}}
{{ } }} {{ } }}
{{? $breakOnError }}{{= $closingBraces }}{{?}} {{? $breakOnError }}{{= $closingBraces }}{{?}}
{{=$valid}} = errs{{=$lvl}} == validate.errors.length; {{=$valid}} = {{=$errs}} == validate.errors.length;
{{# def.cleanUp }} {{# def.cleanUp }}

View File

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

View File

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

View File

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

View File

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

View File

@ -9,8 +9,8 @@
{{??}} {{??}}
var schema{{=$lvl}} = validate.schema{{=$schemaPath}}; var schema{{=$lvl}} = validate.schema{{=$schemaPath}};
for (var i{{=$lvl}} = 0; i{{=$lvl}} < schema{{=$lvl}}.length; i{{=$lvl}}++) { for (var i = 0; i < schema{{=$lvl}}.length; i++) {
{{=$valid}} = data[schema{{=$lvl}}[i{{=$lvl}}]] !== undefined; {{=$valid}} = data[schema{{=$lvl}}[i]] !== undefined;
if (!{{=$valid}}) break; if (!{{=$valid}}) break;
} }
{{?}} {{?}}

View File

@ -5,11 +5,11 @@ var {{=$valid}} = true;
{{? $schema && it.opts.uniqueItems !== false }} {{? $schema && it.opts.uniqueItems !== false }}
if ({{=$data}}.length > 1) { if ({{=$data}}.length > 1) {
var i{{=$lvl}} = {{=$data}}.length, j{{=$lvl}}; var i = {{=$data}}.length, j;
outer: outer:
for (;i{{=$lvl}}--;) { for (;i--;) {
for (j{{=$lvl}} = i{{=$lvl}}; j{{=$lvl}}--;) { for (j = i; j--;) {
if (equal({{=$data}}[i{{=$lvl}}], {{=$data}}[j{{=$lvl}}])) { if (equal({{=$data}}[i], {{=$data}}[j])) {
{{=$valid}} = false; {{=$valid}} = false;
break outer; break outer;
} }

View File

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

View File

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