diff --git a/.gitignore b/.gitignore index 9312e881e..4b0dc57aa 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,9 @@ node_modules .DS_Store +# Compiled templates +lib/dotjs/*.js + # Browserified tests .browser diff --git a/lib/dotjs/README.md b/lib/dotjs/README.md index 5e0f4344e..4d994846c 100644 --- a/lib/dotjs/README.md +++ b/lib/dotjs/README.md @@ -1,3 +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. +Do NOT edit them directly, edit the templates and run `npm run build` from main ajv folder. diff --git a/lib/dotjs/allOf.js b/lib/dotjs/allOf.js deleted file mode 100644 index d9f16cd4c..000000000 --- a/lib/dotjs/allOf.js +++ /dev/null @@ -1,37 +0,0 @@ -'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; - 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; -} diff --git a/lib/dotjs/anyOf.js b/lib/dotjs/anyOf.js deleted file mode 100644 index 6ecfec9cc..000000000 --- a/lib/dotjs/anyOf.js +++ /dev/null @@ -1,47 +0,0 @@ -'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; - 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) + ') { var err = { keyword: \'' + ('anyOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match some schema in anyOf\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } '; - if (it.opts.allErrors) { - out += ' } '; - } - out = it.util.cleanUpCode(out); - } else { - if ($breakOnError) { - out += ' if (true) { '; - } - } - return out; -} diff --git a/lib/dotjs/dependencies.js b/lib/dotjs/dependencies.js deleted file mode 100644 index a7275cc1d..000000000 --- a/lib/dotjs/dependencies.js +++ /dev/null @@ -1,90 +0,0 @@ -'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; - 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 ' + (it.util.escapeQuotes($deps[0])) + ' is'; - } else { - out += 'properties ' + (it.util.escapeQuotes($deps.join(", "))) + ' are'; - } - out += ' required when property ' + (it.util.escapeQuotes($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 ' + (it.util.escapeQuotes($deps[0])) + ' is'; - } else { - out += 'properties ' + (it.util.escapeQuotes($deps.join(", "))) + ' are'; - } - out += ' required when property ' + (it.util.escapeQuotes($property)) + ' is present\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.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.getProperty($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; -} diff --git a/lib/dotjs/enum.js b/lib/dotjs/enum.js deleted file mode 100644 index 09646d6b8..000000000 --- a/lib/dotjs/enum.js +++ /dev/null @@ -1,32 +0,0 @@ -'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; - var $i = 'i' + $lvl; - out += 'var enumSchema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ' , ' + ($valid) + ' = false;for (var ' + ($i) + '=0; ' + ($i) + ' ' + ($i) + ') { '; - $it.schema = $sch; - $it.schemaPath = $schemaPath + '[' + $i + ']'; - $it.errorPath = it.util.getPathExpr(it.errorPath, $i, it.opts.jsonPointers, true); - 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.util.getPathExpr(it.errorPath, 'i' + $lvl, it.opts.jsonPointers, true); - 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.util.getPathExpr(it.errorPath, 'i' + $lvl, it.opts.jsonPointers, true); - 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; -} diff --git a/lib/dotjs/maxItems.js b/lib/dotjs/maxItems.js deleted file mode 100644 index 3a15542b5..000000000 --- a/lib/dotjs/maxItems.js +++ /dev/null @@ -1,31 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/maxLength.js b/lib/dotjs/maxLength.js deleted file mode 100644 index 1bc7bd758..000000000 --- a/lib/dotjs/maxLength.js +++ /dev/null @@ -1,37 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/maxProperties.js b/lib/dotjs/maxProperties.js deleted file mode 100644 index 487b0cd2c..000000000 --- a/lib/dotjs/maxProperties.js +++ /dev/null @@ -1,31 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/maximum.js b/lib/dotjs/maximum.js deleted file mode 100644 index 28d76b267..000000000 --- a/lib/dotjs/maximum.js +++ /dev/null @@ -1,34 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/minItems.js b/lib/dotjs/minItems.js deleted file mode 100644 index 77d46f708..000000000 --- a/lib/dotjs/minItems.js +++ /dev/null @@ -1,31 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/minLength.js b/lib/dotjs/minLength.js deleted file mode 100644 index 2ca18002f..000000000 --- a/lib/dotjs/minLength.js +++ /dev/null @@ -1,37 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/minProperties.js b/lib/dotjs/minProperties.js deleted file mode 100644 index ac9dcb4cb..000000000 --- a/lib/dotjs/minProperties.js +++ /dev/null @@ -1,31 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/minimum.js b/lib/dotjs/minimum.js deleted file mode 100644 index ecf8c15e6..000000000 --- a/lib/dotjs/minimum.js +++ /dev/null @@ -1,34 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/multipleOf.js b/lib/dotjs/multipleOf.js deleted file mode 100644 index ff1401fb8..000000000 --- a/lib/dotjs/multipleOf.js +++ /dev/null @@ -1,31 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/not.js b/lib/dotjs/not.js deleted file mode 100644 index 3a2e7a1d7..000000000 --- a/lib/dotjs/not.js +++ /dev/null @@ -1,47 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } '; - if (it.opts.allErrors) { - out += ' } '; - } - } 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - if ($breakOnError) { - out += ' if (false) { '; - } - } - return out; -} diff --git a/lib/dotjs/oneOf.js b/lib/dotjs/oneOf.js deleted file mode 100644 index e2bc4c822..000000000 --- a/lib/dotjs/oneOf.js +++ /dev/null @@ -1,55 +0,0 @@ -'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; - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }'; - if (it.opts.allErrors) { - out += ' } '; - } - return out; -} diff --git a/lib/dotjs/pattern.js b/lib/dotjs/pattern.js deleted file mode 100644 index a6d3f2719..000000000 --- a/lib/dotjs/pattern.js +++ /dev/null @@ -1,32 +0,0 @@ -'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; - 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 "' + (it.util.escapeQuotes($schema)) + '"\' '; - if (it.opts.verbose) { - out += ', schema: ' + (it.util.toQuotedString($schema)) + ', data: ' + ($data); - } - out += ' }]; return false; '; - } else { - out += ' var err = { keyword: \'' + ('pattern') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match pattern "' + (it.util.escapeQuotes($schema)) + '"\' '; - if (it.opts.verbose) { - out += ', schema: ' + (it.util.toQuotedString($schema)) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += '} '; - if ($breakOnError) { - out += ' else { '; - } - return out; -} diff --git a/lib/dotjs/properties.js b/lib/dotjs/properties.js deleted file mode 100644 index 8a1a2f1b4..000000000 --- a/lib/dotjs/properties.js +++ /dev/null @@ -1,170 +0,0 @@ -'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; - 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, - $removeAdditional = it.opts.removeAdditional, - $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional; - out += 'var ' + ($errs) + ' = errors;var valid' + ($it.level) + ' = true;'; - if ($checkAdditional) { - out += ' var propertiesSchema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ' || {}; 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) + ') { '; - if ($removeAdditional == 'all') { - out += ' delete ' + ($data) + '[key' + ($lvl) + ']; '; - } else { - var $currentErrorPath = it.errorPath; - it.errorPath = it.util.getPathExpr(it.errorPath, 'key' + $lvl, it.opts.jsonPointers); - if ($noAdditional) { - if ($removeAdditional) { - out += ' delete ' + ($data) + '[key' + ($lvl) + ']; '; - } else { - 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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - if ($breakOnError) { - out += ' break; '; - } - } - } else if ($additionalIsSchema) { - if ($removeAdditional == 'failing') { - out += ' var ' + ($errs) + ' = errors; '; - } - $it.schema = $aProperties; - $it.schemaPath = it.schemaPath + '.additionalProperties'; - $it.errorPath = it.errorPath; - 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 ($removeAdditional == 'failing') { - out += ' if (!valid' + ($it.level) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[key' + ($lvl) + ']; } '; - } else { - 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; - var $prop = it.util.getProperty($propertyKey), - $passData = $data + $prop; - $it.schemaPath = $schemaPath + $prop; - $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers); - 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' + it.util.getProperty($pProperty); - out += ' for (var key' + ($lvl) + ' in ' + ($data) + ') { if (/' + (it.util.escapeRegExp($pProperty)) + '/.test(key' + ($lvl) + ')) { '; - $it.errorPath = it.util.getPathExpr(it.errorPath, 'key' + $lvl, it.opts.jsonPointers); - 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; -} diff --git a/lib/dotjs/ref.js b/lib/dotjs/ref.js deleted file mode 100644 index 5a93d3cfa..000000000 --- a/lib/dotjs/ref.js +++ /dev/null @@ -1,82 +0,0 @@ -'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 ($schema == '#' || $schema == '#/') { - if (it.isRoot) { - if ($breakOnError && it.wasTop) { - out += ' if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\')'; - if (it.errorPath != '""') { - out += ' + ' + (it.errorPath); - } - out += ') ) return false; else { '; - } else { - out += ' if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\')'; - if (it.errorPath != '""') { - out += ' + ' + (it.errorPath); - } - out += ') ) { if (vErrors === null) vErrors = ' + ('validate') + '.errors; else vErrors = vErrors.concat(' + ('validate') + '.errors); errors = vErrors.length; } '; - if ($breakOnError) { - out += ' else { '; - } - } - } else { - out += ' if (! ' + ('root.refVal[0]') + '(' + ($data) + ', (dataPath || \'\')'; - if (it.errorPath != '""') { - out += ' + ' + (it.errorPath); - } - out += ') ) { if (vErrors === null) vErrors = ' + ('root.refVal[0]') + '.errors; else vErrors = vErrors.concat(' + ('root.refVal[0]') + '.errors); errors = vErrors.length; } '; - if ($breakOnError) { - out += ' else { '; - } - } - } else { - var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot); - if ($refVal === undefined) { - var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId; - if (it.opts.missingRefs == 'fail') { - console.log($message); - if (it.wasTop && $breakOnError) { - out += ' validate.errors = [ { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' '; - if (it.opts.verbose) { - out += ', schema: ' + (it.util.toQuotedString($schema)) + ', data: ' + ($data); - } - out += ' }]; return false; '; - } else { - out += ' var err = { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' '; - if (it.opts.verbose) { - out += ', schema: ' + (it.util.toQuotedString($schema)) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - if ($breakOnError) { - out += ' if (false) { '; - } - } else if (it.opts.missingRefs == 'ignore') { - console.log($message); - if ($breakOnError) { - out += ' if (true) { '; - } - } else { - throw new Error($message); - } - } else { - out += ' if (! ' + ($refVal) + '(' + ($data) + ', (dataPath || \'\')'; - if (it.errorPath != '""') { - out += ' + ' + (it.errorPath); - } - out += ') ) { if (vErrors === null) vErrors = ' + ($refVal) + '.errors; else vErrors = vErrors.concat(' + ($refVal) + '.errors); errors = vErrors.length; } '; - if ($breakOnError) { - out += ' else { '; - } - } - } - return out; -} diff --git a/lib/dotjs/required.js b/lib/dotjs/required.js deleted file mode 100644 index c59ae8d27..000000000 --- a/lib/dotjs/required.js +++ /dev/null @@ -1,103 +0,0 @@ -'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; - var $currentErrorPath = it.errorPath; - if ($breakOnError) { - out += ' var missing' + ($lvl) + '; '; - if ($schema.length <= 20) { - 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 += ' || '; - } - var $prop = it.util.getProperty($property); - out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $property : $prop)) + ') ) '; - } - } - out += ') { '; - var $propertyPath = 'missing' + $lvl, - $missingProperty = '\' + ' + $propertyPath + ' + \''; - it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath; - if (it.wasTop && $breakOnError) { - out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is 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: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += ' } else { '; - } else { - out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; '; - var $i = 'i' + $lvl, - $propertyPath = 'schema' + $lvl + '[' + $i + ']', - $missingProperty = '\' + "\'" + ' + $propertyPath + ' + "\'" + \''; - it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers); - out += ' 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: \'property ' + ($missingProperty) + ' is 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: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += ' } else { '; - } - } else { - if ($schema.length <= 10) { - var arr2 = $schema; - if (arr2) { - var $property, $i = -1, - l2 = arr2.length - 1; - while ($i < l2) { - $property = arr2[$i += 1]; - var $prop = it.util.getProperty($property), - $missingProperty = it.util.escapeQuotes($prop); - it.errorPath = it.util.getPath($currentErrorPath, $property, it.opts.jsonPointers); - out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } '; - } - } - } else { - out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; '; - var $i = 'i' + $lvl, - $propertyPath = 'schema' + $lvl + '[' + $i + ']', - $missingProperty = '\' + "\'" + ' + $propertyPath + ' + "\'" + \''; - it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers); - out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < schema' + ($lvl) + '.length; ' + ($i) + '++) { if (' + ($data) + '[schema' + ($lvl) + '[' + ($i) + ']] === undefined) { var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } '; - } - } - it.errorPath = $currentErrorPath; - return out; -} diff --git a/lib/dotjs/uniqueItems.js b/lib/dotjs/uniqueItems.js deleted file mode 100644 index d099169cd..000000000 --- a/lib/dotjs/uniqueItems.js +++ /dev/null @@ -1,37 +0,0 @@ -'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 ($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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += ' } '; - if ($breakOnError) { - out += ' else { '; - } - } else { - if ($breakOnError) { - out += ' if (true) { '; - } - } - return out; -} diff --git a/lib/dotjs/validate.js b/lib/dotjs/validate.js deleted file mode 100644 index 22d8648c4..000000000 --- a/lib/dotjs/validate.js +++ /dev/null @@ -1,182 +0,0 @@ -'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.root.schema.id); - delete it.isTop; - it.wasTop = true; - out += ' validate = function (data, dataPath) { \'use strict\'; var vErrors = null; '; - out += ' var errors = 0; '; - } else { - 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 (vErrors === null) vErrors = [err]; else vErrors.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 (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; - } - out += ' }'; - } - if ($breakOnError) { - out += ' ' + ($closingBraces2) + ' '; - } - if ($top) { - out += ' validate.errors = vErrors; '; - 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; -} diff --git a/package.json b/package.json index 8f84f41f7..4a71e8c16 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "bundle": "browserify -r ./lib/ajv.js:ajv -o ajv.bundle.js", "build": "node scripts/compile-dots.js", "test-browser": "scripts/prepare-tests && karma start --single-run --browsers PhantomJS", - "test": "npm run test-cov && npm run test-browser", + "test": "npm run build && npm run test-cov && npm run test-browser", "prepublish": "npm run build", "watch": "watch 'npm run build' ./lib/dot" }, diff --git a/scripts/compile-dots.js b/scripts/compile-dots.js index c6840f7e8..e8e64529e 100644 --- a/scripts/compile-dots.js +++ b/scripts/compile-dots.js @@ -9,6 +9,9 @@ var glob = require('glob') var defs = fs.readFileSync(path.join(__dirname, '../lib/dot/definitions.def')); var files = glob.sync('../lib/dot/*.jst', { cwd: __dirname }); +var dotjsPath = path.join(__dirname, '../lib/dotjs'); +try { fs.mkdirSync(dotjsPath); } catch(e) {} + console.log('\n\nCompiling:'); files.forEach(function (f) { @@ -18,7 +21,7 @@ files.forEach(function (f) { 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); + , targetPath = path.join(dotjsPath, targetFile); fs.writeFileSync(targetPath, code); console.log('compiled', targetFile); });