Commit bf137ad8 authored by Patrick's avatar Patrick
Browse files

CHANGES.md, README.md und 248 weitere dateien aktualisiert...

parent 0081cef7
Showing with 509 additions and 1116 deletions
+509 -1116
'use strict';
//all requires must be explicit because browserify won't work with dynamic requires
module.exports = {
'$ref': require('./ref'),
allOf: require('./allOf'),
anyOf: require('./anyOf'),
'$comment': require('./comment'),
const: require('./const'),
contains: require('./contains'),
dependencies: require('./dependencies'),
'enum': require('./enum'),
format: require('./format'),
'if': require('./if'),
items: require('./items'),
maximum: require('./_limit'),
minimum: require('./_limit'),
maxItems: require('./_limitItems'),
minItems: require('./_limitItems'),
maxLength: require('./_limitLength'),
minLength: require('./_limitLength'),
maxProperties: require('./_limitProperties'),
minProperties: require('./_limitProperties'),
multipleOf: require('./multipleOf'),
not: require('./not'),
oneOf: require('./oneOf'),
pattern: require('./pattern'),
properties: require('./properties'),
propertyNames: require('./propertyNames'),
required: require('./required'),
uniqueItems: require('./uniqueItems'),
validate: require('./validate')
};
'use strict'; 'use strict';
module.exports = function generate_items(it, $keyword, $ruleType) { module.exports = function generate_items(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -43,8 +43,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) { ...@@ -43,8 +43,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -66,7 +65,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) { ...@@ -66,7 +65,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) {
l1 = arr1.length - 1; l1 = arr1.length - 1;
while ($i < l1) { while ($i < l1) {
$sch = arr1[$i += 1]; $sch = arr1[$i += 1];
if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) { if (it.util.schemaHasRules($sch, it.RULES.all)) {
out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { '; out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';
var $passData = $data + '[' + $i + ']'; var $passData = $data + '[' + $i + ']';
$it.schema = $sch; $it.schema = $sch;
...@@ -89,7 +88,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) { ...@@ -89,7 +88,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) {
} }
} }
} }
if (typeof $additionalItems == 'object' && (it.opts.strictKeywords ? typeof $additionalItems == 'object' && Object.keys($additionalItems).length > 0 : it.util.schemaHasRules($additionalItems, it.RULES.all))) { if (typeof $additionalItems == 'object' && it.util.schemaHasRules($additionalItems, it.RULES.all)) {
$it.schema = $additionalItems; $it.schema = $additionalItems;
$it.schemaPath = it.schemaPath + '.additionalItems'; $it.schemaPath = it.schemaPath + '.additionalItems';
$it.errSchemaPath = it.errSchemaPath + '/additionalItems'; $it.errSchemaPath = it.errSchemaPath + '/additionalItems';
...@@ -113,7 +112,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) { ...@@ -113,7 +112,7 @@ module.exports = function generate_items(it, $keyword, $ruleType) {
$closingBraces += '}'; $closingBraces += '}';
} }
} }
} else if ((it.opts.strictKeywords ? typeof $schema == 'object' && Object.keys($schema).length > 0 : it.util.schemaHasRules($schema, it.RULES.all))) { } else if (it.util.schemaHasRules($schema, it.RULES.all)) {
$it.schema = $schema; $it.schema = $schema;
$it.schemaPath = $schemaPath; $it.schemaPath = $schemaPath;
$it.errSchemaPath = $errSchemaPath; $it.errSchemaPath = $errSchemaPath;
...@@ -131,7 +130,11 @@ module.exports = function generate_items(it, $keyword, $ruleType) { ...@@ -131,7 +130,11 @@ module.exports = function generate_items(it, $keyword, $ruleType) {
if ($breakOnError) { if ($breakOnError) {
out += ' if (!' + ($nextValid) + ') break; '; out += ' if (!' + ($nextValid) + ') break; ';
} }
out += ' }'; out += ' } ';
if ($breakOnError) {
out += ' if (' + ($nextValid) + ') { ';
$closingBraces += '}';
}
} }
if ($breakOnError) { if ($breakOnError) {
out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {'; out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
......
'use strict'; 'use strict';
module.exports = function generate_multipleOf(it, $keyword, $ruleType) { module.exports = function generate_multipleOf(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -8,7 +8,7 @@ module.exports = function generate_multipleOf(it, $keyword, $ruleType) { ...@@ -8,7 +8,7 @@ module.exports = function generate_multipleOf(it, $keyword, $ruleType) {
var $errSchemaPath = it.errSchemaPath + '/' + $keyword; var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''); var $data = 'data' + ($dataLvl || '');
var $isData = it.opts.$data && $schema && $schema.$data, var $isData = it.opts.v5 && $schema && $schema.$data,
$schemaValue; $schemaValue;
if ($isData) { if ($isData) {
out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; '; out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
...@@ -41,7 +41,7 @@ module.exports = function generate_multipleOf(it, $keyword, $ruleType) { ...@@ -41,7 +41,7 @@ module.exports = function generate_multipleOf(it, $keyword, $ruleType) {
if ($isData) { if ($isData) {
out += '\' + ' + ($schemaValue); out += '\' + ' + ($schemaValue);
} else { } else {
out += '' + ($schemaValue) + '\''; out += '' + ($schema) + '\'';
} }
} }
if (it.opts.verbose) { if (it.opts.verbose) {
...@@ -59,8 +59,7 @@ module.exports = function generate_multipleOf(it, $keyword, $ruleType) { ...@@ -59,8 +59,7 @@ module.exports = function generate_multipleOf(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
......
'use strict'; 'use strict';
module.exports = function generate_not(it, $keyword, $ruleType) { module.exports = function generate_not(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -12,7 +12,7 @@ module.exports = function generate_not(it, $keyword, $ruleType) { ...@@ -12,7 +12,7 @@ module.exports = function generate_not(it, $keyword, $ruleType) {
var $it = it.util.copy(it); var $it = it.util.copy(it);
$it.level++; $it.level++;
var $nextValid = 'valid' + $it.level; var $nextValid = 'valid' + $it.level;
if ((it.opts.strictKeywords ? typeof $schema == 'object' && Object.keys($schema).length > 0 : it.util.schemaHasRules($schema, it.RULES.all))) { if (it.util.schemaHasRules($schema, it.RULES.all)) {
$it.schema = $schema; $it.schema = $schema;
$it.schemaPath = $schemaPath; $it.schemaPath = $schemaPath;
$it.errSchemaPath = $errSchemaPath; $it.errSchemaPath = $errSchemaPath;
...@@ -47,8 +47,7 @@ module.exports = function generate_not(it, $keyword, $ruleType) { ...@@ -47,8 +47,7 @@ module.exports = function generate_not(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
......
'use strict'; 'use strict';
module.exports = function generate_oneOf(it, $keyword, $ruleType) { module.exports = function generate_oneOf(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -14,10 +14,8 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) { ...@@ -14,10 +14,8 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) {
var $closingBraces = ''; var $closingBraces = '';
$it.level++; $it.level++;
var $nextValid = 'valid' + $it.level; var $nextValid = 'valid' + $it.level;
var $currentBaseId = $it.baseId, out += 'var ' + ($errs) + ' = errors;var prevValid' + ($lvl) + ' = false;var ' + ($valid) + ' = false;';
$prevValid = 'prevValid' + $lvl, var $currentBaseId = $it.baseId;
$passingSchemas = 'passingSchemas' + $lvl;
out += 'var ' + ($errs) + ' = errors , ' + ($prevValid) + ' = false , ' + ($valid) + ' = false , ' + ($passingSchemas) + ' = null; ';
var $wasComposite = it.compositeRule; var $wasComposite = it.compositeRule;
it.compositeRule = $it.compositeRule = true; it.compositeRule = $it.compositeRule = true;
var arr1 = $schema; var arr1 = $schema;
...@@ -26,7 +24,7 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) { ...@@ -26,7 +24,7 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) {
l1 = arr1.length - 1; l1 = arr1.length - 1;
while ($i < l1) { while ($i < l1) {
$sch = arr1[$i += 1]; $sch = arr1[$i += 1];
if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) { if (it.util.schemaHasRules($sch, it.RULES.all)) {
$it.schema = $sch; $it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']'; $it.schemaPath = $schemaPath + '[' + $i + ']';
$it.errSchemaPath = $errSchemaPath + '/' + $i; $it.errSchemaPath = $errSchemaPath + '/' + $i;
...@@ -36,16 +34,19 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) { ...@@ -36,16 +34,19 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) {
out += ' var ' + ($nextValid) + ' = true; '; out += ' var ' + ($nextValid) + ' = true; ';
} }
if ($i) { if ($i) {
out += ' if (' + ($nextValid) + ' && ' + ($prevValid) + ') { ' + ($valid) + ' = false; ' + ($passingSchemas) + ' = [' + ($passingSchemas) + ', ' + ($i) + ']; } else { '; out += ' if (' + ($nextValid) + ' && prevValid' + ($lvl) + ') ' + ($valid) + ' = false; else { ';
$closingBraces += '}'; $closingBraces += '}';
} }
out += ' if (' + ($nextValid) + ') { ' + ($valid) + ' = ' + ($prevValid) + ' = true; ' + ($passingSchemas) + ' = ' + ($i) + '; }'; out += ' if (' + ($nextValid) + ') ' + ($valid) + ' = prevValid' + ($lvl) + ' = true;';
} }
} }
it.compositeRule = $it.compositeRule = $wasComposite; it.compositeRule = $it.compositeRule = $wasComposite;
out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */ out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) { if (it.createErrors !== false) {
out += ' { keyword: \'' + ('oneOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { passingSchemas: ' + ($passingSchemas) + ' } '; out += ' { keyword: \'' + ('oneOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
if (it.opts.messages !== false) { if (it.opts.messages !== false) {
out += ' , message: \'should match exactly one schema in oneOf\' '; out += ' , message: \'should match exactly one schema in oneOf\' ';
} }
...@@ -56,14 +57,16 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) { ...@@ -56,14 +57,16 @@ module.exports = function generate_oneOf(it, $keyword, $ruleType) {
} else { } else {
out += ' {} '; out += ' {} ';
} }
out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; var __err = out;
if (!it.compositeRule && $breakOnError) { out = $$outStack.pop();
/* istanbul ignore if */ if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError(vErrors); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
out += ' validate.errors = vErrors; return false; '; out += ' validate.errors = [' + (__err) + ']; return false; ';
} }
} else {
out += ' var err = ' + (__err) + '; 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; }'; out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }';
if (it.opts.allErrors) { if (it.opts.allErrors) {
......
'use strict'; 'use strict';
module.exports = function generate_pattern(it, $keyword, $ruleType) { module.exports = function generate_pattern(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -8,7 +8,7 @@ module.exports = function generate_pattern(it, $keyword, $ruleType) { ...@@ -8,7 +8,7 @@ module.exports = function generate_pattern(it, $keyword, $ruleType) {
var $errSchemaPath = it.errSchemaPath + '/' + $keyword; var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''); var $data = 'data' + ($dataLvl || '');
var $isData = it.opts.$data && $schema && $schema.$data, var $isData = it.opts.v5 && $schema && $schema.$data,
$schemaValue; $schemaValue;
if ($isData) { if ($isData) {
out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; '; out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
...@@ -57,8 +57,7 @@ module.exports = function generate_pattern(it, $keyword, $ruleType) { ...@@ -57,8 +57,7 @@ module.exports = function generate_pattern(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
......
'use strict'; 'use strict';
module.exports = function generate_properties(it, $keyword, $ruleType) { module.exports = function generate_properties(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -8,16 +8,15 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -8,16 +8,15 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
var $errSchemaPath = it.errSchemaPath + '/' + $keyword; var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''); var $data = 'data' + ($dataLvl || '');
var $valid = 'valid' + $lvl;
var $errs = 'errs__' + $lvl; var $errs = 'errs__' + $lvl;
var $it = it.util.copy(it); var $it = it.util.copy(it);
var $closingBraces = ''; var $closingBraces = '';
$it.level++; $it.level++;
var $nextValid = 'valid' + $it.level; var $nextValid = 'valid' + $it.level;
var $key = 'key' + $lvl, var $key = 'key' + $lvl,
$idx = 'idx' + $lvl,
$dataNxt = $it.dataLevel = it.dataLevel + 1, $dataNxt = $it.dataLevel = it.dataLevel + 1,
$nextData = 'data' + $dataNxt, $nextData = 'data' + $dataNxt;
$dataProperties = 'dataProperties' + $lvl;
var $schemaKeys = Object.keys($schema || {}), var $schemaKeys = Object.keys($schema || {}),
$pProperties = it.schema.patternProperties || {}, $pProperties = it.schema.patternProperties || {},
$pPropertyKeys = Object.keys($pProperties), $pPropertyKeys = Object.keys($pProperties),
...@@ -30,22 +29,22 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -30,22 +29,22 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
$ownProperties = it.opts.ownProperties, $ownProperties = it.opts.ownProperties,
$currentBaseId = it.baseId; $currentBaseId = it.baseId;
var $required = it.schema.required; var $required = it.schema.required;
if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required); if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required);
out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;'; if (it.opts.v5) {
if ($ownProperties) { var $pgProperties = it.schema.patternGroups || {},
out += ' var ' + ($dataProperties) + ' = undefined;'; $pgPropertyKeys = Object.keys($pgProperties);
} }
out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
if ($checkAdditional) { if ($checkAdditional) {
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
if ($ownProperties) { if ($ownProperties) {
out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; '; out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
} else {
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
} }
if ($someProperties) { if ($someProperties) {
out += ' var isAdditional' + ($lvl) + ' = !(false '; out += ' var isAdditional' + ($lvl) + ' = !(false ';
if ($schemaKeys.length) { if ($schemaKeys.length) {
if ($schemaKeys.length > 8) { if ($schemaKeys.length > 5) {
out += ' || validate.schema' + ($schemaPath) + '.hasOwnProperty(' + ($key) + ') '; out += ' || validate.schema' + ($schemaPath) + '[' + ($key) + '] ';
} else { } else {
var arr1 = $schemaKeys; var arr1 = $schemaKeys;
if (arr1) { if (arr1) {
...@@ -69,6 +68,17 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -69,6 +68,17 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
} }
} }
} }
if (it.opts.v5 && $pgPropertyKeys && $pgPropertyKeys.length) {
var arr3 = $pgPropertyKeys;
if (arr3) {
var $pgProperty, $i = -1,
l3 = arr3.length - 1;
while ($i < l3) {
$pgProperty = arr3[$i += 1];
out += ' || ' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ') ';
}
}
}
out += ' ); if (isAdditional' + ($lvl) + ') { '; out += ' ); if (isAdditional' + ($lvl) + ') { ';
} }
if ($removeAdditional == 'all') { if ($removeAdditional == 'all') {
...@@ -92,13 +102,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -92,13 +102,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
if (it.createErrors !== false) { if (it.createErrors !== false) {
out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } '; out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
if (it.opts.messages !== false) { if (it.opts.messages !== false) {
out += ' , message: \''; out += ' , message: \'should NOT have additional properties\' ';
if (it.opts._errorDataPathProperty) {
out += 'is an invalid additional property';
} else {
out += 'should NOT have additional properties';
}
out += '\' ';
} }
if (it.opts.verbose) { if (it.opts.verbose) {
out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' '; out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
...@@ -109,8 +113,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -109,8 +113,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -176,14 +179,14 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -176,14 +179,14 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
} }
var $useDefaults = it.opts.useDefaults && !it.compositeRule; var $useDefaults = it.opts.useDefaults && !it.compositeRule;
if ($schemaKeys.length) { if ($schemaKeys.length) {
var arr3 = $schemaKeys; var arr4 = $schemaKeys;
if (arr3) { if (arr4) {
var $propertyKey, i3 = -1, var $propertyKey, i4 = -1,
l3 = arr3.length - 1; l4 = arr4.length - 1;
while (i3 < l3) { while (i4 < l4) {
$propertyKey = arr3[i3 += 1]; $propertyKey = arr4[i4 += 1];
var $sch = $schema[$propertyKey]; var $sch = $schema[$propertyKey];
if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) { if (it.util.schemaHasRules($sch, it.RULES.all)) {
var $prop = it.util.getProperty($propertyKey), var $prop = it.util.getProperty($propertyKey),
$passData = $data + $prop, $passData = $data + $prop,
$hasDefault = $useDefaults && $sch.default !== undefined; $hasDefault = $useDefaults && $sch.default !== undefined;
...@@ -205,11 +208,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -205,11 +208,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
out += ' ' + ($code) + ' '; out += ' ' + ($code) + ' ';
} else { } else {
if ($requiredHash && $requiredHash[$propertyKey]) { if ($requiredHash && $requiredHash[$propertyKey]) {
out += ' if ( ' + ($useData) + ' === undefined '; out += ' if (' + ($useData) + ' === undefined) { ' + ($nextValid) + ' = false; ';
if ($ownProperties) {
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
}
out += ') { ' + ($nextValid) + ' = false; ';
var $currentErrorPath = it.errorPath, var $currentErrorPath = it.errorPath,
$currErrSchemaPath = $errSchemaPath, $currErrSchemaPath = $errSchemaPath,
$missingProperty = it.util.escapeQuotes($propertyKey); $missingProperty = it.util.escapeQuotes($propertyKey);
...@@ -240,8 +239,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -240,8 +239,7 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -255,17 +253,9 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -255,17 +253,9 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
out += ' } else { '; out += ' } else { ';
} else { } else {
if ($breakOnError) { if ($breakOnError) {
out += ' if ( ' + ($useData) + ' === undefined '; out += ' if (' + ($useData) + ' === undefined) { ' + ($nextValid) + ' = true; } else { ';
if ($ownProperties) {
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
}
out += ') { ' + ($nextValid) + ' = true; } else { ';
} else { } else {
out += ' if (' + ($useData) + ' !== undefined '; out += ' if (' + ($useData) + ' !== undefined) { ';
if ($ownProperties) {
out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
}
out += ' ) { ';
} }
} }
out += ' ' + ($code) + ' } '; out += ' ' + ($code) + ' } ';
...@@ -278,24 +268,65 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -278,24 +268,65 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
} }
} }
} }
if ($pPropertyKeys.length) { var arr5 = $pPropertyKeys;
var arr4 = $pPropertyKeys; if (arr5) {
if (arr4) { var $pProperty, i5 = -1,
var $pProperty, i4 = -1, l5 = arr5.length - 1;
l4 = arr4.length - 1; while (i5 < l5) {
while (i4 < l4) { $pProperty = arr5[i5 += 1];
$pProperty = arr4[i4 += 1]; var $sch = $pProperties[$pProperty];
var $sch = $pProperties[$pProperty]; if (it.util.schemaHasRules($sch, it.RULES.all)) {
if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) { $it.schema = $sch;
$it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
$it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
if ($ownProperties) {
out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
}
out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
$it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
var $passData = $data + '[' + $key + ']';
$it.dataPathArr[$dataNxt] = $key;
var $code = it.validate($it);
$it.baseId = $currentBaseId;
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
if ($breakOnError) {
out += ' if (!' + ($nextValid) + ') break; ';
}
out += ' } ';
if ($breakOnError) {
out += ' else ' + ($nextValid) + ' = true; ';
}
out += ' } ';
if ($breakOnError) {
out += ' if (' + ($nextValid) + ') { ';
$closingBraces += '}';
}
}
}
}
if (it.opts.v5) {
var arr6 = $pgPropertyKeys;
if (arr6) {
var $pgProperty, i6 = -1,
l6 = arr6.length - 1;
while (i6 < l6) {
$pgProperty = arr6[i6 += 1];
var $pgSchema = $pgProperties[$pgProperty],
$sch = $pgSchema.schema;
if (it.util.schemaHasRules($sch, it.RULES.all)) {
$it.schema = $sch; $it.schema = $sch;
$it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty); $it.schemaPath = it.schemaPath + '.patternGroups' + it.util.getProperty($pgProperty) + '.schema';
$it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty); $it.errSchemaPath = it.errSchemaPath + '/patternGroups/' + it.util.escapeFragment($pgProperty) + '/schema';
out += ' var pgPropCount' + ($lvl) + ' = 0; for (var ' + ($key) + ' in ' + ($data) + ') { ';
if ($ownProperties) { if ($ownProperties) {
out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; '; out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
} else {
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
} }
out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { '; out += ' if (' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ')) { pgPropCount' + ($lvl) + '++; ';
$it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers); $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
var $passData = $data + '[' + $key + ']'; var $passData = $data + '[' + $key + ']';
$it.dataPathArr[$dataNxt] = $key; $it.dataPathArr[$dataNxt] = $key;
...@@ -318,6 +349,90 @@ module.exports = function generate_properties(it, $keyword, $ruleType) { ...@@ -318,6 +349,90 @@ module.exports = function generate_properties(it, $keyword, $ruleType) {
out += ' if (' + ($nextValid) + ') { '; out += ' if (' + ($nextValid) + ') { ';
$closingBraces += '}'; $closingBraces += '}';
} }
var $pgMin = $pgSchema.minimum,
$pgMax = $pgSchema.maximum;
if ($pgMin !== undefined || $pgMax !== undefined) {
out += ' var ' + ($valid) + ' = true; ';
var $currErrSchemaPath = $errSchemaPath;
if ($pgMin !== undefined) {
var $limit = $pgMin,
$reason = 'minimum',
$moreOrLess = 'less';
out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' >= ' + ($pgMin) + '; ';
$errSchemaPath = it.errSchemaPath + '/patternGroups/minimum';
out += ' if (!' + ($valid) + ') { ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
}
out += ' } ';
} else {
out += ' {} ';
}
var __err = out;
out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); ';
} else {
out += ' validate.errors = [' + (__err) + ']; return false; ';
}
} else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
}
out += ' } ';
if ($pgMax !== undefined) {
out += ' else ';
}
}
if ($pgMax !== undefined) {
var $limit = $pgMax,
$reason = 'maximum',
$moreOrLess = 'more';
out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' <= ' + ($pgMax) + '; ';
$errSchemaPath = it.errSchemaPath + '/patternGroups/maximum';
out += ' if (!' + ($valid) + ') { ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
}
out += ' } ';
} else {
out += ' {} ';
}
var __err = out;
out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); ';
} else {
out += ' validate.errors = [' + (__err) + ']; return false; ';
}
} else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
}
out += ' } ';
}
$errSchemaPath = $currErrSchemaPath;
if ($breakOnError) {
out += ' if (' + ($valid) + ') { ';
$closingBraces += '}';
}
}
} }
} }
} }
......
'use strict';
module.exports = function generate_propertyNames(it, $keyword, $ruleType) {
var out = ' ';
var $lvl = it.level;
var $dataLvl = it.dataLevel;
var $schema = it.schema[$keyword];
var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || '');
var $errs = 'errs__' + $lvl;
var $it = it.util.copy(it);
var $closingBraces = '';
$it.level++;
var $nextValid = 'valid' + $it.level;
out += 'var ' + ($errs) + ' = errors;';
if ((it.opts.strictKeywords ? typeof $schema == 'object' && Object.keys($schema).length > 0 : it.util.schemaHasRules($schema, it.RULES.all))) {
$it.schema = $schema;
$it.schemaPath = $schemaPath;
$it.errSchemaPath = $errSchemaPath;
var $key = 'key' + $lvl,
$idx = 'idx' + $lvl,
$i = 'i' + $lvl,
$invalidName = '\' + ' + $key + ' + \'',
$dataNxt = $it.dataLevel = it.dataLevel + 1,
$nextData = 'data' + $dataNxt,
$dataProperties = 'dataProperties' + $lvl,
$ownProperties = it.opts.ownProperties,
$currentBaseId = it.baseId;
if ($ownProperties) {
out += ' var ' + ($dataProperties) + ' = undefined; ';
}
if ($ownProperties) {
out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
} else {
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
}
out += ' var startErrs' + ($lvl) + ' = errors; ';
var $passData = $key;
var $wasComposite = it.compositeRule;
it.compositeRule = $it.compositeRule = true;
var $code = it.validate($it);
$it.baseId = $currentBaseId;
if (it.util.varOccurences($code, $nextData) < 2) {
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
it.compositeRule = $it.compositeRule = $wasComposite;
out += ' if (!' + ($nextValid) + ') { for (var ' + ($i) + '=startErrs' + ($lvl) + '; ' + ($i) + '<errors; ' + ($i) + '++) { vErrors[' + ($i) + '].propertyName = ' + ($key) + '; } var err = '; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('propertyNames') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { propertyName: \'' + ($invalidName) + '\' } ';
if (it.opts.messages !== false) {
out += ' , message: \'property name \\\'' + ($invalidName) + '\\\' is invalid\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
}
out += ' } ';
} else {
out += ' {} ';
}
out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
if (!it.compositeRule && $breakOnError) {
/* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError(vErrors); ';
} else {
out += ' validate.errors = vErrors; return false; ';
}
}
if ($breakOnError) {
out += ' break; ';
}
out += ' } }';
}
if ($breakOnError) {
out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
}
out = it.util.cleanUpCode(out);
return out;
}
'use strict'; 'use strict';
module.exports = function generate_ref(it, $keyword, $ruleType) { module.exports = function generate_ref(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -20,9 +20,9 @@ module.exports = function generate_ref(it, $keyword, $ruleType) { ...@@ -20,9 +20,9 @@ module.exports = function generate_ref(it, $keyword, $ruleType) {
} else { } else {
var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot); var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);
if ($refVal === undefined) { if ($refVal === undefined) {
var $message = it.MissingRefError.message(it.baseId, $schema); var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId;
if (it.opts.missingRefs == 'fail') { if (it.opts.missingRefs == 'fail') {
it.logger.error($message); console.log($message);
var $$outStack = $$outStack || []; var $$outStack = $$outStack || [];
$$outStack.push(out); $$outStack.push(out);
out = ''; /* istanbul ignore else */ out = ''; /* istanbul ignore else */
...@@ -40,8 +40,7 @@ module.exports = function generate_ref(it, $keyword, $ruleType) { ...@@ -40,8 +40,7 @@ module.exports = function generate_ref(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -54,12 +53,15 @@ module.exports = function generate_ref(it, $keyword, $ruleType) { ...@@ -54,12 +53,15 @@ module.exports = function generate_ref(it, $keyword, $ruleType) {
out += ' if (false) { '; out += ' if (false) { ';
} }
} else if (it.opts.missingRefs == 'ignore') { } else if (it.opts.missingRefs == 'ignore') {
it.logger.warn($message); console.log($message);
if ($breakOnError) { if ($breakOnError) {
out += ' if (true) { '; out += ' if (true) { ';
} }
} else { } else {
throw new it.MissingRefError(it.baseId, $schema, $message); var $error = new Error($message);
$error.missingRef = it.resolve.url(it.baseId, $schema);
$error.missingSchema = it.resolve.normalizeId(it.resolve.fullPath($error.missingRef));
throw $error;
} }
} else if ($refVal.inline) { } else if ($refVal.inline) {
var $it = it.util.copy(it); var $it = it.util.copy(it);
...@@ -74,7 +76,7 @@ module.exports = function generate_ref(it, $keyword, $ruleType) { ...@@ -74,7 +76,7 @@ module.exports = function generate_ref(it, $keyword, $ruleType) {
out += ' if (' + ($nextValid) + ') { '; out += ' if (' + ($nextValid) + ') { ';
} }
} else { } else {
$async = $refVal.$async === true || (it.async && $refVal.$async !== false); $async = $refVal.$async === true;
$refCode = $refVal.code; $refCode = $refVal.code;
} }
} }
...@@ -98,18 +100,11 @@ module.exports = function generate_ref(it, $keyword, $ruleType) { ...@@ -98,18 +100,11 @@ module.exports = function generate_ref(it, $keyword, $ruleType) {
out = $$outStack.pop(); out = $$outStack.pop();
if ($async) { if ($async) {
if (!it.async) throw new Error('async schema referenced by sync schema'); if (!it.async) throw new Error('async schema referenced by sync schema');
out += ' try { ';
if ($breakOnError) { if ($breakOnError) {
out += ' var ' + ($valid) + '; '; out += 'var ' + ($valid) + ' =';
} }
out += ' try { await ' + (__callValidate) + '; '; out += ' ' + (it.yieldAwait) + ' ' + (__callValidate) + '; } catch (e) { if (!(e instanceof ValidationError)) throw e; if (vErrors === null) vErrors = e.errors; else vErrors = vErrors.concat(e.errors); errors = vErrors.length; } ';
if ($breakOnError) {
out += ' ' + ($valid) + ' = true; ';
}
out += ' } catch (e) { if (!(e instanceof ValidationError)) throw e; if (vErrors === null) vErrors = e.errors; else vErrors = vErrors.concat(e.errors); errors = vErrors.length; ';
if ($breakOnError) {
out += ' ' + ($valid) + ' = false; ';
}
out += ' } ';
if ($breakOnError) { if ($breakOnError) {
out += ' if (' + ($valid) + ') { '; out += ' if (' + ($valid) + ') { ';
} }
......
'use strict'; 'use strict';
module.exports = function generate_required(it, $keyword, $ruleType) { module.exports = function generate_required(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -9,7 +9,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''); var $data = 'data' + ($dataLvl || '');
var $valid = 'valid' + $lvl; var $valid = 'valid' + $lvl;
var $isData = it.opts.$data && $schema && $schema.$data, var $isData = it.opts.v5 && $schema && $schema.$data,
$schemaValue; $schemaValue;
if ($isData) { if ($isData) {
out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; '; out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
...@@ -28,7 +28,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -28,7 +28,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
while (i1 < l1) { while (i1 < l1) {
$property = arr1[i1 += 1]; $property = arr1[i1 += 1];
var $propertySch = it.schema.properties[$property]; var $propertySch = it.schema.properties[$property];
if (!($propertySch && (it.opts.strictKeywords ? typeof $propertySch == 'object' && Object.keys($propertySch).length > 0 : it.util.schemaHasRules($propertySch, it.RULES.all)))) { if (!($propertySch && it.util.schemaHasRules($propertySch, it.RULES.all))) {
$required[$required.length] = $property; $required[$required.length] = $property;
} }
} }
...@@ -39,8 +39,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -39,8 +39,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
} }
if ($isData || $required.length) { if ($isData || $required.length) {
var $currentErrorPath = it.errorPath, var $currentErrorPath = it.errorPath,
$loopRequired = $isData || $required.length >= it.opts.loopRequired, $loopRequired = $isData || $required.length >= it.opts.loopRequired;
$ownProperties = it.opts.ownProperties;
if ($breakOnError) { if ($breakOnError) {
out += ' var missing' + ($lvl) + '; '; out += ' var missing' + ($lvl) + '; ';
if ($loopRequired) { if ($loopRequired) {
...@@ -57,11 +56,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -57,11 +56,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
if ($isData) { if ($isData) {
out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {'; out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';
} }
out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined '; out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined; if (!' + ($valid) + ') break; } ';
if ($ownProperties) {
out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
}
out += '; if (!' + ($valid) + ') break; } ';
if ($isData) { if ($isData) {
out += ' } '; out += ' } ';
} }
...@@ -89,8 +84,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -89,8 +84,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -104,20 +98,15 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -104,20 +98,15 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
out += ' if ( '; out += ' if ( ';
var arr2 = $required; var arr2 = $required;
if (arr2) { if (arr2) {
var $propertyKey, $i = -1, var _$property, $i = -1,
l2 = arr2.length - 1; l2 = arr2.length - 1;
while ($i < l2) { while ($i < l2) {
$propertyKey = arr2[$i += 1]; _$property = arr2[$i += 1];
if ($i) { if ($i) {
out += ' || '; out += ' || ';
} }
var $prop = it.util.getProperty($propertyKey), var $prop = it.util.getProperty(_$property);
$useData = $data + $prop; out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
out += ' ( ( ' + ($useData) + ' === undefined ';
if ($ownProperties) {
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
}
out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';
} }
} }
out += ') { '; out += ') { ';
...@@ -149,8 +138,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -149,8 +138,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -194,11 +182,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -194,11 +182,7 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
} }
out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { '; out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
} }
out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined '; out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined) { var err = '; /* istanbul ignore else */
if ($ownProperties) {
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
}
out += ') { var err = '; /* istanbul ignore else */
if (it.createErrors !== false) { if (it.createErrors !== false) {
out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } '; out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
if (it.opts.messages !== false) { if (it.opts.messages !== false) {
...@@ -224,21 +208,16 @@ module.exports = function generate_required(it, $keyword, $ruleType) { ...@@ -224,21 +208,16 @@ module.exports = function generate_required(it, $keyword, $ruleType) {
} else { } else {
var arr3 = $required; var arr3 = $required;
if (arr3) { if (arr3) {
var $propertyKey, i3 = -1, var $reqProperty, i3 = -1,
l3 = arr3.length - 1; l3 = arr3.length - 1;
while (i3 < l3) { while (i3 < l3) {
$propertyKey = arr3[i3 += 1]; $reqProperty = arr3[i3 += 1];
var $prop = it.util.getProperty($propertyKey), var $prop = it.util.getProperty($reqProperty),
$missingProperty = it.util.escapeQuotes($propertyKey), $missingProperty = it.util.escapeQuotes($reqProperty);
$useData = $data + $prop;
if (it.opts._errorDataPathProperty) { if (it.opts._errorDataPathProperty) {
it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers); it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
}
out += ' if ( ' + ($useData) + ' === undefined ';
if ($ownProperties) {
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
} }
out += ') { var err = '; /* istanbul ignore else */ out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = '; /* istanbul ignore else */
if (it.createErrors !== false) { if (it.createErrors !== false) {
out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } '; out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
if (it.opts.messages !== false) { if (it.opts.messages !== false) {
......
'use strict'; 'use strict';
module.exports = function generate_uniqueItems(it, $keyword, $ruleType) { module.exports = function generate_uniqueItems(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -9,7 +9,7 @@ module.exports = function generate_uniqueItems(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate_uniqueItems(it, $keyword, $ruleType) {
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || ''); var $data = 'data' + ($dataLvl || '');
var $valid = 'valid' + $lvl; var $valid = 'valid' + $lvl;
var $isData = it.opts.$data && $schema && $schema.$data, var $isData = it.opts.v5 && $schema && $schema.$data,
$schemaValue; $schemaValue;
if ($isData) { if ($isData) {
out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; '; out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
...@@ -21,21 +21,7 @@ module.exports = function generate_uniqueItems(it, $keyword, $ruleType) { ...@@ -21,21 +21,7 @@ module.exports = function generate_uniqueItems(it, $keyword, $ruleType) {
if ($isData) { if ($isData) {
out += ' var ' + ($valid) + '; if (' + ($schemaValue) + ' === false || ' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'boolean\') ' + ($valid) + ' = false; else { '; out += ' var ' + ($valid) + '; if (' + ($schemaValue) + ' === false || ' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'boolean\') ' + ($valid) + ' = false; else { ';
} }
out += ' var i = ' + ($data) + '.length , ' + ($valid) + ' = true , j; if (i > 1) { '; 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; } } } } ';
var $itemType = it.schema.items && it.schema.items.type,
$typeIsArray = Array.isArray($itemType);
if (!$itemType || $itemType == 'object' || $itemType == 'array' || ($typeIsArray && ($itemType.indexOf('object') >= 0 || $itemType.indexOf('array') >= 0))) {
out += ' outer: for (;i--;) { for (j = i; j--;) { if (equal(' + ($data) + '[i], ' + ($data) + '[j])) { ' + ($valid) + ' = false; break outer; } } } ';
} else {
out += ' var itemIndices = {}, item; for (;i--;) { var item = ' + ($data) + '[i]; ';
var $method = 'checkDataType' + ($typeIsArray ? 's' : '');
out += ' if (' + (it.util[$method]($itemType, 'item', true)) + ') continue; ';
if ($typeIsArray) {
out += ' if (typeof item == \'string\') item = \'"\' + item; ';
}
out += ' if (typeof itemIndices[item] == \'number\') { ' + ($valid) + ' = false; j = itemIndices[item]; break; } itemIndices[item] = i; } ';
}
out += ' } ';
if ($isData) { if ($isData) {
out += ' } '; out += ' } ';
} }
...@@ -63,8 +49,7 @@ module.exports = function generate_uniqueItems(it, $keyword, $ruleType) { ...@@ -63,8 +49,7 @@ module.exports = function generate_uniqueItems(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
......
'use strict'; 'use strict';
module.exports = function generate_validate(it, $keyword, $ruleType) { module.exports = function generate_validate(it, $keyword) {
var out = ''; var out = '';
var $async = it.schema.$async === true, var $async = it.schema.$async === true;
$refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref'),
$id = it.self._getId(it.schema);
if (it.opts.strictKeywords) {
var $unknownKwd = it.util.schemaUnknownRules(it.schema, it.RULES.keywords);
if ($unknownKwd) {
var $keywordsMsg = 'unknown keyword: ' + $unknownKwd;
if (it.opts.strictKeywords === 'log') it.logger.warn($keywordsMsg);
else throw new Error($keywordsMsg);
}
}
if (it.isTop) {
out += ' var validate = ';
if ($async) {
it.async = true;
out += 'async ';
}
out += 'function(data, dataPath, parentData, parentDataProperty, rootData) { \'use strict\'; ';
if ($id && (it.opts.sourceCode || it.opts.processCode)) {
out += ' ' + ('/\*# sourceURL=' + $id + ' */') + ' ';
}
}
if (typeof it.schema == 'boolean' || !($refKeywords || it.schema.$ref)) {
var $keyword = 'false schema';
var $lvl = it.level;
var $dataLvl = it.dataLevel;
var $schema = it.schema[$keyword];
var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors;
var $errorKeyword;
var $data = 'data' + ($dataLvl || '');
var $valid = 'valid' + $lvl;
if (it.schema === false) {
if (it.isTop) {
$breakOnError = true;
} else {
out += ' var ' + ($valid) + ' = false; ';
}
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ($errorKeyword || 'false schema') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
if (it.opts.messages !== false) {
out += ' , message: \'boolean schema is false\' ';
}
if (it.opts.verbose) {
out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
}
out += ' } ';
} else {
out += ' {} ';
}
var __err = out;
out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) {
/* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); ';
} else {
out += ' validate.errors = [' + (__err) + ']; return false; ';
}
} else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
}
} else {
if (it.isTop) {
if ($async) {
out += ' return data; ';
} else {
out += ' validate.errors = null; return true; ';
}
} else {
out += ' var ' + ($valid) + ' = true; ';
}
}
if (it.isTop) {
out += ' }; return validate; ';
}
return out;
}
if (it.isTop) { if (it.isTop) {
var $top = it.isTop, var $top = it.isTop,
$lvl = it.level = 0, $lvl = it.level = 0,
$dataLvl = it.dataLevel = 0, $dataLvl = it.dataLevel = 0,
$data = 'data'; $data = 'data';
it.rootId = it.resolve.fullPath(it.self._getId(it.root.schema)); it.rootId = it.resolve.fullPath(it.root.schema.id);
it.baseId = it.baseId || it.rootId; it.baseId = it.baseId || it.rootId;
if ($async) {
it.async = true;
var $es7 = it.opts.async == 'es7';
it.yieldAwait = $es7 ? 'await' : 'yield';
}
delete it.isTop; delete it.isTop;
it.dataPathArr = [undefined]; it.dataPathArr = [undefined];
if (it.schema.default !== undefined && it.opts.useDefaults && it.opts.strictDefaults) { out += ' var validate = ';
var $defaultMsg = 'default is ignored in the schema root'; if ($async) {
if (it.opts.strictDefaults === 'log') it.logger.warn($defaultMsg); if ($es7) {
else throw new Error($defaultMsg); out += ' (async function ';
} else {
if (it.opts.async == 'co*') {
out += 'co.wrap';
}
out += '(function* ';
}
} else {
out += ' (function ';
} }
out += ' var vErrors = null; '; out += ' (data, dataPath, parentData, parentDataProperty, rootData) { \'use strict\'; var vErrors = null; ';
out += ' var errors = 0; '; out += ' var errors = 0; ';
out += ' if (rootData === undefined) rootData = data; '; out += ' if (rootData === undefined) rootData = data;';
} else { } else {
var $lvl = it.level, var $lvl = it.level,
$dataLvl = it.dataLevel, $dataLvl = it.dataLevel,
$data = 'data' + ($dataLvl || ''); $data = 'data' + ($dataLvl || '');
if ($id) it.baseId = it.resolve.url(it.baseId, $id); if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
if ($async && !it.async) throw new Error('async schema in sync schema'); if ($async && !it.async) throw new Error('async schema in sync schema');
out += ' var errs_' + ($lvl) + ' = errors;'; out += ' var errs_' + ($lvl) + ' = errors;';
} }
...@@ -112,173 +44,111 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -112,173 +44,111 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
$breakOnError = !it.opts.allErrors, $breakOnError = !it.opts.allErrors,
$closingBraces1 = '', $closingBraces1 = '',
$closingBraces2 = ''; $closingBraces2 = '';
var $errorKeyword;
var $typeSchema = it.schema.type, var $typeSchema = it.schema.type,
$typeIsArray = Array.isArray($typeSchema); $typeIsArray = Array.isArray($typeSchema);
if ($typeSchema && it.opts.nullable && it.schema.nullable === true) { if ($typeSchema && it.opts.coerceTypes) {
if ($typeIsArray) { var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema);
if ($typeSchema.indexOf('null') == -1) $typeSchema = $typeSchema.concat('null'); if ($coerceToTypes) {
} else if ($typeSchema != 'null') {
$typeSchema = [$typeSchema, 'null'];
$typeIsArray = true;
}
}
if ($typeIsArray && $typeSchema.length == 1) {
$typeSchema = $typeSchema[0];
$typeIsArray = false;
}
if (it.schema.$ref && $refKeywords) {
if (it.opts.extendRefs == 'fail') {
throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)');
} else if (it.opts.extendRefs !== true) {
$refKeywords = false;
it.logger.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
}
}
if (it.schema.$comment && it.opts.$comment) {
out += ' ' + (it.RULES.all.$comment.code(it, '$comment'));
}
if ($typeSchema) {
if (it.opts.coerceTypes) {
var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema);
}
var $rulesGroup = it.RULES.types[$typeSchema];
if ($coerceToTypes || $typeIsArray || $rulesGroup === true || ($rulesGroup && !$shouldUseGroup($rulesGroup))) {
var $schemaPath = it.schemaPath + '.type',
$errSchemaPath = it.errSchemaPath + '/type';
var $schemaPath = it.schemaPath + '.type', var $schemaPath = it.schemaPath + '.type',
$errSchemaPath = it.errSchemaPath + '/type', $errSchemaPath = it.errSchemaPath + '/type',
$method = $typeIsArray ? 'checkDataTypes' : 'checkDataType'; $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { '; out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { ';
if ($coerceToTypes) { var $dataType = 'dataType' + $lvl,
var $dataType = 'dataType' + $lvl, $coerced = 'coerced' + $lvl;
$coerced = 'coerced' + $lvl; out += ' var ' + ($dataType) + ' = typeof ' + ($data) + '; ';
out += ' var ' + ($dataType) + ' = typeof ' + ($data) + '; '; if (it.opts.coerceTypes == 'array') {
if (it.opts.coerceTypes == 'array') { out += ' if (' + ($dataType) + ' == \'object\' && Array.isArray(' + ($data) + ')) ' + ($dataType) + ' = \'array\'; ';
out += ' if (' + ($dataType) + ' == \'object\' && Array.isArray(' + ($data) + ')) ' + ($dataType) + ' = \'array\'; '; }
} out += ' var ' + ($coerced) + ' = undefined; ';
out += ' var ' + ($coerced) + ' = undefined; '; var $bracesCoercion = '';
var $bracesCoercion = ''; var arr1 = $coerceToTypes;
var arr1 = $coerceToTypes; if (arr1) {
if (arr1) { var $type, $i = -1,
var $type, $i = -1, l1 = arr1.length - 1;
l1 = arr1.length - 1; while ($i < l1) {
while ($i < l1) { $type = arr1[$i += 1];
$type = arr1[$i += 1]; if ($i) {
if ($i) { out += ' if (' + ($coerced) + ' === undefined) { ';
out += ' if (' + ($coerced) + ' === undefined) { '; $bracesCoercion += '}';
$bracesCoercion += '}';
}
if (it.opts.coerceTypes == 'array' && $type != 'array') {
out += ' if (' + ($dataType) + ' == \'array\' && ' + ($data) + '.length == 1) { ' + ($coerced) + ' = ' + ($data) + ' = ' + ($data) + '[0]; ' + ($dataType) + ' = typeof ' + ($data) + '; } ';
}
if ($type == 'string') {
out += ' if (' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\') ' + ($coerced) + ' = \'\' + ' + ($data) + '; else if (' + ($data) + ' === null) ' + ($coerced) + ' = \'\'; ';
} else if ($type == 'number' || $type == 'integer') {
out += ' if (' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' === null || (' + ($dataType) + ' == \'string\' && ' + ($data) + ' && ' + ($data) + ' == +' + ($data) + ' ';
if ($type == 'integer') {
out += ' && !(' + ($data) + ' % 1)';
}
out += ')) ' + ($coerced) + ' = +' + ($data) + '; ';
} else if ($type == 'boolean') {
out += ' if (' + ($data) + ' === \'false\' || ' + ($data) + ' === 0 || ' + ($data) + ' === null) ' + ($coerced) + ' = false; else if (' + ($data) + ' === \'true\' || ' + ($data) + ' === 1) ' + ($coerced) + ' = true; ';
} else if ($type == 'null') {
out += ' if (' + ($data) + ' === \'\' || ' + ($data) + ' === 0 || ' + ($data) + ' === false) ' + ($coerced) + ' = null; ';
} else if (it.opts.coerceTypes == 'array' && $type == 'array') {
out += ' if (' + ($dataType) + ' == \'string\' || ' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' == null) ' + ($coerced) + ' = [' + ($data) + ']; ';
}
} }
} if (it.opts.coerceTypes == 'array' && $type != 'array') {
out += ' ' + ($bracesCoercion) + ' if (' + ($coerced) + ' === undefined) { '; out += ' if (' + ($dataType) + ' == \'array\' && ' + ($data) + '.length == 1) { ' + ($coerced) + ' = ' + ($data) + ' = ' + ($data) + '[0]; ' + ($dataType) + ' = typeof ' + ($data) + '; } ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
if ($typeIsArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
} }
out += '\' } '; if ($type == 'string') {
if (it.opts.messages !== false) { out += ' if (' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\') ' + ($coerced) + ' = \'\' + ' + ($data) + '; else if (' + ($data) + ' === null) ' + ($coerced) + ' = \'\'; ';
out += ' , message: \'should be '; } else if ($type == 'number' || $type == 'integer') {
if ($typeIsArray) { out += ' if (' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' === null || (' + ($dataType) + ' == \'string\' && ' + ($data) + ' && ' + ($data) + ' == +' + ($data) + ' ';
out += '' + ($typeSchema.join(",")); if ($type == 'integer') {
} else { out += ' && !(' + ($data) + ' % 1)';
out += '' + ($typeSchema);
} }
out += '\' '; out += ')) ' + ($coerced) + ' = +' + ($data) + '; ';
} } else if ($type == 'boolean') {
if (it.opts.verbose) { out += ' if (' + ($data) + ' === \'false\' || ' + ($data) + ' === 0 || ' + ($data) + ' === null) ' + ($coerced) + ' = false; else if (' + ($data) + ' === \'true\' || ' + ($data) + ' === 1) ' + ($coerced) + ' = true; ';
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' '; } else if ($type == 'null') {
out += ' if (' + ($data) + ' === \'\' || ' + ($data) + ' === 0 || ' + ($data) + ' === false) ' + ($coerced) + ' = null; ';
} else if (it.opts.coerceTypes == 'array' && $type == 'array') {
out += ' if (' + ($dataType) + ' == \'string\' || ' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' == null) ' + ($coerced) + ' = [' + ($data) + ']; ';
} }
out += ' } ';
} else {
out += ' {} ';
} }
var __err = out; }
out = $$outStack.pop(); out += ' ' + ($bracesCoercion) + ' if (' + ($coerced) + ' === undefined) { ';
if (!it.compositeRule && $breakOnError) { var $$outStack = $$outStack || [];
/* istanbul ignore if */ $$outStack.push(out);
if (it.async) { out = ''; /* istanbul ignore else */
out += ' throw new ValidationError([' + (__err) + ']); '; if (it.createErrors !== false) {
} else { out += ' { keyword: \'' + ('type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
out += ' validate.errors = [' + (__err) + ']; return false; '; if ($typeIsArray) {
} out += '' + ($typeSchema.join(","));
} else { } else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; out += '' + ($typeSchema);
} }
out += ' } else { '; out += '\' } ';
var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData', if (it.opts.messages !== false) {
$parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty'; out += ' , message: \'should be ';
out += ' ' + ($data) + ' = ' + ($coerced) + '; ';
if (!$dataLvl) {
out += 'if (' + ($parentData) + ' !== undefined)';
}
out += ' ' + ($parentData) + '[' + ($parentDataProperty) + '] = ' + ($coerced) + '; } ';
} else {
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
if ($typeIsArray) { if ($typeIsArray) {
out += '' + ($typeSchema.join(",")); out += '' + ($typeSchema.join(","));
} else { } else {
out += '' + ($typeSchema); out += '' + ($typeSchema);
} }
out += '\' } '; out += '\' ';
if (it.opts.messages !== false) {
out += ' , message: \'should be ';
if ($typeIsArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
}
out += ' } ';
} else {
out += ' {} ';
} }
var __err = out; if (it.opts.verbose) {
out = $$outStack.pop(); out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
if (!it.compositeRule && $breakOnError) { }
/* istanbul ignore if */ out += ' } ';
if (it.async) { } else {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' {} ';
} else { }
out += ' validate.errors = [' + (__err) + ']; return false; '; var __err = out;
} out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; out += ' validate.errors = [' + (__err) + ']; return false; ';
} }
} else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
} }
out += ' } '; out += ' } else { ';
var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
$parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
out += ' ' + ($data) + ' = ' + ($coerced) + '; ';
if (!$dataLvl) {
out += 'if (' + ($parentData) + ' !== undefined)';
}
out += ' ' + ($parentData) + '[' + ($parentDataProperty) + '] = ' + ($coerced) + '; } } ';
}
}
var $refKeywords;
if (it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref'))) {
if (it.opts.extendRefs == 'fail') {
throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"');
} else if (it.opts.extendRefs == 'ignore') {
$refKeywords = false;
console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
} else if (it.opts.extendRefs !== true) {
console.log('$ref: all keywords used in schema at path "' + it.errSchemaPath + '". It will change in the next major version, see issue #260. Use option { extendRefs: true } to keep current behaviour');
} }
} }
if (it.schema.$ref && !$refKeywords) { if (it.schema.$ref && !$refKeywords) {
...@@ -304,7 +174,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -304,7 +174,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
if ($rulesGroup.type) { if ($rulesGroup.type) {
out += ' if (' + (it.util.checkDataType($rulesGroup.type, $data)) + ') { '; out += ' if (' + (it.util.checkDataType($rulesGroup.type, $data)) + ') { ';
} }
if (it.opts.useDefaults) { if (it.opts.useDefaults && !it.compositeRule) {
if ($rulesGroup.type == 'object' && it.schema.properties) { if ($rulesGroup.type == 'object' && it.schema.properties) {
var $schema = it.schema.properties, var $schema = it.schema.properties,
$schemaKeys = Object.keys($schema); $schemaKeys = Object.keys($schema);
...@@ -317,25 +187,13 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -317,25 +187,13 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
var $sch = $schema[$propertyKey]; var $sch = $schema[$propertyKey];
if ($sch.default !== undefined) { if ($sch.default !== undefined) {
var $passData = $data + it.util.getProperty($propertyKey); var $passData = $data + it.util.getProperty($propertyKey);
if (it.compositeRule) { out += ' if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
if (it.opts.strictDefaults) { if (it.opts.useDefaults == 'shared') {
var $defaultMsg = 'default is ignored for: ' + $passData; out += ' ' + (it.useDefault($sch.default)) + ' ';
if (it.opts.strictDefaults === 'log') it.logger.warn($defaultMsg);
else throw new Error($defaultMsg);
}
} else { } else {
out += ' if (' + ($passData) + ' === undefined '; out += ' ' + (JSON.stringify($sch.default)) + ' ';
if (it.opts.useDefaults == 'empty') {
out += ' || ' + ($passData) + ' === null || ' + ($passData) + ' === \'\' ';
}
out += ' ) ' + ($passData) + ' = ';
if (it.opts.useDefaults == 'shared') {
out += ' ' + (it.useDefault($sch.default)) + ' ';
} else {
out += ' ' + (JSON.stringify($sch.default)) + ' ';
}
out += '; ';
} }
out += '; ';
} }
} }
} }
...@@ -348,25 +206,13 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -348,25 +206,13 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
$sch = arr4[$i += 1]; $sch = arr4[$i += 1];
if ($sch.default !== undefined) { if ($sch.default !== undefined) {
var $passData = $data + '[' + $i + ']'; var $passData = $data + '[' + $i + ']';
if (it.compositeRule) { out += ' if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
if (it.opts.strictDefaults) { if (it.opts.useDefaults == 'shared') {
var $defaultMsg = 'default is ignored for: ' + $passData; out += ' ' + (it.useDefault($sch.default)) + ' ';
if (it.opts.strictDefaults === 'log') it.logger.warn($defaultMsg);
else throw new Error($defaultMsg);
}
} else { } else {
out += ' if (' + ($passData) + ' === undefined '; out += ' ' + (JSON.stringify($sch.default)) + ' ';
if (it.opts.useDefaults == 'empty') {
out += ' || ' + ($passData) + ' === null || ' + ($passData) + ' === \'\' ';
}
out += ' ) ' + ($passData) + ' = ';
if (it.opts.useDefaults == 'shared') {
out += ' ' + (it.useDefault($sch.default)) + ' ';
} else {
out += ' ' + (JSON.stringify($sch.default)) + ' ';
}
out += '; ';
} }
out += '; ';
} }
} }
} }
...@@ -379,12 +225,9 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -379,12 +225,9 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
while (i5 < l5) { while (i5 < l5) {
$rule = arr5[i5 += 1]; $rule = arr5[i5 += 1];
if ($shouldUseRule($rule)) { if ($shouldUseRule($rule)) {
var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); out += ' ' + ($rule.code(it, $rule.keyword)) + ' ';
if ($code) { if ($breakOnError) {
out += ' ' + ($code) + ' '; $closingBraces1 += '}';
if ($breakOnError) {
$closingBraces1 += '}';
}
} }
} }
} }
...@@ -396,6 +239,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -396,6 +239,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
if ($rulesGroup.type) { if ($rulesGroup.type) {
out += ' } '; out += ' } ';
if ($typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes) { if ($typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes) {
var $typeChecked = true;
out += ' else { '; out += ' else { ';
var $schemaPath = it.schemaPath + '.type', var $schemaPath = it.schemaPath + '.type',
$errSchemaPath = it.errSchemaPath + '/type'; $errSchemaPath = it.errSchemaPath + '/type';
...@@ -403,7 +247,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -403,7 +247,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
$$outStack.push(out); $$outStack.push(out);
out = ''; /* istanbul ignore else */ out = ''; /* istanbul ignore else */
if (it.createErrors !== false) { if (it.createErrors !== false) {
out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \''; out += ' { keyword: \'' + ('type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
if ($typeIsArray) { if ($typeIsArray) {
out += '' + ($typeSchema.join(",")); out += '' + ($typeSchema.join(","));
} else { } else {
...@@ -428,8 +272,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -428,8 +272,7 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
} }
var __err = out; var __err = out;
out = $$outStack.pop(); out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); '; out += ' throw new ValidationError([' + (__err) + ']); ';
} else { } else {
...@@ -455,40 +298,78 @@ module.exports = function generate_validate(it, $keyword, $ruleType) { ...@@ -455,40 +298,78 @@ module.exports = function generate_validate(it, $keyword, $ruleType) {
} }
} }
} }
if ($typeSchema && !$typeChecked && !$coerceToTypes) {
var $schemaPath = it.schemaPath + '.type',
$errSchemaPath = it.errSchemaPath + '/type',
$method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
if ($typeIsArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should be ';
if ($typeIsArray) {
out += '' + ($typeSchema.join(","));
} else {
out += '' + ($typeSchema);
}
out += '\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
}
out += ' } ';
} else {
out += ' {} ';
}
var __err = out;
out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError([' + (__err) + ']); ';
} else {
out += ' validate.errors = [' + (__err) + ']; return false; ';
}
} else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
}
out += ' }';
}
if ($breakOnError) { if ($breakOnError) {
out += ' ' + ($closingBraces2) + ' '; out += ' ' + ($closingBraces2) + ' ';
} }
if ($top) { if ($top) {
if ($async) { if ($async) {
out += ' if (errors === 0) return data; '; out += ' if (errors === 0) return true; ';
out += ' else throw new ValidationError(vErrors); '; out += ' else throw new ValidationError(vErrors); ';
} else { } else {
out += ' validate.errors = vErrors; '; out += ' validate.errors = vErrors; ';
out += ' return errors === 0; '; out += ' return errors === 0; ';
} }
out += ' }; return validate;'; out += ' }); return validate;';
} else { } else {
out += ' var ' + ($valid) + ' = errors === errs_' + ($lvl) + ';'; out += ' var ' + ($valid) + ' = errors === errs_' + ($lvl) + ';';
} }
out = it.util.cleanUpCode(out); out = it.util.cleanUpCode(out);
if ($top) { if ($top && $breakOnError) {
out = it.util.finalCleanUpCode(out, $async); out = it.util.cleanUpVarErrors(out, $async);
} }
function $shouldUseGroup($rulesGroup) { function $shouldUseGroup($rulesGroup) {
var rules = $rulesGroup.rules; for (var i = 0; i < $rulesGroup.rules.length; i++)
for (var i = 0; i < rules.length; i++) if ($shouldUseRule($rulesGroup.rules[i])) return true;
if ($shouldUseRule(rules[i])) return true;
} }
function $shouldUseRule($rule) { function $shouldUseRule($rule) {
return it.schema[$rule.keyword] !== undefined || ($rule.implements && $ruleImplementsSomeKeyword($rule)); return it.schema[$rule.keyword] !== undefined || ($rule.keyword == 'properties' && (it.schema.additionalProperties === false || typeof it.schema.additionalProperties == 'object' || (it.schema.patternProperties && Object.keys(it.schema.patternProperties).length) || (it.opts.v5 && it.schema.patternGroups && Object.keys(it.schema.patternGroups).length)));
}
function $ruleImplementsSomeKeyword($rule) {
var impl = $rule.implements;
for (var i = 0; i < impl.length; i++)
if (it.schema[impl[i]] !== undefined) return true;
} }
return out; return out;
} }
'use strict'; 'use strict';
var IDENTIFIER = /^[a-z_$][a-z0-9_$-]*$/i; var IDENTIFIER = /^[a-z_$][a-z0-9_$\-]*$/i;
var customRuleCode = require('./dotjs/custom'); var customRuleCode = require('./dotjs/custom');
var definitionSchema = require('./definition_schema');
module.exports = { module.exports = {
add: addKeyword, add: addKeyword,
get: getKeyword, get: getKeyword,
remove: removeKeyword, remove: removeKeyword
validate: validateKeyword
}; };
/** /**
* Define custom keyword * Define custom keyword
* @this Ajv * @this Ajv
* @param {String} keyword custom keyword, should be unique (including different from all standard, custom and macro keywords). * @param {String} keyword custom keyword, should be unique (including different from all standard, custom and macro keywords).
* @param {Object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`. * @param {Object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`.
* @return {Ajv} this for method chaining
*/ */
function addKeyword(keyword, definition) { function addKeyword(keyword, definition) {
/* jshint validthis: true */ /* jshint validthis: true */
/* eslint no-shadow: 0 */ /* eslint no-shadow: 0 */
var RULES = this.RULES; var RULES = this.RULES;
if (RULES.keywords[keyword]) if (RULES.keywords[keyword])
throw new Error('Keyword ' + keyword + ' is already defined'); throw new Error('Keyword ' + keyword + ' is already defined');
...@@ -30,23 +27,30 @@ function addKeyword(keyword, definition) { ...@@ -30,23 +27,30 @@ function addKeyword(keyword, definition) {
throw new Error('Keyword ' + keyword + ' is not a valid identifier'); throw new Error('Keyword ' + keyword + ' is not a valid identifier');
if (definition) { if (definition) {
this.validateKeyword(definition, true); if (definition.macro && definition.valid !== undefined)
throw new Error('"valid" option cannot be used with macro keywords');
var dataType = definition.type; var dataType = definition.type;
if (Array.isArray(dataType)) { if (Array.isArray(dataType)) {
for (var i=0; i<dataType.length; i++) var i, len = dataType.length;
_addRule(keyword, dataType[i], definition); for (i=0; i<len; i++) checkDataType(dataType[i]);
for (i=0; i<len; i++) _addRule(keyword, dataType[i], definition);
} else { } else {
if (dataType) checkDataType(dataType);
_addRule(keyword, dataType, definition); _addRule(keyword, dataType, definition);
} }
var $data = definition.$data === true && this._opts.v5;
if ($data && !definition.validate)
throw new Error('$data support: "validate" function is not defined');
var metaSchema = definition.metaSchema; var metaSchema = definition.metaSchema;
if (metaSchema) { if (metaSchema) {
if (definition.$data && this._opts.$data) { if ($data) {
metaSchema = { metaSchema = {
anyOf: [ anyOf: [
metaSchema, metaSchema,
{ '$ref': 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/data.json#' } { '$ref': 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#/definitions/$data' }
] ]
}; };
} }
...@@ -76,14 +80,16 @@ function addKeyword(keyword, definition) { ...@@ -76,14 +80,16 @@ function addKeyword(keyword, definition) {
keyword: keyword, keyword: keyword,
definition: definition, definition: definition,
custom: true, custom: true,
code: customRuleCode, code: customRuleCode
implements: definition.implements
}; };
ruleGroup.rules.push(rule); ruleGroup.rules.push(rule);
RULES.custom[keyword] = rule; RULES.custom[keyword] = rule;
} }
return this;
function checkDataType(dataType) {
if (!RULES.types[dataType]) throw new Error('Unknown type ' + dataType);
}
} }
...@@ -104,7 +110,6 @@ function getKeyword(keyword) { ...@@ -104,7 +110,6 @@ function getKeyword(keyword) {
* Remove keyword * Remove keyword
* @this Ajv * @this Ajv
* @param {String} keyword pre-defined or custom keyword. * @param {String} keyword pre-defined or custom keyword.
* @return {Ajv} this for method chaining
*/ */
function removeKeyword(keyword) { function removeKeyword(keyword) {
/* jshint validthis: true */ /* jshint validthis: true */
...@@ -121,26 +126,4 @@ function removeKeyword(keyword) { ...@@ -121,26 +126,4 @@ function removeKeyword(keyword) {
} }
} }
} }
return this;
}
/**
* Validate keyword definition
* @this Ajv
* @param {Object} definition keyword definition object.
* @param {Boolean} throwError true to throw exception if definition is invalid
* @return {boolean} validation result
*/
function validateKeyword(definition, throwError) {
validateKeyword.errors = null;
var v = this._validateKeyword = this._validateKeyword
|| this.compile(definitionSchema, true);
if (v(definition)) return true;
validateKeyword.errors = v.errors;
if (throwError)
throw new Error('custom keyword definition is invalid: ' + this.errorsText(v.errors));
else
return false;
} }
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/data.json#",
"description": "Meta-schema for $data reference (JSON Schema extension proposal)",
"type": "object",
"required": [ "$data" ],
"properties": {
"$data": {
"type": "string",
"anyOf": [
{ "format": "relative-json-pointer" },
{ "format": "json-pointer" }
]
}
},
"additionalProperties": false
}
...@@ -28,10 +28,12 @@ ...@@ -28,10 +28,12 @@
"type": "object", "type": "object",
"properties": { "properties": {
"id": { "id": {
"type": "string" "type": "string",
"format": "uri"
}, },
"$schema": { "$schema": {
"type": "string" "type": "string",
"format": "uri"
}, },
"title": { "title": {
"type": "string" "type": "string"
...@@ -135,7 +137,6 @@ ...@@ -135,7 +137,6 @@
} }
] ]
}, },
"format": { "type": "string" },
"allOf": { "$ref": "#/definitions/schemaArray" }, "allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" }, "anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" }, "oneOf": { "$ref": "#/definitions/schemaArray" },
......
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$id": "http://json-schema.org/draft-06/schema#",
"title": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{ "$ref": "#/definitions/nonNegativeInteger" },
{ "default": 0 }
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"default": []
}
},
"type": ["object", "boolean"],
"properties": {
"$id": {
"type": "string",
"format": "uri-reference"
},
"$schema": {
"type": "string",
"format": "uri"
},
"$ref": {
"type": "string",
"format": "uri-reference"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"examples": {
"type": "array",
"items": {}
},
"multipleOf": {
"type": "number",
"exclusiveMinimum": 0
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "number"
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "number"
},
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": { "$ref": "#" },
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": {}
},
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"contains": { "$ref": "#" },
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": { "$ref": "#" },
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"propertyNames": { "$ref": "#" },
"const": {},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"default": {}
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{ "$ref": "#/definitions/nonNegativeInteger" },
{ "default": 0 }
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"default": []
}
},
"type": ["object", "boolean"],
"properties": {
"$id": {
"type": "string",
"format": "uri-reference"
},
"$schema": {
"type": "string",
"format": "uri"
},
"$ref": {
"type": "string",
"format": "uri-reference"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": true,
"readOnly": {
"type": "boolean",
"default": false
},
"examples": {
"type": "array",
"items": true
},
"multipleOf": {
"type": "number",
"exclusiveMinimum": 0
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "number"
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "number"
},
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": { "$ref": "#" },
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": true
},
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"contains": { "$ref": "#" },
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": { "$ref": "#" },
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"propertyNames": { "format": "regex" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"propertyNames": { "$ref": "#" },
"const": true,
"enum": {
"type": "array",
"items": true,
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"contentMediaType": { "type": "string" },
"contentEncoding": { "type": "string" },
"if": {"$ref": "#"},
"then": {"$ref": "#"},
"else": {"$ref": "#"},
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"default": true
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-secure.json#",
"title": "Meta-schema for the security assessment of JSON Schemas",
"description": "If a JSON Schema fails validation against this meta-schema, it may be unsafe to validate untrusted data",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": {"$ref": "#"}
}
},
"dependencies": {
"patternProperties": {
"description": "prevent slow validation of large property names",
"required": ["propertyNames"],
"properties": {
"propertyNames": {
"required": ["maxLength"]
}
}
},
"uniqueItems": {
"description": "prevent slow validation of large non-scalar arrays",
"if": {
"properties": {
"uniqueItems": {"const": true},
"items": {
"properties": {
"type": {
"anyOf": [
{
"enum": ["object", "array"]
},
{
"type": "array",
"contains": {"enum": ["object", "array"]}
}
]
}
}
}
}
},
"then": {
"required": ["maxItems"]
}
},
"pattern": {
"description": "prevent slow pattern matching of large strings",
"required": ["maxLength"]
},
"format": {
"description": "prevent slow format validation of large strings",
"required": ["maxLength"]
}
},
"properties": {
"additionalItems": {"$ref": "#"},
"additionalProperties": {"$ref": "#"},
"dependencies": {
"additionalProperties": {
"anyOf": [
{"type": "array"},
{"$ref": "#"}
]
}
},
"items": {
"anyOf": [
{"$ref": "#"},
{"$ref": "#/definitions/schemaArray"}
]
},
"definitions": {
"additionalProperties": {"$ref": "#"}
},
"patternProperties": {
"additionalProperties": {"$ref": "#"}
},
"properties": {
"additionalProperties": {"$ref": "#"}
},
"if": {"$ref": "#"},
"then": {"$ref": "#"},
"else": {"$ref": "#"},
"allOf": {"$ref": "#/definitions/schemaArray"},
"anyOf": {"$ref": "#/definitions/schemaArray"},
"oneOf": {"$ref": "#/definitions/schemaArray"},
"not": {"$ref": "#"},
"contains": {"$ref": "#"},
"propertyNames": {"$ref": "#"}
}
}
{ {
"_from": "ajv@^6.5.5", "_from": "ajv@^4.9.1",
"_id": "ajv@6.11.0", "_id": "ajv@4.11.8",
"_inBundle": false, "_inBundle": false,
"_integrity": "sha512-nCprB/0syFYy9fVYU1ox1l2KN8S9I+tziH8D4zdZuLT3N6RMlGSGt5FSTpAiHB/Whv8Qs1cWHma1aMKZyaHRKA==", "_integrity": "sha1-gv+wKynmYq5TvcIK8VlHcGc5xTY=",
"_location": "/har-validator/ajv", "_location": "/har-validator/ajv",
"_phantomChildren": {}, "_phantomChildren": {},
"_requested": { "_requested": {
"type": "range", "type": "range",
"registry": true, "registry": true,
"raw": "ajv@^6.5.5", "raw": "ajv@^4.9.1",
"name": "ajv", "name": "ajv",
"escapedName": "ajv", "escapedName": "ajv",
"rawSpec": "^6.5.5", "rawSpec": "^4.9.1",
"saveSpec": null, "saveSpec": null,
"fetchSpec": "^6.5.5" "fetchSpec": "^4.9.1"
}, },
"_requiredBy": [ "_requiredBy": [
"/har-validator" "/har-validator"
], ],
"_resolved": "https://registry.npmjs.org/ajv/-/ajv-6.11.0.tgz", "_resolved": "https://registry.npmjs.org/ajv/-/ajv-4.11.8.tgz",
"_shasum": "c3607cbc8ae392d8a5a536f25b21f8e5f3f87fe9", "_shasum": "82ffb02b29e662ae53bdc20af15947706739c536",
"_spec": "ajv@^6.5.5", "_spec": "ajv@^4.9.1",
"_where": "C:\\Work\\OneDrive - bwstaff\\M4_Lab\\TV3\\NewVersion01\\dev\\node_modules\\har-validator", "_where": "C:\\Work\\OneDrive - bwstaff\\M4_Lab\\Main\\02_Plattform_Main\\m4labplatform\\node_modules\\har-validator",
"author": { "author": {
"name": "Evgeny Poberezkin" "name": "Evgeny Poberezkin"
}, },
...@@ -30,39 +30,40 @@ ...@@ -30,39 +30,40 @@
}, },
"bundleDependencies": false, "bundleDependencies": false,
"dependencies": { "dependencies": {
"fast-deep-equal": "^3.1.1", "co": "^4.6.0",
"fast-json-stable-stringify": "^2.0.0", "json-stable-stringify": "^1.0.1"
"json-schema-traverse": "^0.4.1",
"uri-js": "^4.2.2"
}, },
"deprecated": false, "deprecated": false,
"description": "Another JSON Schema Validator", "description": "Another JSON Schema Validator",
"devDependencies": { "devDependencies": {
"ajv-async": "^1.0.0", "bluebird": "^3.1.5",
"bluebird": "^3.5.3", "brfs": "^1.4.3",
"brfs": "^2.0.0", "browserify": "^14.1.0",
"browserify": "^16.2.0", "chai": "^3.5.0",
"chai": "^4.0.1", "coveralls": "^2.11.4",
"coveralls": "^3.0.1", "del-cli": "^0.2.1",
"del-cli": "^3.0.0",
"dot": "^1.0.3", "dot": "^1.0.3",
"eslint": "^6.0.0", "eslint": "^3.2.2",
"gh-pages-generator": "^0.2.3", "gh-pages-generator": "^0.2.0",
"glob": "^7.0.0", "glob": "^7.0.0",
"if-node-version": "^1.0.0", "if-node-version": "^1.0.0",
"js-beautify": "^1.7.3", "js-beautify": "^1.5.6",
"jshint": "^2.10.2", "jshint": "^2.8.0",
"json-schema-test": "^2.0.0", "json-schema-test": "^1.1.1",
"karma": "^4.0.1", "karma": "^1.0.0",
"karma-chrome-launcher": "^3.0.0", "karma-chrome-launcher": "^2.0.0",
"karma-mocha": "^1.1.1", "karma-mocha": "^1.1.1",
"karma-sauce-launcher": "^2.0.0", "karma-phantomjs-launcher": "^1.0.0",
"mocha": "^6.0.0", "karma-sauce-launcher": "^1.1.0",
"nyc": "^15.0.0", "mocha": "^3.0.0",
"nodent": "^3.0.17",
"nyc": "^10.0.0",
"phantomjs-prebuilt": "^2.1.4",
"pre-commit": "^1.1.1", "pre-commit": "^1.1.1",
"regenerator": "0.9.7",
"require-globify": "^1.3.0", "require-globify": "^1.3.0",
"typescript": "^2.8.3", "typescript": "^2.0.3",
"uglify-js": "^3.6.9", "uglify-js": "^2.6.1",
"watch": "^1.0.0" "watch": "^1.0.0"
}, },
"files": [ "files": [
...@@ -96,30 +97,35 @@ ...@@ -96,30 +97,35 @@
"text-summary" "text-summary"
] ]
}, },
"publishConfig": {
"tag": "4.x"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/epoberezkin/ajv.git" "url": "git+https://github.com/epoberezkin/ajv.git"
}, },
"scripts": { "scripts": {
"build": "del-cli lib/dotjs/*.js \"!lib/dotjs/index.js\" && node scripts/compile-dots.js", "build": "del-cli lib/dotjs/*.js && node scripts/compile-dots.js",
"bundle": "del-cli dist && node ./scripts/bundle.js . Ajv pure_getters", "bundle": "node ./scripts/bundle.js . Ajv pure_getters",
"bundle-all": "del-cli dist && npm run bundle && npm run bundle-regenerator && npm run bundle-nodent",
"bundle-beautify": "node ./scripts/bundle.js js-beautify", "bundle-beautify": "node ./scripts/bundle.js js-beautify",
"eslint": "eslint lib/{compile/,}*.js spec/{**/,}*.js scripts --ignore-pattern spec/JSON-Schema-Test-Suite", "bundle-nodent": "node ./scripts/bundle.js nodent",
"jshint": "jshint lib/{compile/,}*.js", "bundle-regenerator": "node ./scripts/bundle.js regenerator",
"lint": "npm run jshint && npm run eslint", "eslint": "if-node-version \">=4\" eslint lib/*.js lib/compile/*.js spec scripts",
"prepublish": "npm run build && npm run bundle", "jshint": "jshint lib/*.js lib/**/*.js --exclude lib/dotjs/**/*",
"test": "npm run lint && npm run build && npm run test-all", "prepublish": "npm run build && npm run bundle-all",
"test-all": "npm run test-cov && if-node-version 10 npm run test-browser", "test": "npm run jshint && npm run eslint && npm run test-ts && npm run build && npm run test-cov && if-node-version 4 npm run test-browser",
"test-browser": "del-cli .browser && npm run bundle && scripts/prepare-tests && npm run test-karma", "test-browser": "del-cli .browser && npm run bundle-all && scripts/prepare-tests && npm run test-karma",
"test-cov": "nyc npm run test-spec", "test-cov": "nyc npm run test-spec",
"test-debug": "npm run test-spec -- --inspect-brk", "test-debug": "mocha spec/*.spec.js --debug-brk -R spec",
"test-fast": "AJV_FAST_TEST=true npm run test-spec", "test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-karma": "karma start", "test-karma": "karma start --single-run --browsers PhantomJS",
"test-spec": "mocha spec/{**/,}*.spec.js -R spec", "test-spec": "mocha spec/*.spec.js -R spec",
"test-ts": "tsc --target ES5 --noImplicitAny --noEmit spec/typescript/index.ts", "test-ts": "tsc --target ES5 --noImplicitAny lib/ajv.d.ts",
"watch": "watch \"npm run build\" ./lib/dot" "watch": "watch 'npm run build' ./lib/dot"
}, },
"tonicExampleFilename": ".tonic_example.js", "tonicExampleFilename": ".tonic_example.js",
"typings": "lib/ajv.d.ts", "typings": "lib/ajv.d.ts",
"version": "6.11.0" "version": "4.11.8",
"webpack": "dist/ajv.bundle.js"
} }
...@@ -29,6 +29,8 @@ browserify(bOpts) ...@@ -29,6 +29,8 @@ browserify(bOpts)
} }
var outputFile = path.join(distDir, json.name); var outputFile = path.join(distDir, json.name);
var outputBundle = outputFile + '.bundle.js';
fs.writeFileSync(outputBundle, buf);
var uglifyOpts = { var uglifyOpts = {
warnings: true, warnings: true,
compress: {}, compress: {},
...@@ -38,24 +40,15 @@ browserify(bOpts) ...@@ -38,24 +40,15 @@ browserify(bOpts)
}; };
if (compress) { if (compress) {
var compressOpts = compress.split(','); var compressOpts = compress.split(',');
for (var i=0, il = compressOpts.length; i<il; ++i) { for (var i=0; i<compressOpts.length; ++i) {
var pair = compressOpts[i].split('='); var pair = compressOpts[i].split('=');
uglifyOpts.compress[pair[0]] = pair.length < 1 || pair[1] != 'false'; uglifyOpts.compress[pair[0]] = pair.length < 1 || pair[1] != 'false';
} }
} }
if (standalone) { if (standalone) uglifyOpts.outSourceMap = json.name + '.min.js.map';
uglifyOpts.sourceMap = {
filename: json.name + '.min.js',
url: json.name + '.min.js.map'
};
}
var result = uglify.minify(buf.toString(), uglifyOpts); var result = uglify.minify(outputBundle, uglifyOpts);
fs.writeFileSync(outputFile + '.min.js', result.code); fs.writeFileSync(outputFile + '.min.js', result.code);
if (result.map) fs.writeFileSync(outputFile + '.min.js.map', result.map); if (result.map) fs.writeFileSync(outputFile + '.min.js.map', result.map);
if (standalone) fs.writeFileSync(outputFile + '.bundle.js', buf); if (!standalone) fs.unlinkSync(outputBundle);
if (result.warnings) {
for (var j=0, jl = result.warnings.length; j<jl; ++j)
console.warn('UglifyJS warning:', result.warnings[j]);
}
}); });
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment