precompiled dot templates, dot is devDependency, closes #3

master
Evgeny Poberezkin 2015-06-17 14:41:52 +01:00
parent 5b62a32c33
commit 5f36f17f63
56 changed files with 1441 additions and 17 deletions

View File

@ -1,6 +1,9 @@
# ajv - Another JSON Schema Validator
One of the fastest JSON Schema validators for node.js. It uses [doT templates](https://github.com/olado/doT) to generate super-fast validating functions.
One of the fastest JSON Schema validators for node.js.
It uses precompiled [doT templates](https://github.com/olado/doT) to generate super-fast validating functions.
[![Build Status](https://travis-ci.org/epoberezkin/ajv.svg?branch=master)](https://travis-ci.org/epoberezkin/ajv)
[![npm version](https://badge.fury.io/js/ajv.svg)](http://badge.fury.io/js/ajv)
@ -150,6 +153,18 @@ git submodule update --init
npm test
```
## Contributing
All validation functions are generated using doT templates in dot folder. Templates are precompiled so doT is not a run-time dependency.
`bin/compile_dots` to compile templates to dotjs folder
`bin/watch_dots` to automatically compile templates when files in dot folder change
There is pre-commit hook that runs compile_dots and tests.
## Changes history
##### 0.5.0

3
bin/compile_dots Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
node ./bin/compile_dots.js

22
bin/compile_dots.js Normal file
View File

@ -0,0 +1,22 @@
//compile doT templates to js functions
var glob = require('glob')
, fs = require('fs')
, path = require('path')
, doT = require('dot')
, beautify = require('js-beautify').js_beautify;
var defs = fs.readFileSync(path.join(__dirname, '../lib/dot/definitions.def'));
var files = glob.sync('../lib/dot/*.jst', { cwd: __dirname });
files.forEach(function (f) {
var template = fs.readFileSync(path.join(__dirname, f));
var code = doT.compile(template, { definitions: defs });
code = "'use strict';\nmodule.exports = " + code.toString();
code = code.replace(/out\s*\+=\s*'\s*';/g, '');
code = beautify(code, { indent_size: 2 }) + '\n';
var targetFile = f.replace('../lib/dot', '').replace('.jst', '.js')
, targetPath = path.join(__dirname, '../lib/dotjs', targetFile);
fs.writeFileSync(targetPath, code);
console.log('compiled', targetFile);
});

2
bin/watch_dots Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
./node_modules/.bin/watch 'node bin/compile_dots.js' ./lib/dot

View File

@ -1,16 +1,13 @@
'use strict';
var doT = require('dot')
, fs = require('fs')
, resolve = require('./resolve')
var resolve = require('./resolve')
, util = require('./util')
, equal = require('./equal');
try { var beautify = require('js-beautify').js_beautify; } catch(e) {}
var RULES = require('./rules')
, validateTemplate = fs.readFileSync(__dirname + '/validate.dot.js')
, validateGenerator = doT.compile(validateTemplate, { definitions: RULES.defs });
, validateGenerator = require('../dotjs/validate');
module.exports = compile;

View File

@ -1,10 +1,6 @@
'use strict';
var fs = require('fs')
, doT = require('dot')
, util = require('../util');
var defs = fs.readFileSync(__dirname + '/definitions.def.js');
var util = require('./util');
var RULES = module.exports = [
{ type: 'number',
@ -18,16 +14,14 @@ var RULES = module.exports = [
{ rules: [ '$ref', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] }
];
RULES.defs = defs;
RULES.all = [ 'type', 'additionalProperties', 'patternProperties' ];
RULES.forEach(function (group) {
group.rules = group.rules.map(function (keyword) {
RULES.all.push(keyword);
var template = fs.readFileSync(__dirname + '/' + keyword + '.dot.js');
return {
keyword: keyword,
code: doT.compile(template, { definitions: defs })
code: require('../dotjs/' + keyword)
};
});
});

60
lib/dotjs/$ref.js Normal file
View File

@ -0,0 +1,60 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['$ref'],
$schemaPath = it.schemaPath + '.' + '$ref',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('$ref') + '\'); ';
}
if ($schema == '#' || $schema == '#/') {
if (it.isRoot) {
if ($breakOnError && it.wasTop) {
out += ' if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) return false; else { ';
} else {
out += ' 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; } ';
if ($breakOnError) {
out += ' else { ';
}
}
} else {
var $v = 'v' + $lvl;
out += ' var ' + ($v) + ' = root.refVal[0]; if (! ' + ($v) + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (validate.errors === null) validate.errors = ' + ($v) + '.errors; else validate.errors = validate.errors.concat(' + ($v) + '.errors); errors = validate.errors.length; } ';
if ($breakOnError) {
out += ' else { ';
}
}
} else {
var $refVal = it.resolveRef(it.baseId, $schema, it.rootId);
if ($refVal === undefined) {
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
if ($breakOnError) {
out += ' if (false) { ';
}
} else {
var $v = 'v' + $lvl;
out += ' var ' + ($v) + ' = ' + ($refVal) + '; if (! ' + ($v) + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (validate.errors === null) validate.errors = ' + ($v) + '.errors; else validate.errors = validate.errors.concat(' + ($v) + '.errors); errors = validate.errors.length; } ';
if ($breakOnError) {
out += ' else { ';
}
}
}
return out;
}

3
lib/dotjs/README.md Normal file
View File

@ -0,0 +1,3 @@
These files are compiled dot templates from dot folder.
Do NOT edit them directly, edit the templates and run `node bin/compile_dots` from main ajv folder.

40
lib/dotjs/allOf.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['allOf'],
$schemaPath = it.schemaPath + '.' + 'allOf',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('allOf') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
var arr1 = $schema;
if (arr1) {
var $sch, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$sch = arr1[$i += 1];
if (it.util.schemaHasRules($sch, it.RULES.all)) {
$it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']';
out += ' ' + (it.validate($it)) + ' ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
}
}
if ($breakOnError) {
out += ' ' + ($closingBraces.slice(0, -1));
}
out = it.util.cleanUpCode(out);
return out;
}

46
lib/dotjs/anyOf.js Normal file
View File

@ -0,0 +1,46 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['anyOf'],
$schemaPath = it.schemaPath + '.' + 'anyOf',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('anyOf') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
var $noEmptySchema = $schema.every(function($sch) {
return it.util.schemaHasRules($sch, it.RULES.all);
});
if ($noEmptySchema) {
out += ' var ' + ($errs) + ' = errors; var ' + ($valid) + ' = false; ';
var arr1 = $schema;
if (arr1) {
var $sch, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$sch = arr1[$i += 1];
$it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']';
out += ' ' + (it.validate($it)) + ' ' + ($valid) + ' = ' + ($valid) + ' || valid' + ($it.level) + '; if (!' + ($valid) + ') { ';
$closingBraces += '}';
}
}
out += ' ' + ($closingBraces) + ' if (' + ($valid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; } ';
if (it.opts.allErrors) {
out += ' } ';
}
out = it.util.cleanUpCode(out);
} else {
if ($breakOnError) {
out += ' if (true) { ';
}
}
return out;
}

93
lib/dotjs/dependencies.js Normal file
View File

@ -0,0 +1,93 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['dependencies'],
$schemaPath = it.schemaPath + '.' + 'dependencies',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('dependencies') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
var $schemaDeps = {},
$propertyDeps = {};
for ($property in $schema) {
var $sch = $schema[$property];
var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps;
$deps[$property] = $sch;
}
out += 'var ' + ($errs) + ' = errors;';
for (var $property in $propertyDeps) {
out += ' if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined) { ';
$deps = $propertyDeps[$property];
out += ' if ( ';
var arr1 = $deps;
if (arr1) {
var $dep, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$dep = arr1[$i += 1];
if ($i) {
out += ' || ';
}
out += ' ' + ($data) + (it.util.getProperty($dep)) + ' === undefined ';
}
}
out += ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('dependencies') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'';
if ($deps.length == 1) {
out += 'property ' + ($deps[0]) + ' is';
} else {
out += 'properties ' + ($deps.join(", ")) + ' are';
}
out += ' required when property ' + ($property) + ' is present\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('dependencies') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'';
if ($deps.length == 1) {
out += 'property ' + ($deps[0]) + ' is';
} else {
out += 'properties ' + ($deps.join(", ")) + ' are';
}
out += ' required when property ' + ($property) + ' is present\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
if ($breakOnError) {
$closingBraces += '}';
out += ' else { ';
}
out += ' }';
}
for (var $property in $schemaDeps) {
var $sch = $schemaDeps[$property];
if (it.util.schemaHasRules($sch, it.RULES.all)) {
out += ' valid' + ($it.level) + ' = true; if (' + ($data) + '[\'' + ($property) + '\'] !== undefined) { ';
$it.schema = $sch;
$it.schemaPath = $schemaPath + "['" + it.util.escapeQuotes($property) + "']";
out += ' ' + (it.validate($it)) + ' } ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
}
if ($breakOnError) {
out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
}
out = it.util.cleanUpCode(out);
return out;
}

35
lib/dotjs/enum.js Normal file
View File

@ -0,0 +1,35 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['enum'],
$schemaPath = it.schemaPath + '.' + 'enum',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('enum') + '\'); ';
}
var $i = 'i' + $lvl;
out += 'var enumSchema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ' , ' + ($valid) + ' = false;for (var ' + ($i) + '=0; ' + ($i) + '<enumSchema' + ($lvl) + '.length; ' + ($i) + '++) if (equal(' + ($data) + ', enumSchema' + ($lvl) + '[' + ($i) + '])) { ' + ($valid) + ' = true; break; } if (!' + ($valid) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('enum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be equal to one of values\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('enum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be equal to one of values\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' }';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

47
lib/dotjs/format.js Normal file
View File

@ -0,0 +1,47 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['format'],
$schemaPath = it.schemaPath + '.' + 'format',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('format') + '\'); ';
}
var $format = it.formats[$schema];
if (it.opts.format !== false && $format) {
out += ' if (! ';
if (typeof $format == 'function') {
out += ' formats' + (it.util.getProperty($schema)) + ' (' + ($data) + ') ';
} else {
out += ' formats' + (it.util.getProperty($schema)) + ' .test(' + ($data) + ') ';
}
out += ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('format') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match format ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('format') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match format ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
if ($breakOnError) {
out += ' else { ';
}
} else {
if ($breakOnError) {
out += ' if (true) { ';
}
}
return out;
}

120
lib/dotjs/items.js Normal file
View File

@ -0,0 +1,120 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['items'],
$schemaPath = it.schemaPath + '.' + 'items',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('items') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
var $dataNxt = $it.dataLevel = it.dataLevel + 1,
$nextData = 'data' + $dataNxt;
out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
if (Array.isArray($schema)) {
var $additionalItems = it.schema.additionalItems;
if ($additionalItems === false) {
out += ' ' + ($valid) + ' = ' + ($data) + '.length <= ' + ($schema.length) + '; if (!' + ($valid) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('additionalItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: false, data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('additionalItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: false, data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
if ($breakOnError) {
$closingBraces += '}';
out += ' else { ';
}
}
var arr1 = $schema;
if (arr1) {
var $sch, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$sch = arr1[$i += 1];
if (it.util.schemaHasRules($sch, it.RULES.all)) {
out += ' valid' + ($it.level) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';
$it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']';
$it.errorPath = (it.errorPath + ' + "[' + $i + ']"').replace('" + "', '');
$it.dataPath = it.dataPath + '[' + $i + ']';
var $passData = $data + '[' + $i + ']';
var $code = it.validate($it);
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
out += ' } ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
}
}
if (typeof $additionalItems == 'object' && it.util.schemaHasRules($additionalItems, it.RULES.all)) {
$it.schema = $additionalItems;
$it.schemaPath = it.schemaPath + '.additionalItems';
out += ' valid' + ($it.level) + ' = true; if (' + ($data) + '.length > ' + ($schema.length) + ') { for (var i' + ($lvl) + ' = ' + ($schema.length) + '; i' + ($lvl) + ' < ' + ($data) + '.length; i' + ($lvl) + '++) { ';
$it.errorPath = (it.errorPath + ' + "[" + i' + $lvl + ' + "]"').replace('" + "', '');
$it.dataPath = it.dataPath + '[i' + $lvl + ']';
var $passData = $data + '[i' + $lvl + ']';
var $code = it.validate($it);
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
if ($breakOnError) {
out += ' if (!valid' + ($it.level) + ') break; ';
}
out += ' } } ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
} else if (it.util.schemaHasRules($schema, it.RULES.all)) {
$it.schema = $schema;
$it.schemaPath = $schemaPath;
out += ' for (var i' + ($lvl) + ' = ' + (0) + '; i' + ($lvl) + ' < ' + ($data) + '.length; i' + ($lvl) + '++) { ';
$it.errorPath = (it.errorPath + ' + "[" + i' + $lvl + ' + "]"').replace('" + "', '');
$it.dataPath = it.dataPath + '[i' + $lvl + ']';
var $passData = $data + '[i' + $lvl + ']';
var $code = it.validate($it);
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
if ($breakOnError) {
out += ' if (!valid' + ($it.level) + ') break; ';
}
out += ' } ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
if ($breakOnError) {
out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
}
out = it.util.cleanUpCode(out);
return out;
}

34
lib/dotjs/maxItems.js Normal file
View File

@ -0,0 +1,34 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['maxItems'],
$schemaPath = it.schemaPath + '.' + 'maxItems',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('maxItems') + '\'); ';
}
out += 'if (' + ($data) + '.length > ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('maxItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maxItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

40
lib/dotjs/maxLength.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['maxLength'],
$schemaPath = it.schemaPath + '.' + 'maxLength',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('maxLength') + '\'); ';
}
out += 'if ( ';
if (it.opts.unicode === false) {
out += ' ' + ($data) + '.length ';
} else {
out += ' ucs2length(' + ($data) + ') ';
}
out += ' > ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('maxLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be longer than ' + ($schema) + ' characters\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maxLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be longer than ' + ($schema) + ' characters\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

View File

@ -0,0 +1,34 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['maxProperties'],
$schemaPath = it.schemaPath + '.' + 'maxProperties',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('maxProperties') + '\'); ';
}
out += 'if (Object.keys(' + ($data) + ').length > ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('maxProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' properties\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maxProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' properties\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

37
lib/dotjs/maximum.js Normal file
View File

@ -0,0 +1,37 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['maximum'],
$schemaPath = it.schemaPath + '.' + 'maximum',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('maximum') + '\'); ';
}
var $exclusive = it.schema.exclusiveMaximum === true,
$op = $exclusive ? '<' : '<=',
$notOp = $exclusive ? '>=' : '>';
out += 'if (' + ($data) + ' ' + ($notOp) + ' ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('maximum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maximum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

34
lib/dotjs/minItems.js Normal file
View File

@ -0,0 +1,34 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['minItems'],
$schemaPath = it.schemaPath + '.' + 'minItems',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('minItems') + '\'); ';
}
out += 'if (' + ($data) + '.length < ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('minItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

40
lib/dotjs/minLength.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['minLength'],
$schemaPath = it.schemaPath + '.' + 'minLength',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('minLength') + '\'); ';
}
out += 'if ( ';
if (it.opts.unicode === false) {
out += ' ' + ($data) + '.length ';
} else {
out += ' ucs2length(' + ($data) + ') ';
}
out += ' < ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('minLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be shorter than ' + ($schema) + ' characters\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be shorter than ' + ($schema) + ' characters\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

View File

@ -0,0 +1,34 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['minProperties'],
$schemaPath = it.schemaPath + '.' + 'minProperties',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('minProperties') + '\'); ';
}
out += 'if (Object.keys(' + ($data) + ').length < ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('minProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' properties\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' properties\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

37
lib/dotjs/minimum.js Normal file
View File

@ -0,0 +1,37 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['minimum'],
$schemaPath = it.schemaPath + '.' + 'minimum',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('minimum') + '\'); ';
}
var $exclusive = it.schema.exclusiveMinimum === true,
$op = $exclusive ? '>' : '>=',
$notOp = $exclusive ? '<=' : '<';
out += 'if (' + ($data) + ' ' + ($notOp) + ' ' + ($schema) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('minimum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minimum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

34
lib/dotjs/multipleOf.js Normal file
View File

@ -0,0 +1,34 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['multipleOf'],
$schemaPath = it.schemaPath + '.' + 'multipleOf',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('multipleOf') + '\'); ';
}
out += 'var division' + ($lvl) + ' = ' + ($data) + ' / ' + ($schema) + ';if (' + ($data) + ' / ' + ($schema) + ' !== parseInt(division' + ($lvl) + ')) { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('multipleOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be multiple of ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('multipleOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be multiple of ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

58
lib/dotjs/not.js Normal file
View File

@ -0,0 +1,58 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['not'],
$schemaPath = it.schemaPath + '.' + 'not',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('not') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
if (it.util.schemaHasRules($schema, it.RULES.all)) {
$it.schema = $schema;
$it.schemaPath = $schemaPath;
out += ' var ' + ($errs) + ' = errors; ' + (it.validate($it)) + ' if (valid' + ($it.level) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } else { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; } ';
if (it.opts.allErrors) {
out += ' } ';
}
} else {
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
if ($breakOnError) {
out += ' if (false) { ';
}
}
return out;
}

58
lib/dotjs/oneOf.js Normal file
View File

@ -0,0 +1,58 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['oneOf'],
$schemaPath = it.schemaPath + '.' + 'oneOf',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('oneOf') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
out += 'var ' + ($errs) + ' = errors;var prevValid' + ($lvl) + ' = false;var ' + ($valid) + ' = false;';
var arr1 = $schema;
if (arr1) {
var $sch, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$sch = arr1[$i += 1];
if (it.util.schemaHasRules($sch, it.RULES.all)) {
$it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']';
out += ' ' + (it.validate($it)) + ' ';
} else {
out += ' var valid' + ($it.level) + ' = true; ';
}
if ($i) {
out += ' if (valid' + ($it.level) + ' && prevValid' + ($lvl) + ') ' + ($valid) + ' = false; else { ';
$closingBraces += '}';
}
out += ' if (valid' + ($it.level) + ') ' + ($valid) + ' = prevValid' + ($lvl) + ' = true;';
}
}
out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('oneOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match exactly one schema in oneOf\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('oneOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match exactly one schema in oneOf\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} else { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; }';
if (it.opts.allErrors) {
out += ' } ';
}
return out;
}

35
lib/dotjs/pattern.js Normal file
View File

@ -0,0 +1,35 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['pattern'],
$schemaPath = it.schemaPath + '.' + 'pattern',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('pattern') + '\'); ';
}
new RegExp($schema);
out += 'if (! /' + (it.util.escapeRegExp($schema)) + '/.test(' + ($data) + ') ) { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('pattern') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match pattern "' + ($schema) + '"\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('pattern') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match pattern "' + ($schema) + '"\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
}

163
lib/dotjs/properties.js Normal file
View File

@ -0,0 +1,163 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['properties'],
$schemaPath = it.schemaPath + '.' + 'properties',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('properties') + '\'); ';
}
var $it = it.util.copy(it),
$closingBraces = '';
$it.level++;
var $dataNxt = $it.dataLevel = it.dataLevel + 1,
$nextData = 'data' + $dataNxt;
var $pProperties = it.schema.patternProperties || {},
$pPropertyKeys = Object.keys($pProperties),
$aProperties = it.schema.additionalProperties,
$noAdditional = $aProperties === false,
$additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
$checkAdditional = $noAdditional || $additionalIsSchema;
out += 'var ' + ($errs) + ' = errors;var valid' + ($it.level) + ' = true;';
if ($checkAdditional) {
out += ' var propertiesSchema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ' || {};';
}
if ($checkAdditional) {
out += ' for (var key' + ($lvl) + ' in ' + ($data) + ') { var isAdditional' + ($lvl) + ' = propertiesSchema' + ($lvl) + '[key' + ($lvl) + '] === undefined; ';
if ($pPropertyKeys.length) {
out += ' if (isAdditional' + ($lvl) + ') { ';
var arr1 = $pPropertyKeys;
if (arr1) {
var $pProperty, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$pProperty = arr1[$i += 1];
out += ' if (/' + (it.util.escapeRegExp($pProperty)) + '/.test(key' + ($lvl) + ')) isAdditional' + ($lvl) + ' = false; ';
if ($i < $pPropertyKeys.length - 1) {
out += ' else ';
}
}
}
out += ' } ';
}
out += ' if (isAdditional' + ($lvl) + ') { ';
var $currentErrorPath = it.errorPath;
it.errorPath = (it.errorPath + ' + "[\'" + key' + $lvl + ' + "\']"').replace('" + "', '');
if ($noAdditional) {
out += ' valid' + ($it.level) + ' = false; ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('additionalProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'additional properties NOT allowed\' ';
if (it.opts.verbose) {
out += ', schema: false, data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('additionalProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'additional properties NOT allowed\' ';
if (it.opts.verbose) {
out += ', schema: false, data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
if ($breakOnError) {
out += ' break; ';
}
} else {
$it.schema = $aProperties;
$it.schemaPath = it.schemaPath + '.additionalProperties';
$it.errorPath = it.errorPath;
$it.dataPath = it.dataPath + '[key' + $lvl + ']';
var $passData = $data + '[key' + $lvl + ']';
var $code = it.validate($it);
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
if ($breakOnError) {
out += ' if (!valid' + ($it.level) + ') break; ';
}
}
it.errorPath = $currentErrorPath;
out += ' } } ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
if ($schema) {
for (var $propertyKey in $schema) {
var $sch = $schema[$propertyKey];
if (it.util.schemaHasRules($sch, it.RULES.all)) {
$it.schema = $sch;
$it.schemaPath = $schemaPath + "['" + it.util.escapeQuotes($propertyKey) + "']";
var $prop = it.util.getProperty($propertyKey),
$passData = $data + $prop;
$it.errorPath = (it.errorPath + ' + "' + $prop + '"').replace('" + "', '');
$it.dataPath = it.dataPath + $prop;
var $code = it.validate($it);
if (it.util.varOccurences($code, $nextData) < 2) {
$code = it.util.varReplace($code, $nextData, $passData);
var $useData = $passData;
} else {
var $useData = $nextData;
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
}
if ($breakOnError) {
out += ' if (' + ($useData) + ' === undefined) { valid' + ($it.level) + ' = true; } else { ';
} else {
out += ' if (' + ($useData) + ' !== undefined) { ';
}
out += ' ' + ($code) + ' } ';
}
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
}
var arr2 = $pPropertyKeys;
if (arr2) {
var $pProperty, i2 = -1,
l2 = arr2.length - 1;
while (i2 < l2) {
$pProperty = arr2[i2 += 1];
var $sch = $pProperties[$pProperty];
if (it.util.schemaHasRules($sch, it.RULES.all)) {
$it.schema = $sch;
$it.schemaPath = it.schemaPath + '.patternProperties.' + $pProperty;
out += ' for (var key' + ($lvl) + ' in ' + ($data) + ') { if (/' + (it.util.escapeRegExp($pProperty)) + '/.test(key' + ($lvl) + ')) { ';
$it.errorPath = (it.errorPath + ' + "[\'" + key' + $lvl + ' + "\']"').replace('" + "', '');
$it.dataPath = it.dataPath + '[key' + $lvl + ']';
var $passData = $data + '[key' + $lvl + ']';
var $code = it.validate($it);
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
if ($breakOnError) {
out += ' if (!valid' + ($it.level) + ') break; ';
}
out += ' } ';
if ($breakOnError) {
out += ' else valid' + ($it.level) + ' = true; ';
}
out += ' } ';
if ($breakOnError) {
out += ' if (valid' + ($it.level) + ') { ';
$closingBraces += '}';
}
}
}
}
if ($breakOnError) {
out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
}
out = it.util.cleanUpCode(out);
return out;
}

84
lib/dotjs/required.js Normal file
View File

@ -0,0 +1,84 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['required'],
$schemaPath = it.schemaPath + '.' + 'required',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('required') + '\'); ';
}
if ($schema.length <= 100) {
out += ' if ( ';
var arr1 = $schema;
if (arr1) {
var $property, $i = -1,
l1 = arr1.length - 1;
while ($i < l1) {
$property = arr1[$i += 1];
if ($i) {
out += ' || ';
}
out += ' ' + ($data) + (it.util.getProperty($property)) + ' === undefined ';
}
}
out += ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
if ($schema.length > 7) {
out += '...';
}
out += ' are required\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
if ($schema.length > 7) {
out += '...';
}
out += ' are required\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
if ($breakOnError) {
out += ' else { ';
}
} else {
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; for (var i = 0; i < schema' + ($lvl) + '.length; i++) { var ' + ($valid) + ' = data[schema' + ($lvl) + '[i]] !== undefined; if (!' + ($valid) + ') break; } if (!' + ($valid) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
if ($schema.length > 7) {
out += '...';
}
out += ' are required\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
if ($schema.length > 7) {
out += '...';
}
out += ' are required\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
if ($breakOnError) {
out += ' else { ';
}
}
return out;
}

40
lib/dotjs/uniqueItems.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
module.exports = function anonymous(it) {
var out = ' ';
var $lvl = it.level,
$dataLvl = it.dataLevel,
$schema = it.schema['uniqueItems'],
$schemaPath = it.schemaPath + '.' + 'uniqueItems',
$breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''),
$valid = 'valid' + $lvl,
$errs = 'errs' + $lvl;
if (it.opts._debug) {
out += ' console.log(\'Keyword ' + ('uniqueItems') + '\'); ';
}
if ($schema && it.opts.uniqueItems !== false) {
out += ' var ' + ($valid) + ' = true; if (' + ($data) + '.length > 1) { var i = ' + ($data) + '.length, j; outer: for (;i--;) { for (j = i; j--;) { if (equal(' + ($data) + '[i], ' + ($data) + '[j])) { ' + ($valid) + ' = false; break outer; } } } } if (!' + ($valid) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('uniqueItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'items ## \' + j + \' and \' + i + \' are duplicate\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('uniqueItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'items ## \' + j + \' and \' + i + \' are duplicate\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
if ($breakOnError) {
out += ' else { ';
}
} else {
if ($breakOnError) {
out += ' if (true) { ';
}
}
return out;
}

184
lib/dotjs/validate.js Normal file
View File

@ -0,0 +1,184 @@
'use strict';
module.exports = function anonymous(it) {
var out = '';
if (it.isTop) {
var $top = it.isTop,
$lvl = it.level = 0,
$dataLvl = it.dataLevel = 0,
$data = 'data';
it.rootId = it.baseId = it.resolve.fullPath(it.schema.id);
delete it.isTop;
it.wasTop = true;
out += ' validate = function (data, dataPath) { \'use strict\'; validate.errors = null;';
out += ' var errors = 0; ';
} else {
if (it.opts._debug) {
out += ' console.log(\'validate dataPath:\', dataPath); ';
}
var $lvl = it.level,
$dataLvl = it.dataLevel,
$data = 'data' + ($dataLvl || '');
if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
delete it.wasTop;
out += ' var errs_' + ($lvl) + ' = errors;';
}
var $valid = 'valid' + $lvl,
$breakOnError = !it.opts.allErrors,
$closingBraces1 = '',
$closingBraces2 = '';
var $typeSchema = it.schema.type;
var arr1 = it.RULES;
if (arr1) {
var $rulesGroup, i1 = -1,
l1 = arr1.length - 1;
while (i1 < l1) {
$rulesGroup = arr1[i1 += 1];
if ($shouldUseGroup($rulesGroup)) {
if ($rulesGroup.type) {
out += ' if (' + (it.util.checkDataType($rulesGroup.type, $data)) + ') { ';
}
var arr2 = $rulesGroup.rules;
if (arr2) {
var $rule, i2 = -1,
l2 = arr2.length - 1;
while (i2 < l2) {
$rule = arr2[i2 += 1];
if ($shouldUseRule($rule)) {
out += ' ' + ($rule.code(it)) + ' ';
if ($breakOnError) {
$closingBraces1 += '}';
}
}
}
}
if ($breakOnError) {
out += ' ' + ($closingBraces1) + ' ';
$closingBraces1 = '';
}
if ($rulesGroup.type) {
out += ' } ';
if ($typeSchema && $typeSchema === $rulesGroup.type) {
var $typeChecked = true;
out += ' else { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
if ($isArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' ';
if (it.opts.verbose) {
out += ', schema: ';
if ($isArray) {
out += 'validate.schema' + ($schemaPath);
} else {
out += '\'' + ($typeSchema) + '\'';
}
out += ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
if ($isArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' ';
if (it.opts.verbose) {
out += ', schema: ';
if ($isArray) {
out += 'validate.schema' + ($schemaPath);
} else {
out += '\'' + ($typeSchema) + '\'';
}
out += ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' } ';
}
}
if ($breakOnError) {
out += ' if (errors === ';
if ($top) {
out += '0';
} else {
out += 'errs_' + ($lvl);
}
out += ') { ';
$closingBraces2 += '}';
}
}
}
}
if ($typeSchema && !$typeChecked) {
var $schemaPath = it.schemaPath + '.type',
$isArray = Array.isArray($typeSchema),
$method = $isArray ? 'checkDataTypes' : 'checkDataType';
out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
if ($isArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' ';
if (it.opts.verbose) {
out += ', schema: ';
if ($isArray) {
out += 'validate.schema' + ($schemaPath);
} else {
out += '\'' + ($typeSchema) + '\'';
}
out += ', data: ' + ($data);
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
if ($isArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' ';
if (it.opts.verbose) {
out += ', schema: ';
if ($isArray) {
out += 'validate.schema' + ($schemaPath);
} else {
out += '\'' + ($typeSchema) + '\'';
}
out += ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; ';
}
out += ' }';
}
if ($breakOnError) {
out += ' ' + ($closingBraces2) + ' ';
}
if ($top) {
out += ' return errors === 0;';
out += ' }';
} else {
out += ' var ' + ($valid) + ' = errors === errs_' + ($lvl) + ';';
}
out = it.util.cleanUpCode(out);
if ($top && $breakOnError) {
out = it.util.cleanUpVarErrors(out);
}
function $shouldUseGroup($rulesGroup) {
return $rulesGroup.rules.some(function($rule) {
return $shouldUseRule($rule);
});
}
function $shouldUseRule($rule) {
return it.schema[$rule.keyword] !== undefined || ($rule.keyword == 'properties' && (it.schema.additionalProperties !== undefined || (it.schema.patternProperties && Object.keys(it.schema.patternProperties).length)));
}
return out;
}

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.5.0",
"version": "0.5.1",
"description": "Another JSON schema Validator",
"main": "lib/ajv.js",
"scripts": {
@ -22,13 +22,14 @@
},
"homepage": "https://github.com/epoberezkin/ajv",
"dependencies": {
"dot": "^1.0.3",
"json-stable-stringify": "^1.0.0"
},
"devDependencies": {
"chai": "^3.0.0",
"dot": "^1.0.3",
"glob": "^5.0.10",
"js-beautify": "^1.5.6",
"mocha": "^2.2.5"
"mocha": "^2.2.5",
"watch": "^0.16.0"
}
}