Commit bf137ad8 authored by Patrick's avatar Patrick
Browse files

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

Showing with 344 additions and 773 deletions
+344 -773
...@@ -23,10 +23,8 @@ ...@@ -23,10 +23,8 @@
{{ {{
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 || {}
...@@ -42,24 +40,27 @@ ...@@ -42,24 +40,27 @@
, $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) if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired)
var $requiredHash = it.util.toHash($required); var $requiredHash = it.util.toHash($required);
if (it.opts.v5) {
var $pgProperties = it.schema.patternGroups || {}
, $pgPropertyKeys = Object.keys($pgProperties);
}
}} }}
var {{=$errs}} = errors; var {{=$errs}} = errors;
var {{=$nextValid}} = true; var {{=$nextValid}} = true;
{{? $ownProperties }}
var {{=$dataProperties}} = undefined;
{{?}}
{{? $checkAdditional }} {{? $checkAdditional }}
{{# def.iterateProperties }} for (var {{=$key}} in {{=$data}}) {
{{# def.checkOwnProperty }}
{{? $someProperties }} {{? $someProperties }}
var isAdditional{{=$lvl}} = !(false var isAdditional{{=$lvl}} = !(false
{{? $schemaKeys.length }} {{? $schemaKeys.length }}
{{? $schemaKeys.length > 8 }} {{? $schemaKeys.length > 5 }}
|| validate.schema{{=$schemaPath}}.hasOwnProperty({{=$key}}) || validate.schema{{=$schemaPath}}[{{=$key}}]
{{??}} {{??}}
{{~ $schemaKeys:$propertyKey }} {{~ $schemaKeys:$propertyKey }}
|| {{=$key}} == {{= it.util.toQuotedString($propertyKey) }} || {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}
...@@ -71,6 +72,11 @@ var {{=$nextValid}} = true; ...@@ -71,6 +72,11 @@ var {{=$nextValid}} = true;
|| {{= it.usePattern($pProperty) }}.test({{=$key}}) || {{= it.usePattern($pProperty) }}.test({{=$key}})
{{~}} {{~}}
{{?}} {{?}}
{{? it.opts.v5 && $pgPropertyKeys && $pgPropertyKeys.length }}
{{~ $pgPropertyKeys:$pgProperty:$i }}
|| {{= it.usePattern($pgProperty) }}.test({{=$key}})
{{~}}
{{?}}
); );
if (isAdditional{{=$lvl}}) { if (isAdditional{{=$lvl}}) {
...@@ -164,7 +170,7 @@ var {{=$nextValid}} = true; ...@@ -164,7 +170,7 @@ var {{=$nextValid}} = true;
{{= $code }} {{= $code }}
{{??}} {{??}}
{{? $requiredHash && $requiredHash[$propertyKey] }} {{? $requiredHash && $requiredHash[$propertyKey] }}
if ({{# def.noPropertyInData }}) { if ({{=$useData}} === undefined) {
{{=$nextValid}} = false; {{=$nextValid}} = false;
{{ {{
var $currentErrorPath = it.errorPath var $currentErrorPath = it.errorPath
...@@ -181,15 +187,11 @@ var {{=$nextValid}} = true; ...@@ -181,15 +187,11 @@ var {{=$nextValid}} = true;
} else { } else {
{{??}} {{??}}
{{? $breakOnError }} {{? $breakOnError }}
if ({{# def.noPropertyInData }}) { if ({{=$useData}} === undefined) {
{{=$nextValid}} = true; {{=$nextValid}} = true;
} else { } else {
{{??}} {{??}}
if ({{=$useData}} !== undefined if ({{=$useData}} !== undefined) {
{{? $ownProperties }}
&& {{# def.isOwnProperty }}
{{?}}
) {
{{?}} {{?}}
{{?}} {{?}}
...@@ -202,20 +204,62 @@ var {{=$nextValid}} = true; ...@@ -202,20 +204,62 @@ var {{=$nextValid}} = true;
{{~}} {{~}}
{{?}} {{?}}
{{? $pPropertyKeys.length }} {{~ $pPropertyKeys:$pProperty }}
{{~ $pPropertyKeys:$pProperty }} {{ var $sch = $pProperties[$pProperty]; }}
{{ var $sch = $pProperties[$pProperty]; }}
{{? {{# def.nonEmptySchema:$sch}} }}
{{
$it.schema = $sch;
$it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
$it.errSchemaPath = it.errSchemaPath + '/patternProperties/'
+ it.util.escapeFragment($pProperty);
}}
for (var {{=$key}} in {{=$data}}) {
{{# def.checkOwnProperty }}
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;
}}
{{# def.generateSubschemaCode }}
{{# def.optimizeValidate }}
{{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
}
{{? $breakOnError }} else {{=$nextValid}} = true; {{?}}
}
{{# def.ifResultValid }}
{{?}} {{ /* def.nonEmptySchema */ }}
{{~}}
{{? it.opts.v5 }}
{{~ $pgPropertyKeys:$pgProperty }}
{{
var $pgSchema = $pgProperties[$pgProperty]
, $sch = $pgSchema.schema;
}}
{{? {{# def.nonEmptySchema:$sch}} }} {{? {{# def.nonEmptySchema:$sch}} }}
{{ {{
$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.errSchemaPath = it.errSchemaPath + '/patternGroups/'
+ it.util.escapeFragment($pProperty); + it.util.escapeFragment($pgProperty)
+ '/schema';
}} }}
{{# def.iterateProperties }} var pgPropCount{{=$lvl}} = 0;
if ({{= it.usePattern($pProperty) }}.test({{=$key}})) {
for (var {{=$key}} in {{=$data}}) {
{{# def.checkOwnProperty }}
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 + ']';
...@@ -231,6 +275,37 @@ var {{=$nextValid}} = true; ...@@ -231,6 +275,37 @@ var {{=$nextValid}} = true;
} }
{{# def.ifResultValid }} {{# def.ifResultValid }}
{{
var $pgMin = $pgSchema.minimum
, $pgMax = $pgSchema.maximum;
}}
{{? $pgMin !== undefined || $pgMax !== undefined }}
var {{=$valid}} = true;
{{ var $currErrSchemaPath = $errSchemaPath; }}
{{? $pgMin !== undefined }}
{{ var $limit = $pgMin, $reason = 'minimum', $moreOrLess = 'less'; }}
{{=$valid}} = pgPropCount{{=$lvl}} >= {{=$pgMin}};
{{ $errSchemaPath = it.errSchemaPath + '/patternGroups/minimum'; }}
{{# def.checkError:'patternGroups' }}
{{? $pgMax !== undefined }}
else
{{?}}
{{?}}
{{? $pgMax !== undefined }}
{{ var $limit = $pgMax, $reason = 'maximum', $moreOrLess = 'more'; }}
{{=$valid}} = pgPropCount{{=$lvl}} <= {{=$pgMax}};
{{ $errSchemaPath = it.errSchemaPath + '/patternGroups/maximum'; }}
{{# def.checkError:'patternGroups' }}
{{?}}
{{ $errSchemaPath = $currErrSchemaPath; }}
{{# def.ifValid }}
{{?}}
{{?}} {{ /* def.nonEmptySchema */ }} {{?}} {{ /* def.nonEmptySchema */ }}
{{~}} {{~}}
{{?}} {{?}}
......
{{# def.definitions }}
{{# def.errors }}
{{# def.setupKeyword }}
{{# def.setupNextLevel }}
var {{=$errs}} = errors;
{{? {{# def.nonEmptySchema:$schema }} }}
{{
$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;
}}
{{? $ownProperties }}
var {{=$dataProperties}} = undefined;
{{?}}
{{# def.iterateProperties }}
var startErrs{{=$lvl}} = errors;
{{ var $passData = $key; }}
{{# def.setCompositeRule }}
{{# def.generateSubschemaCode }}
{{# def.optimizeValidate }}
{{# def.resetCompositeRule }}
if (!{{=$nextValid}}) {
for (var {{=$i}}=startErrs{{=$lvl}}; {{=$i}}<errors; {{=$i}}++) {
vErrors[{{=$i}}].propertyName = {{=$key}};
}
{{# def.extraError:'propertyNames' }}
{{? $breakOnError }} break; {{?}}
}
}
{{?}}
{{? $breakOnError }}
{{= $closingBraces }}
if ({{=$errs}} == errors) {
{{?}}
{{# def.cleanUp }}
...@@ -25,16 +25,21 @@ ...@@ -25,16 +25,21 @@
{{??}} {{??}}
{{ var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot); }} {{ var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot); }}
{{? $refVal === undefined }} {{? $refVal === undefined }}
{{ var $message = it.MissingRefError.message(it.baseId, $schema); }} {{ var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId; }}
{{? it.opts.missingRefs == 'fail' }} {{? it.opts.missingRefs == 'fail' }}
{{ it.logger.error($message); }} {{ console.log($message); }}
{{# def.error:'$ref' }} {{# def.error:'$ref' }}
{{? $breakOnError }} if (false) { {{?}} {{? $breakOnError }} if (false) { {{?}}
{{?? it.opts.missingRefs == 'ignore' }} {{?? it.opts.missingRefs == 'ignore' }}
{{ it.logger.warn($message); }} {{ console.log($message); }}
{{? $breakOnError }} if (true) { {{?}} {{? $breakOnError }} if (true) { {{?}}
{{??}} {{??}}
{{ 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;
}}
{{?}} {{?}}
{{?? $refVal.inline }} {{?? $refVal.inline }}
{{# def.setupNextLevel }} {{# def.setupNextLevel }}
...@@ -50,7 +55,7 @@ ...@@ -50,7 +55,7 @@
{{?}} {{?}}
{{??}} {{??}}
{{ {{
$async = $refVal.$async === true || (it.async && $refVal.$async !== false); $async = $refVal.$async === true;
$refCode = $refVal.code; $refCode = $refVal.code;
}} }}
{{?}} {{?}}
...@@ -63,16 +68,12 @@ ...@@ -63,16 +68,12 @@
{{? $async }} {{? $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'); }}
{{? $breakOnError }} var {{=$valid}}; {{?}} try { {{? $breakOnError }}var {{=$valid}} ={{?}} {{=it.yieldAwait}} {{=__callValidate}}; }
try { catch (e) {
await {{=__callValidate}};
{{? $breakOnError }} {{=$valid}} = true; {{?}}
} catch (e) {
if (!(e instanceof ValidationError)) throw e; if (!(e instanceof ValidationError)) throw e;
if (vErrors === null) vErrors = e.errors; if (vErrors === null) vErrors = e.errors;
else vErrors = vErrors.concat(e.errors); else vErrors = vErrors.concat(e.errors);
errors = vErrors.length; errors = vErrors.length;
{{? $breakOnError }} {{=$valid}} = false; {{?}}
} }
{{? $breakOnError }} if ({{=$valid}}) { {{?}} {{? $breakOnError }} if ({{=$valid}}) { {{?}}
{{??}} {{??}}
......
...@@ -22,11 +22,6 @@ ...@@ -22,11 +22,6 @@
#}} #}}
{{## def.isRequiredOwnProperty:
Object.prototype.hasOwnProperty.call({{=$data}}, {{=$vSchema}}[{{=$i}}])
#}}
{{? !$isData }} {{? !$isData }}
{{? $schema.length < it.opts.loopRequired && {{? $schema.length < it.opts.loopRequired &&
it.schema.properties && Object.keys(it.schema.properties).length }} it.schema.properties && Object.keys(it.schema.properties).length }}
...@@ -46,8 +41,7 @@ ...@@ -46,8 +41,7 @@
{{? $isData || $required.length }} {{? $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;
}} }}
{{? $breakOnError }} {{? $breakOnError }}
...@@ -59,10 +53,7 @@ ...@@ -59,10 +53,7 @@
{{?$isData}}{{# def.check$dataIsArray }}{{?}} {{?$isData}}{{# def.check$dataIsArray }}{{?}}
for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) { for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) {
{{=$valid}} = {{=$data}}[{{=$vSchema}}[{{=$i}}]] !== undefined {{=$valid}} = {{=$data}}[{{=$vSchema}}[{{=$i}}]] !== undefined;
{{? $ownProperties }}
&& {{# def.isRequiredOwnProperty }}
{{?}};
if (!{{=$valid}}) break; if (!{{=$valid}}) break;
} }
...@@ -85,17 +76,14 @@ ...@@ -85,17 +76,14 @@
{{?}} {{?}}
for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) { for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) {
if ({{=$data}}[{{=$vSchema}}[{{=$i}}]] === undefined if ({{=$data}}[{{=$vSchema}}[{{=$i}}]] === undefined) {
{{? $ownProperties }}
|| !{{# def.isRequiredOwnProperty }}
{{?}}) {
{{# def.addError:'required' }} {{# def.addError:'required' }}
} }
} }
{{? $isData }} } {{?}} {{? $isData }} } {{?}}
{{??}} {{??}}
{{~ $required:$propertyKey }} {{~ $required:$reqProperty }}
{{# def.allErrorsMissingProperty:'required' }} {{# def.allErrorsMissingProperty:'required' }}
{{~}} {{~}}
{{?}} {{?}}
......
...@@ -14,42 +14,18 @@ ...@@ -14,42 +14,18 @@
else { else {
{{?}} {{?}}
var i = {{=$data}}.length var {{=$valid}} = true;
, {{=$valid}} = true if ({{=$data}}.length > 1) {
, j; var i = {{=$data}}.length, j;
if (i > 1) { outer:
{{ for (;i--;) {
var $itemType = it.schema.items && it.schema.items.type for (j = i; j--;) {
, $typeIsArray = Array.isArray($itemType); if (equal({{=$data}}[i], {{=$data}}[j])) {
}}
{{? !$itemType || $itemType == 'object' || $itemType == 'array' ||
($typeIsArray && ($itemType.indexOf('object') >= 0 || $itemType.indexOf('array') >= 0)) }}
outer:
for (;i--;) {
for (j = i; j--;) {
if (equal({{=$data}}[i], {{=$data}}[j])) {
{{=$valid}} = false;
break outer;
}
}
}
{{??}}
var itemIndices = {}, item;
for (;i--;) {
var item = {{=$data}}[i];
{{ var $method = 'checkDataType' + ($typeIsArray ? 's' : ''); }}
if ({{= it.util[$method]($itemType, 'item', true) }}) continue;
{{? $typeIsArray}}
if (typeof item == 'string') item = '"' + item;
{{?}}
if (typeof itemIndices[item] == 'number') {
{{=$valid}} = false; {{=$valid}} = false;
j = itemIndices[item]; break outer;
break;
} }
itemIndices[item] = i;
} }
{{?}} }
} }
{{? $isData }} } {{?}} {{? $isData }} } {{?}}
......
...@@ -14,92 +14,48 @@ ...@@ -14,92 +14,48 @@
* validateRef etc. are defined in the parent scope in index.js * validateRef etc. are defined in the parent scope in index.js
*/ }} */ }}
{{ {{ 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);
}
}
}}
{{? it.isTop }}
var validate = {{?$async}}{{it.async = true;}}async {{?}}function(data, dataPath, parentData, parentDataProperty, rootData) {
'use strict';
{{? $id && (it.opts.sourceCode || it.opts.processCode) }}
{{= '/\*# sourceURL=' + $id + ' */' }}
{{?}}
{{?}}
{{? typeof it.schema == 'boolean' || !($refKeywords || it.schema.$ref) }}
{{ var $keyword = 'false schema'; }}
{{# def.setupKeyword }}
{{? it.schema === false}}
{{? it.isTop}}
{{ $breakOnError = true; }}
{{??}}
var {{=$valid}} = false;
{{?}}
{{# def.error:'false schema' }}
{{??}}
{{? it.isTop}}
{{? $async }}
return data;
{{??}}
validate.errors = null;
return true;
{{?}}
{{??}}
var {{=$valid}} = true;
{{?}}
{{?}}
{{? it.isTop}}
};
return validate;
{{?}}
{{ return out; }}
{{?}}
{{? it.isTop}}
{{? 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) {
var $defaultMsg = 'default is ignored in the schema root';
if (it.opts.strictDefaults === 'log') it.logger.warn($defaultMsg);
else throw new Error($defaultMsg);
}
}} }}
var vErrors = null; {{ /* don't edit, used in replace */ }} var validate =
var errors = 0; {{ /* don't edit, used in replace */ }} {{? $async }}
if (rootData === undefined) rootData = data; {{ /* don't edit, used in replace */ }} {{? $es7 }}
(async function
{{??}}
{{? it.opts.async == 'co*'}}co.wrap{{?}}(function*
{{?}}
{{??}}
(function
{{?}}
(data, dataPath, parentData, parentDataProperty, rootData) {
'use strict';
var vErrors = null; {{ /* don't edit, used in replace */ }}
var errors = 0; {{ /* don't edit, used in replace */ }}
if (rootData === undefined) rootData = data;
{{??}} {{??}}
{{ {{
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');
}} }}
...@@ -116,21 +72,6 @@ ...@@ -116,21 +72,6 @@
var $errorKeyword; 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 ($typeIsArray) {
if ($typeSchema.indexOf('null') == -1)
$typeSchema = $typeSchema.concat('null');
} else if ($typeSchema != 'null') {
$typeSchema = [$typeSchema, 'null'];
$typeIsArray = true;
}
}
if ($typeIsArray && $typeSchema.length == 1) {
$typeSchema = $typeSchema[0];
$typeIsArray = false;
}
}} }}
{{## def.checkType: {{## def.checkType:
...@@ -143,44 +84,29 @@ ...@@ -143,44 +84,29 @@
if ({{= it.util[$method]($typeSchema, $data, true) }}) { if ({{= it.util[$method]($typeSchema, $data, true) }}) {
#}} #}}
{{? it.schema.$ref && $refKeywords }} {{? $typeSchema && it.opts.coerceTypes }}
{{? it.opts.extendRefs == 'fail' }} {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
{{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)'); }} {{? $coerceToTypes }}
{{?? it.opts.extendRefs !== true }} {{# def.checkType }}
{{ {{# def.coerceType }}
$refKeywords = false; }
it.logger.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
}}
{{?}} {{?}}
{{?}} {{?}}
{{? it.schema.$comment && it.opts.$comment }} {{ var $refKeywords; }}
{{= it.RULES.all.$comment.code(it, '$comment') }} {{? it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref')) }}
{{?}} {{? it.opts.extendRefs == 'fail' }}
{{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"'); }}
{{? $typeSchema }} {{?? it.opts.extendRefs == 'ignore' }}
{{? it.opts.coerceTypes }}
{{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
{{?}}
{{ var $rulesGroup = it.RULES.types[$typeSchema]; }}
{{? $coerceToTypes || $typeIsArray || $rulesGroup === true ||
($rulesGroup && !$shouldUseGroup($rulesGroup)) }}
{{ {{
var $schemaPath = it.schemaPath + '.type' $refKeywords = false;
, $errSchemaPath = it.errSchemaPath + '/type'; console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
}} }}
{{# def.checkType }} {{?? it.opts.extendRefs !== true }}
{{? $coerceToTypes }} {{ 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'); }}
{{# def.coerceType }}
{{??}}
{{# def.error:'type' }}
{{?}}
}
{{?}} {{?}}
{{?}} {{?}}
{{? it.schema.$ref && !$refKeywords }} {{? it.schema.$ref && !$refKeywords }}
{{= it.RULES.all.$ref.code(it, '$ref') }} {{= it.RULES.all.$ref.code(it, '$ref') }}
{{? $breakOnError }} {{? $breakOnError }}
...@@ -194,7 +120,7 @@ ...@@ -194,7 +120,7 @@
{{? $rulesGroup.type }} {{? $rulesGroup.type }}
if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) { if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
{{?}} {{?}}
{{? it.opts.useDefaults }} {{? it.opts.useDefaults && !it.compositeRule }}
{{? $rulesGroup.type == 'object' && it.schema.properties }} {{? $rulesGroup.type == 'object' && it.schema.properties }}
{{# def.defaultProperties }} {{# def.defaultProperties }}
{{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }} {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
...@@ -203,12 +129,9 @@ ...@@ -203,12 +129,9 @@
{{?}} {{?}}
{{~ $rulesGroup.rules:$rule }} {{~ $rulesGroup.rules:$rule }}
{{? $shouldUseRule($rule) }} {{? $shouldUseRule($rule) }}
{{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }} {{= $rule.code(it, $rule.keyword) }}
{{? $code }} {{? $breakOnError }}
{{= $code }} {{ $closingBraces1 += '}'; }}
{{? $breakOnError }}
{{ $closingBraces1 += '}'; }}
{{?}}
{{?}} {{?}}
{{?}} {{?}}
{{~}} {{~}}
...@@ -219,6 +142,7 @@ ...@@ -219,6 +142,7 @@
{{? $rulesGroup.type }} {{? $rulesGroup.type }}
} }
{{? $typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes }} {{? $typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes }}
{{ var $typeChecked = true; }}
else { else {
{{ {{
var $schemaPath = it.schemaPath + '.type' var $schemaPath = it.schemaPath + '.type'
...@@ -237,17 +161,23 @@ ...@@ -237,17 +161,23 @@
{{~}} {{~}}
{{?}} {{?}}
{{? $typeSchema && !$typeChecked && !$coerceToTypes }}
{{# def.checkType }}
{{# def.error:'type' }}
}
{{?}}
{{? $breakOnError }} {{= $closingBraces2 }} {{?}} {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
{{? $top }} {{? $top }}
{{? $async }} {{? $async }}
if (errors === 0) return data; {{ /* don't edit, used in replace */ }} if (errors === 0) return true; {{ /* don't edit, used in replace */ }}
else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }} else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
{{??}} {{??}}
validate.errors = vErrors; {{ /* don't edit, used in replace */ }} validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
return errors === 0; {{ /* don't edit, used in replace */ }} return errors === 0; {{ /* don't edit, used in replace */ }}
{{?}} {{?}}
}; });
return validate; return validate;
{{??}} {{??}}
...@@ -256,27 +186,25 @@ ...@@ -256,27 +186,25 @@
{{# def.cleanUp }} {{# def.cleanUp }}
{{? $top }} {{? $top && $breakOnError }}
{{# def.finalCleanUp }} {{# def.cleanUpVarErrors }}
{{?}} {{?}}
{{ {{
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]))
if ($shouldUseRule(rules[i]))
return true; return true;
} }
function $shouldUseRule($rule) { function $shouldUseRule($rule) {
return it.schema[$rule.keyword] !== undefined || return it.schema[$rule.keyword] !== undefined ||
($rule.implements && $ruleImplementsSomeKeyword($rule)); ( $rule.keyword == 'properties' &&
} ( it.schema.additionalProperties === false ||
typeof it.schema.additionalProperties == 'object'
function $ruleImplementsSomeKeyword($rule) { || ( it.schema.patternProperties &&
var impl = $rule.implements; Object.keys(it.schema.patternProperties).length )
for (var i=0; i < impl.length; i++) || ( it.opts.v5 && it.schema.patternGroups &&
if (it.schema[impl[i]] !== undefined) Object.keys(it.schema.patternGroups).length )));
return true;
} }
}} }}
'use strict'; 'use strict';
module.exports = function generate__limit(it, $keyword, $ruleType) { module.exports = function generate__limit(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__limit(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate__limit(it, $keyword, $ruleType) {
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $errorKeyword; var $errorKeyword;
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)) + '; ';
...@@ -20,20 +20,17 @@ module.exports = function generate__limit(it, $keyword, $ruleType) { ...@@ -20,20 +20,17 @@ module.exports = function generate__limit(it, $keyword, $ruleType) {
var $isMax = $keyword == 'maximum', var $isMax = $keyword == 'maximum',
$exclusiveKeyword = $isMax ? 'exclusiveMaximum' : 'exclusiveMinimum', $exclusiveKeyword = $isMax ? 'exclusiveMaximum' : 'exclusiveMinimum',
$schemaExcl = it.schema[$exclusiveKeyword], $schemaExcl = it.schema[$exclusiveKeyword],
$isDataExcl = it.opts.$data && $schemaExcl && $schemaExcl.$data, $isDataExcl = it.opts.v5 && $schemaExcl && $schemaExcl.$data,
$op = $isMax ? '<' : '>', $op = $isMax ? '<' : '>',
$notOp = $isMax ? '>' : '<', $notOp = $isMax ? '>' : '<';
$errorKeyword = undefined;
if ($isDataExcl) { if ($isDataExcl) {
var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr), var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr),
$exclusive = 'exclusive' + $lvl, $exclusive = 'exclusive' + $lvl,
$exclType = 'exclType' + $lvl,
$exclIsNumber = 'exclIsNumber' + $lvl,
$opExpr = 'op' + $lvl, $opExpr = 'op' + $lvl,
$opStr = '\' + ' + $opExpr + ' + \''; $opStr = '\' + ' + $opExpr + ' + \'';
out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; '; out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; ';
$schemaValueExcl = 'schemaExcl' + $lvl; $schemaValueExcl = 'schemaExcl' + $lvl;
out += ' var ' + ($exclusive) + '; var ' + ($exclType) + ' = typeof ' + ($schemaValueExcl) + '; if (' + ($exclType) + ' != \'boolean\' && ' + ($exclType) + ' != \'undefined\' && ' + ($exclType) + ' != \'number\') { '; out += ' var exclusive' + ($lvl) + '; if (typeof ' + ($schemaValueExcl) + ' != \'boolean\' && typeof ' + ($schemaValueExcl) + ' != \'undefined\') { ';
var $errorKeyword = $exclusiveKeyword; var $errorKeyword = $exclusiveKeyword;
var $$outStack = $$outStack || []; var $$outStack = $$outStack || [];
$$outStack.push(out); $$outStack.push(out);
...@@ -52,8 +49,7 @@ module.exports = function generate__limit(it, $keyword, $ruleType) { ...@@ -52,8 +49,7 @@ module.exports = function generate__limit(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 {
...@@ -62,55 +58,27 @@ module.exports = function generate__limit(it, $keyword, $ruleType) { ...@@ -62,55 +58,27 @@ module.exports = function generate__limit(it, $keyword, $ruleType) {
} else { } else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
} }
out += ' } else if ( '; out += ' } else if( ';
if ($isData) { if ($isData) {
out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || '; out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
} }
out += ' ' + ($exclType) + ' == \'number\' ? ( (' + ($exclusive) + ' = ' + ($schemaValue) + ' === undefined || ' + ($schemaValueExcl) + ' ' + ($op) + '= ' + ($schemaValue) + ') ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaValueExcl) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) : ( (' + ($exclusive) + ' = ' + ($schemaValueExcl) + ' === true) ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaValue) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) || ' + ($data) + ' !== ' + ($data) + ') { var op' + ($lvl) + ' = ' + ($exclusive) + ' ? \'' + ($op) + '\' : \'' + ($op) + '=\'; '; out += ' ((exclusive' + ($lvl) + ' = ' + ($schemaValueExcl) + ' === true) ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaValue) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ') || ' + ($data) + ' !== ' + ($data) + ') { var op' + ($lvl) + ' = exclusive' + ($lvl) + ' ? \'' + ($op) + '\' : \'' + ($op) + '=\';';
if ($schema === undefined) {
$errorKeyword = $exclusiveKeyword;
$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
$schemaValue = $schemaValueExcl;
$isData = $isDataExcl;
}
} else { } else {
var $exclIsNumber = typeof $schemaExcl == 'number', var $exclusive = $schemaExcl === true,
$opStr = $op; $opStr = $op;
if ($exclIsNumber && $isData) { if (!$exclusive) $opStr += '=';
var $opExpr = '\'' + $opStr + '\''; var $opExpr = '\'' + $opStr + '\'';
out += ' if ( '; out += ' if ( ';
if ($isData) { if ($isData) {
out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || '; out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
} }
out += ' ( ' + ($schemaValue) + ' === undefined || ' + ($schemaExcl) + ' ' + ($op) + '= ' + ($schemaValue) + ' ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaExcl) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' ) || ' + ($data) + ' !== ' + ($data) + ') { '; out += ' ' + ($data) + ' ' + ($notOp);
} else { if ($exclusive) {
if ($exclIsNumber && $schema === undefined) { out += '=';
$exclusive = true;
$errorKeyword = $exclusiveKeyword;
$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
$schemaValue = $schemaExcl;
$notOp += '=';
} else {
if ($exclIsNumber) $schemaValue = Math[$isMax ? 'min' : 'max']($schemaExcl, $schema);
if ($schemaExcl === ($exclIsNumber ? $schemaValue : true)) {
$exclusive = true;
$errorKeyword = $exclusiveKeyword;
$errSchemaPath = it.errSchemaPath + '/' + $exclusiveKeyword;
$notOp += '=';
} else {
$exclusive = false;
$opStr += '=';
}
}
var $opExpr = '\'' + $opStr + '\'';
out += ' if ( ';
if ($isData) {
out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
}
out += ' ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ' || ' + ($data) + ' !== ' + ($data) + ') { ';
} }
out += ' ' + ($schemaValue) + ' || ' + ($data) + ' !== ' + ($data) + ') {';
} }
$errorKeyword = $errorKeyword || $keyword; var $errorKeyword = $keyword;
var $$outStack = $$outStack || []; var $$outStack = $$outStack || [];
$$outStack.push(out); $$outStack.push(out);
out = ''; /* istanbul ignore else */ out = ''; /* istanbul ignore else */
...@@ -121,7 +89,7 @@ module.exports = function generate__limit(it, $keyword, $ruleType) { ...@@ -121,7 +89,7 @@ module.exports = function generate__limit(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) {
...@@ -139,8 +107,7 @@ module.exports = function generate__limit(it, $keyword, $ruleType) { ...@@ -139,8 +107,7 @@ module.exports = function generate__limit(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__limitItems(it, $keyword, $ruleType) { module.exports = function generate__limitItems(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__limitItems(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate__limitItems(it, $keyword, $ruleType) {
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $errorKeyword; var $errorKeyword;
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)) + '; ';
...@@ -34,7 +34,7 @@ module.exports = function generate__limitItems(it, $keyword, $ruleType) { ...@@ -34,7 +34,7 @@ module.exports = function generate__limitItems(it, $keyword, $ruleType) {
if ($keyword == 'maxItems') { if ($keyword == 'maxItems') {
out += 'more'; out += 'more';
} else { } else {
out += 'fewer'; out += 'less';
} }
out += ' than '; out += ' than ';
if ($isData) { if ($isData) {
...@@ -59,8 +59,7 @@ module.exports = function generate__limitItems(it, $keyword, $ruleType) { ...@@ -59,8 +59,7 @@ module.exports = function generate__limitItems(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__limitLength(it, $keyword, $ruleType) { module.exports = function generate__limitLength(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__limitLength(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate__limitLength(it, $keyword, $ruleType) {
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $errorKeyword; var $errorKeyword;
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)) + '; ';
...@@ -64,8 +64,7 @@ module.exports = function generate__limitLength(it, $keyword, $ruleType) { ...@@ -64,8 +64,7 @@ module.exports = function generate__limitLength(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__limitProperties(it, $keyword, $ruleType) { module.exports = function generate__limitProperties(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__limitProperties(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate__limitProperties(it, $keyword, $ruleType) {
var $breakOnError = !it.opts.allErrors; var $breakOnError = !it.opts.allErrors;
var $errorKeyword; var $errorKeyword;
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)) + '; ';
...@@ -34,7 +34,7 @@ module.exports = function generate__limitProperties(it, $keyword, $ruleType) { ...@@ -34,7 +34,7 @@ module.exports = function generate__limitProperties(it, $keyword, $ruleType) {
if ($keyword == 'maxProperties') { if ($keyword == 'maxProperties') {
out += 'more'; out += 'more';
} else { } else {
out += 'fewer'; out += 'less';
} }
out += ' than '; out += ' than ';
if ($isData) { if ($isData) {
...@@ -59,8 +59,7 @@ module.exports = function generate__limitProperties(it, $keyword, $ruleType) { ...@@ -59,8 +59,7 @@ module.exports = function generate__limitProperties(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_allOf(it, $keyword, $ruleType) { module.exports = function generate_allOf(it, $keyword) {
var out = ' '; var out = ' ';
var $schema = it.schema[$keyword]; var $schema = it.schema[$keyword];
var $schemaPath = it.schemaPath + it.util.getProperty($keyword); var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
...@@ -17,7 +17,7 @@ module.exports = function generate_allOf(it, $keyword, $ruleType) { ...@@ -17,7 +17,7 @@ module.exports = function generate_allOf(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)) {
$allSchemasEmpty = false; $allSchemasEmpty = false;
$it.schema = $sch; $it.schema = $sch;
$it.schemaPath = $schemaPath + '[' + $i + ']'; $it.schemaPath = $schemaPath + '[' + $i + ']';
......
'use strict'; 'use strict';
module.exports = function generate_anyOf(it, $keyword, $ruleType) { module.exports = function generate_anyOf(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -15,7 +15,7 @@ module.exports = function generate_anyOf(it, $keyword, $ruleType) { ...@@ -15,7 +15,7 @@ module.exports = function generate_anyOf(it, $keyword, $ruleType) {
$it.level++; $it.level++;
var $nextValid = 'valid' + $it.level; var $nextValid = 'valid' + $it.level;
var $noEmptySchema = $schema.every(function($sch) { var $noEmptySchema = $schema.every(function($sch) {
return (it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all)); return it.util.schemaHasRules($sch, it.RULES.all);
}); });
if ($noEmptySchema) { if ($noEmptySchema) {
var $currentBaseId = $it.baseId; var $currentBaseId = $it.baseId;
...@@ -38,7 +38,7 @@ module.exports = function generate_anyOf(it, $keyword, $ruleType) { ...@@ -38,7 +38,7 @@ module.exports = function generate_anyOf(it, $keyword, $ruleType) {
} }
} }
it.compositeRule = $it.compositeRule = $wasComposite; it.compositeRule = $it.compositeRule = $wasComposite;
out += ' ' + ($closingBraces) + ' if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */ out += ' ' + ($closingBraces) + ' if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */
if (it.createErrors !== false) { if (it.createErrors !== false) {
out += ' { keyword: \'' + ('anyOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} '; out += ' { keyword: \'' + ('anyOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
if (it.opts.messages !== false) { if (it.opts.messages !== false) {
...@@ -51,16 +51,7 @@ module.exports = function generate_anyOf(it, $keyword, $ruleType) { ...@@ -51,16 +51,7 @@ module.exports = function generate_anyOf(it, $keyword, $ruleType) {
} else { } else {
out += ' {} '; out += ' {} ';
} }
out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
if (!it.compositeRule && $breakOnError) {
/* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError(vErrors); ';
} else {
out += ' validate.errors = vErrors; return false; ';
}
}
out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
if (it.opts.allErrors) { if (it.opts.allErrors) {
out += ' } '; out += ' } ';
} }
......
'use strict';
module.exports = function generate_comment(it, $keyword, $ruleType) {
var out = ' ';
var $schema = it.schema[$keyword];
var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors;
var $comment = it.util.toQuotedString($schema);
if (it.opts.$comment === true) {
out += ' console.log(' + ($comment) + ');';
} else if (typeof it.opts.$comment == 'function') {
out += ' self._opts.$comment(' + ($comment) + ', ' + (it.util.toQuotedString($errSchemaPath)) + ', validate.root.schema);';
}
return out;
}
'use strict';
module.exports = function generate_const(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 $valid = 'valid' + $lvl;
var $isData = it.opts.$data && $schema && $schema.$data,
$schemaValue;
if ($isData) {
out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
$schemaValue = 'schema' + $lvl;
} else {
$schemaValue = $schema;
}
if (!$isData) {
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ';';
}
out += 'var ' + ($valid) + ' = equal(' + ($data) + ', schema' + ($lvl) + '); if (!' + ($valid) + ') { ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('const') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { allowedValue: schema' + ($lvl) + ' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should be equal to constant\' ';
}
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) {
out += ' else { ';
}
return out;
}
'use strict';
module.exports = function generate_contains(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 $valid = 'valid' + $lvl;
var $errs = 'errs__' + $lvl;
var $it = it.util.copy(it);
var $closingBraces = '';
$it.level++;
var $nextValid = 'valid' + $it.level;
var $idx = 'i' + $lvl,
$dataNxt = $it.dataLevel = it.dataLevel + 1,
$nextData = 'data' + $dataNxt,
$currentBaseId = it.baseId,
$nonEmptySchema = (it.opts.strictKeywords ? typeof $schema == 'object' && Object.keys($schema).length > 0 : it.util.schemaHasRules($schema, it.RULES.all));
out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
if ($nonEmptySchema) {
var $wasComposite = it.compositeRule;
it.compositeRule = $it.compositeRule = true;
$it.schema = $schema;
$it.schemaPath = $schemaPath;
$it.errSchemaPath = $errSchemaPath;
out += ' var ' + ($nextValid) + ' = false; for (var ' + ($idx) + ' = 0; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
$it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
var $passData = $data + '[' + $idx + ']';
$it.dataPathArr[$dataNxt] = $idx;
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) + ' ';
}
out += ' if (' + ($nextValid) + ') break; } ';
it.compositeRule = $it.compositeRule = $wasComposite;
out += ' ' + ($closingBraces) + ' if (!' + ($nextValid) + ') {';
} else {
out += ' if (' + ($data) + '.length == 0) {';
}
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('contains') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
if (it.opts.messages !== false) {
out += ' , message: \'should contain a valid item\' ';
}
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 += ' } else { ';
if ($nonEmptySchema) {
out += ' errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
}
if (it.opts.allErrors) {
out += ' } ';
}
out = it.util.cleanUpCode(out);
return out;
}
'use strict'; 'use strict';
module.exports = function generate_custom(it, $keyword, $ruleType) { module.exports = function generate_custom(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -11,7 +11,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -11,7 +11,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
var $data = 'data' + ($dataLvl || ''); var $data = 'data' + ($dataLvl || '');
var $valid = 'valid' + $lvl; var $valid = 'valid' + $lvl;
var $errs = 'errs__' + $lvl; var $errs = 'errs__' + $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,8 +21,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -21,8 +21,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
} }
var $rule = this, var $rule = this,
$definition = 'definition' + $lvl, $definition = 'definition' + $lvl,
$rDef = $rule.definition, $rDef = $rule.definition;
$closingBraces = '';
var $compile, $inline, $macro, $ruleValidate, $validateCode; var $compile, $inline, $macro, $ruleValidate, $validateCode;
if ($isData && $rDef.$data) { if ($isData && $rDef.$data) {
$validateCode = 'keywordValidate' + $lvl; $validateCode = 'keywordValidate' + $lvl;
...@@ -30,7 +29,6 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -30,7 +29,6 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
out += ' var ' + ($definition) + ' = RULES.custom[\'' + ($keyword) + '\'].definition; var ' + ($validateCode) + ' = ' + ($definition) + '.validate;'; out += ' var ' + ($definition) + ' = RULES.custom[\'' + ($keyword) + '\'].definition; var ' + ($validateCode) + ' = ' + ($definition) + '.validate;';
} else { } else {
$ruleValidate = it.useCustomRule($rule, $schema, it.schema, it); $ruleValidate = it.useCustomRule($rule, $schema, it.schema, it);
if (!$ruleValidate) return;
$schemaValue = 'validate.schema' + $schemaPath; $schemaValue = 'validate.schema' + $schemaPath;
$validateCode = $ruleValidate.code; $validateCode = $ruleValidate.code;
$compile = $rDef.compile; $compile = $rDef.compile;
...@@ -46,13 +44,8 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -46,13 +44,8 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
out += '' + ($ruleErrs) + ' = null;'; out += '' + ($ruleErrs) + ' = null;';
} }
out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';'; out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
if ($isData && $rDef.$data) { if ($validateSchema) {
$closingBraces += '}'; out += ' ' + ($valid) + ' = ' + ($definition) + '.validateSchema(' + ($schemaValue) + '); if (' + ($valid) + ') {';
out += ' if (' + ($schemaValue) + ' === undefined) { ' + ($valid) + ' = true; } else { ';
if ($validateSchema) {
$closingBraces += '}';
out += ' ' + ($valid) + ' = ' + ($definition) + '.validateSchema(' + ($schemaValue) + '); if (' + ($valid) + ') { ';
}
} }
if ($inline) { if ($inline) {
if ($rDef.statements) { if ($rDef.statements) {
...@@ -62,7 +55,6 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -62,7 +55,6 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
} }
} else if ($macro) { } else if ($macro) {
var $it = it.util.copy(it); var $it = it.util.copy(it);
var $closingBraces = '';
$it.level++; $it.level++;
var $nextValid = 'valid' + $it.level; var $nextValid = 'valid' + $it.level;
$it.schema = $ruleValidate.validate; $it.schema = $ruleValidate.validate;
...@@ -99,22 +91,24 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -99,22 +91,24 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
if ($rDef.errors === false) { if ($rDef.errors === false) {
out += ' ' + ($valid) + ' = '; out += ' ' + ($valid) + ' = ';
if ($asyncKeyword) { if ($asyncKeyword) {
out += 'await '; out += '' + (it.yieldAwait);
} }
out += '' + (def_callRuleValidate) + '; '; out += '' + (def_callRuleValidate) + '; ';
} else { } else {
if ($asyncKeyword) { if ($asyncKeyword) {
$ruleErrs = 'customErrors' + $lvl; $ruleErrs = 'customErrors' + $lvl;
out += ' var ' + ($ruleErrs) + ' = null; try { ' + ($valid) + ' = await ' + (def_callRuleValidate) + '; } catch (e) { ' + ($valid) + ' = false; if (e instanceof ValidationError) ' + ($ruleErrs) + ' = e.errors; else throw e; } '; out += ' var ' + ($ruleErrs) + ' = null; try { ' + ($valid) + ' = ' + (it.yieldAwait) + (def_callRuleValidate) + '; } catch (e) { ' + ($valid) + ' = false; if (e instanceof ValidationError) ' + ($ruleErrs) + ' = e.errors; else throw e; } ';
} else { } else {
out += ' ' + ($ruleErrs) + ' = null; ' + ($valid) + ' = ' + (def_callRuleValidate) + '; '; out += ' ' + ($ruleErrs) + ' = null; ' + ($valid) + ' = ' + (def_callRuleValidate) + '; ';
} }
} }
} }
if ($rDef.modifying) { if ($rDef.modifying) {
out += ' if (' + ($parentData) + ') ' + ($data) + ' = ' + ($parentData) + '[' + ($parentDataProperty) + '];'; out += ' ' + ($data) + ' = ' + ($parentData) + '[' + ($parentDataProperty) + '];';
}
if ($validateSchema) {
out += ' }';
} }
out += '' + ($closingBraces);
if ($rDef.valid) { if ($rDef.valid) {
if ($breakOnError) { if ($breakOnError) {
out += ' if (true) { '; out += ' if (true) { ';
...@@ -153,8 +147,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -153,8 +147,7 @@ module.exports = function generate_custom(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 {
...@@ -200,8 +193,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) { ...@@ -200,8 +193,7 @@ module.exports = function generate_custom(it, $keyword, $ruleType) {
out += ' {} '; out += ' {} ';
} }
out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
if (!it.compositeRule && $breakOnError) { if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
/* istanbul ignore if */
if (it.async) { if (it.async) {
out += ' throw new ValidationError(vErrors); '; out += ' throw new ValidationError(vErrors); ';
} else { } else {
......
'use strict'; 'use strict';
module.exports = function generate_dependencies(it, $keyword, $ruleType) { module.exports = function generate_dependencies(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -14,8 +14,7 @@ module.exports = function generate_dependencies(it, $keyword, $ruleType) { ...@@ -14,8 +14,7 @@ module.exports = function generate_dependencies(it, $keyword, $ruleType) {
$it.level++; $it.level++;
var $nextValid = 'valid' + $it.level; var $nextValid = 'valid' + $it.level;
var $schemaDeps = {}, var $schemaDeps = {},
$propertyDeps = {}, $propertyDeps = {};
$ownProperties = it.opts.ownProperties;
for ($property in $schema) { for ($property in $schema) {
var $sch = $schema[$property]; var $sch = $schema[$property];
var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps; var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps;
...@@ -26,128 +25,108 @@ module.exports = function generate_dependencies(it, $keyword, $ruleType) { ...@@ -26,128 +25,108 @@ module.exports = function generate_dependencies(it, $keyword, $ruleType) {
out += 'var missing' + ($lvl) + ';'; out += 'var missing' + ($lvl) + ';';
for (var $property in $propertyDeps) { for (var $property in $propertyDeps) {
$deps = $propertyDeps[$property]; $deps = $propertyDeps[$property];
if ($deps.length) { out += ' if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';
out += ' if ( ' + ($data) + (it.util.getProperty($property)) + ' !== undefined '; if ($breakOnError) {
if ($ownProperties) { out += ' && ( ';
out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($property)) + '\') '; var arr1 = $deps;
} if (arr1) {
if ($breakOnError) { var _$property, $i = -1,
out += ' && ( '; l1 = arr1.length - 1;
var arr1 = $deps; while ($i < l1) {
if (arr1) { _$property = arr1[$i += 1];
var $propertyKey, $i = -1, if ($i) {
l1 = arr1.length - 1; out += ' || ';
while ($i < l1) {
$propertyKey = arr1[$i += 1];
if ($i) {
out += ' || ';
}
var $prop = it.util.getProperty($propertyKey),
$useData = $data + $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 += ')) { ';
var $propertyPath = 'missing' + $lvl,
$missingProperty = '\' + ' + $propertyPath + ' + \'';
if (it.opts._errorDataPathProperty) {
it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
}
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should have ';
if ($deps.length == 1) {
out += 'property ' + (it.util.escapeQuotes($deps[0]));
} else {
out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
}
out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
} }
out += ' } '; var $prop = it.util.getProperty(_$property);
} else { out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
out += ' {} ';
} }
var __err = out; }
out = $$outStack.pop(); out += ')) { ';
if (!it.compositeRule && $breakOnError) { var $propertyPath = 'missing' + $lvl,
/* istanbul ignore if */ $missingProperty = '\' + ' + $propertyPath + ' + \'';
if (it.async) { if (it.opts._errorDataPathProperty) {
out += ' throw new ValidationError([' + (__err) + ']); '; it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
}
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should have ';
if ($deps.length == 1) {
out += 'property ' + (it.util.escapeQuotes($deps[0]));
} else { } else {
out += ' validate.errors = [' + (__err) + ']; return false; '; out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
} }
out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
}
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 { } else {
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; out += ' validate.errors = [' + (__err) + ']; return false; ';
} }
} else { } else {
out += ' ) { '; out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
var arr2 = $deps; }
if (arr2) { } else {
var $propertyKey, i2 = -1, out += ' ) { ';
l2 = arr2.length - 1; var arr2 = $deps;
while (i2 < l2) { if (arr2) {
$propertyKey = arr2[i2 += 1]; var $reqProperty, i2 = -1,
var $prop = it.util.getProperty($propertyKey), l2 = arr2.length - 1;
$missingProperty = it.util.escapeQuotes($propertyKey), while (i2 < l2) {
$useData = $data + $prop; $reqProperty = arr2[i2 += 1];
if (it.opts._errorDataPathProperty) { var $prop = it.util.getProperty($reqProperty),
it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers); $missingProperty = it.util.escapeQuotes($reqProperty);
} if (it.opts._errorDataPathProperty) {
out += ' if ( ' + ($useData) + ' === undefined '; it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
if ($ownProperties) { }
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') '; out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = '; /* istanbul ignore else */
} if (it.createErrors !== false) {
out += ') { var err = '; /* istanbul ignore else */ out += ' { keyword: \'' + ('dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
if (it.createErrors !== false) { if (it.opts.messages !== false) {
out += ' { keyword: \'' + ('dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } '; out += ' , message: \'should have ';
if (it.opts.messages !== false) { if ($deps.length == 1) {
out += ' , message: \'should have '; out += 'property ' + (it.util.escapeQuotes($deps[0]));
if ($deps.length == 1) { } else {
out += 'property ' + (it.util.escapeQuotes($deps[0])); out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
} else {
out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
}
out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
}
if (it.opts.verbose) {
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
} }
out += ' } '; out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
} else {
out += ' {} ';
} }
out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } '; 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++; } ';
} }
} }
out += ' } '; }
if ($breakOnError) { out += ' } ';
$closingBraces += '}'; if ($breakOnError) {
out += ' else { '; $closingBraces += '}';
} out += ' else { ';
} }
} }
it.errorPath = $currentErrorPath; it.errorPath = $currentErrorPath;
var $currentBaseId = $it.baseId; var $currentBaseId = $it.baseId;
for (var $property in $schemaDeps) { for (var $property in $schemaDeps) {
var $sch = $schemaDeps[$property]; var $sch = $schemaDeps[$property];
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) + (it.util.getProperty($property)) + ' !== undefined '; out += ' ' + ($nextValid) + ' = true; if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined) { ';
if ($ownProperties) {
out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($property)) + '\') ';
}
out += ') { ';
$it.schema = $sch; $it.schema = $sch;
$it.schemaPath = $schemaPath + it.util.getProperty($property); $it.schemaPath = $schemaPath + it.util.getProperty($property);
$it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property); $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property);
......
'use strict'; 'use strict';
module.exports = function generate_enum(it, $keyword, $ruleType) { module.exports = function generate_enum(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_enum(it, $keyword, $ruleType) { ...@@ -9,7 +9,7 @@ module.exports = function generate_enum(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)) + '; ';
...@@ -48,8 +48,7 @@ module.exports = function generate_enum(it, $keyword, $ruleType) { ...@@ -48,8 +48,7 @@ module.exports = function generate_enum(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_format(it, $keyword, $ruleType) { module.exports = function generate_format(it, $keyword) {
var out = ' '; var out = ' ';
var $lvl = it.level; var $lvl = it.level;
var $dataLvl = it.dataLevel; var $dataLvl = it.dataLevel;
...@@ -14,7 +14,7 @@ module.exports = function generate_format(it, $keyword, $ruleType) { ...@@ -14,7 +14,7 @@ module.exports = function generate_format(it, $keyword, $ruleType) {
} }
return out; return out;
} }
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)) + '; ';
...@@ -25,10 +25,8 @@ module.exports = function generate_format(it, $keyword, $ruleType) { ...@@ -25,10 +25,8 @@ module.exports = function generate_format(it, $keyword, $ruleType) {
var $unknownFormats = it.opts.unknownFormats, var $unknownFormats = it.opts.unknownFormats,
$allowUnknown = Array.isArray($unknownFormats); $allowUnknown = Array.isArray($unknownFormats);
if ($isData) { if ($isData) {
var $format = 'format' + $lvl, var $format = 'format' + $lvl;
$isObject = 'isObject' + $lvl, out += ' var ' + ($format) + ' = formats[' + ($schemaValue) + ']; var isObject' + ($lvl) + ' = typeof ' + ($format) + ' == \'object\' && !(' + ($format) + ' instanceof RegExp) && ' + ($format) + '.validate; if (isObject' + ($lvl) + ') { ';
$formatType = 'formatType' + $lvl;
out += ' var ' + ($format) + ' = formats[' + ($schemaValue) + ']; var ' + ($isObject) + ' = typeof ' + ($format) + ' == \'object\' && !(' + ($format) + ' instanceof RegExp) && ' + ($format) + '.validate; var ' + ($formatType) + ' = ' + ($isObject) + ' && ' + ($format) + '.type || \'string\'; if (' + ($isObject) + ') { ';
if (it.async) { if (it.async) {
out += ' var async' + ($lvl) + ' = ' + ($format) + '.async; '; out += ' var async' + ($lvl) + ' = ' + ($format) + '.async; ';
} }
...@@ -37,16 +35,16 @@ module.exports = function generate_format(it, $keyword, $ruleType) { ...@@ -37,16 +35,16 @@ module.exports = function generate_format(it, $keyword, $ruleType) {
out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'string\') || '; out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'string\') || ';
} }
out += ' ('; out += ' (';
if ($unknownFormats != 'ignore') { if ($unknownFormats === true || $allowUnknown) {
out += ' (' + ($schemaValue) + ' && !' + ($format) + ' '; out += ' (' + ($schemaValue) + ' && !' + ($format) + ' ';
if ($allowUnknown) { if ($allowUnknown) {
out += ' && self._opts.unknownFormats.indexOf(' + ($schemaValue) + ') == -1 '; out += ' && self._opts.unknownFormats.indexOf(' + ($schemaValue) + ') == -1 ';
} }
out += ') || '; out += ') || ';
} }
out += ' (' + ($format) + ' && ' + ($formatType) + ' == \'' + ($ruleType) + '\' && !(typeof ' + ($format) + ' == \'function\' ? '; out += ' (' + ($format) + ' && !(typeof ' + ($format) + ' == \'function\' ? ';
if (it.async) { if (it.async) {
out += ' (async' + ($lvl) + ' ? await ' + ($format) + '(' + ($data) + ') : ' + ($format) + '(' + ($data) + ')) '; out += ' (async' + ($lvl) + ' ? ' + (it.yieldAwait) + ' ' + ($format) + '(' + ($data) + ') : ' + ($format) + '(' + ($data) + ')) ';
} else { } else {
out += ' ' + ($format) + '(' + ($data) + ') '; out += ' ' + ($format) + '(' + ($data) + ') ';
} }
...@@ -54,37 +52,28 @@ module.exports = function generate_format(it, $keyword, $ruleType) { ...@@ -54,37 +52,28 @@ module.exports = function generate_format(it, $keyword, $ruleType) {
} else { } else {
var $format = it.formats[$schema]; var $format = it.formats[$schema];
if (!$format) { if (!$format) {
if ($unknownFormats == 'ignore') { if ($unknownFormats === true || ($allowUnknown && $unknownFormats.indexOf($schema) == -1)) {
it.logger.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"'); throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"');
if ($breakOnError) { } else {
out += ' if (true) { '; if (!$allowUnknown) {
console.warn('unknown format "' + $schema + '" ignored in schema at path "' + it.errSchemaPath + '"');
if ($unknownFormats !== 'ignore') console.warn('In the next major version it will throw exception. See option unknownFormats for more information');
} }
return out;
} else if ($allowUnknown && $unknownFormats.indexOf($schema) >= 0) {
if ($breakOnError) { if ($breakOnError) {
out += ' if (true) { '; out += ' if (true) { ';
} }
return out; return out;
} else {
throw new Error('unknown format "' + $schema + '" is used in schema at path "' + it.errSchemaPath + '"');
} }
} }
var $isObject = typeof $format == 'object' && !($format instanceof RegExp) && $format.validate; var $isObject = typeof $format == 'object' && !($format instanceof RegExp) && $format.validate;
var $formatType = $isObject && $format.type || 'string';
if ($isObject) { if ($isObject) {
var $async = $format.async === true; var $async = $format.async === true;
$format = $format.validate; $format = $format.validate;
} }
if ($formatType != $ruleType) {
if ($breakOnError) {
out += ' if (true) { ';
}
return out;
}
if ($async) { if ($async) {
if (!it.async) throw new Error('async format in sync schema'); if (!it.async) throw new Error('async format in sync schema');
var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate'; var $formatRef = 'formats' + it.util.getProperty($schema) + '.validate';
out += ' if (!(await ' + ($formatRef) + '(' + ($data) + '))) { '; out += ' if (!(' + (it.yieldAwait) + ' ' + ($formatRef) + '(' + ($data) + '))) { ';
} else { } else {
out += ' if (! '; out += ' if (! ';
var $formatRef = 'formats' + it.util.getProperty($schema); var $formatRef = 'formats' + it.util.getProperty($schema);
...@@ -132,8 +121,7 @@ module.exports = function generate_format(it, $keyword, $ruleType) { ...@@ -132,8 +121,7 @@ module.exports = function generate_format(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';
module.exports = function generate_if(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 $valid = 'valid' + $lvl;
var $errs = 'errs__' + $lvl;
var $it = it.util.copy(it);
$it.level++;
var $nextValid = 'valid' + $it.level;
var $thenSch = it.schema['then'],
$elseSch = it.schema['else'],
$thenPresent = $thenSch !== undefined && (it.opts.strictKeywords ? typeof $thenSch == 'object' && Object.keys($thenSch).length > 0 : it.util.schemaHasRules($thenSch, it.RULES.all)),
$elsePresent = $elseSch !== undefined && (it.opts.strictKeywords ? typeof $elseSch == 'object' && Object.keys($elseSch).length > 0 : it.util.schemaHasRules($elseSch, it.RULES.all)),
$currentBaseId = $it.baseId;
if ($thenPresent || $elsePresent) {
var $ifClause;
$it.createErrors = false;
$it.schema = $schema;
$it.schemaPath = $schemaPath;
$it.errSchemaPath = $errSchemaPath;
out += ' var ' + ($errs) + ' = errors; var ' + ($valid) + ' = true; ';
var $wasComposite = it.compositeRule;
it.compositeRule = $it.compositeRule = true;
out += ' ' + (it.validate($it)) + ' ';
$it.baseId = $currentBaseId;
$it.createErrors = true;
out += ' errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
it.compositeRule = $it.compositeRule = $wasComposite;
if ($thenPresent) {
out += ' if (' + ($nextValid) + ') { ';
$it.schema = it.schema['then'];
$it.schemaPath = it.schemaPath + '.then';
$it.errSchemaPath = it.errSchemaPath + '/then';
out += ' ' + (it.validate($it)) + ' ';
$it.baseId = $currentBaseId;
out += ' ' + ($valid) + ' = ' + ($nextValid) + '; ';
if ($thenPresent && $elsePresent) {
$ifClause = 'ifClause' + $lvl;
out += ' var ' + ($ifClause) + ' = \'then\'; ';
} else {
$ifClause = '\'then\'';
}
out += ' } ';
if ($elsePresent) {
out += ' else { ';
}
} else {
out += ' if (!' + ($nextValid) + ') { ';
}
if ($elsePresent) {
$it.schema = it.schema['else'];
$it.schemaPath = it.schemaPath + '.else';
$it.errSchemaPath = it.errSchemaPath + '/else';
out += ' ' + (it.validate($it)) + ' ';
$it.baseId = $currentBaseId;
out += ' ' + ($valid) + ' = ' + ($nextValid) + '; ';
if ($thenPresent && $elsePresent) {
$ifClause = 'ifClause' + $lvl;
out += ' var ' + ($ifClause) + ' = \'else\'; ';
} else {
$ifClause = '\'else\'';
}
out += ' } ';
}
out += ' if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */
if (it.createErrors !== false) {
out += ' { keyword: \'' + ('if') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { failingKeyword: ' + ($ifClause) + ' } ';
if (it.opts.messages !== false) {
out += ' , message: \'should match "\' + ' + ($ifClause) + ' + \'" schema\' ';
}
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; ';
}
}
out += ' } ';
if ($breakOnError) {
out += ' else { ';
}
out = it.util.cleanUpCode(out);
} else {
if ($breakOnError) {
out += ' if (true) { ';
}
}
return out;
}
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