fixed required for inner level with many properties, closes #19

master
Evgeny Poberezkin 2015-07-20 17:41:06 +01:00
parent 0357b8221b
commit af46a38428
4 changed files with 80 additions and 10 deletions

View File

@ -12,7 +12,8 @@
{{## def.setupLoop:
var schema{{=$lvl}} = validate.schema{{=$schemaPath}};
{{
var $propertyPath = ' + schema' + $lvl + '[i] + '
var $i = 'i' + $lvl
, $propertyPath = ' + schema' + $lvl + '[' + $i + '] + '
, $missingProperty = '\' + "\'"' + $propertyPath + '"\'" + \'';
it.errorPath = ($currentErrorPath + ' + "[\'"' + $propertyPath + '"\']"').replace('" + "', '');
}}
@ -35,8 +36,8 @@
{{??}}
{{# def.setupLoop }}
for (var i = 0; i < schema{{=$lvl}}.length; i++) {
var {{=$valid}} = data[schema{{=$lvl}}[i]] !== undefined;
for (var {{=$i}} = 0; {{=$i}} < schema{{=$lvl}}.length; {{=$i}}++) {
var {{=$valid}} = {{=$data}}[schema{{=$lvl}}[{{=$i}}]] !== undefined;
if (!{{=$valid}}) break;
}
@ -58,8 +59,8 @@
{{??}}
{{# def.setupLoop }}
for (var i = 0; i < schema{{=$lvl}}.length; i++) {
if (data[schema{{=$lvl}}[i]] === undefined) {
for (var {{=$i}} = 0; {{=$i}} < schema{{=$lvl}}.length; {{=$i}}++) {
if ({{=$data}}[schema{{=$lvl}}[{{=$i}}]] === undefined) {
{{# def.error:'required' }}
}
}

View File

@ -50,10 +50,11 @@ module.exports = function anonymous(it) {
out += ' } else { ';
} else {
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; ';
var $propertyPath = ' + schema' + $lvl + '[i] + ',
var $i = 'i' + $lvl,
$propertyPath = ' + schema' + $lvl + '[' + $i + '] + ',
$missingProperty = '\' + "\'"' + $propertyPath + '"\'" + \'';
it.errorPath = ($currentErrorPath + ' + "[\'"' + $propertyPath + '"\']"').replace('" + "', '');
out += ' for (var i = 0; i < schema' + ($lvl) + '.length; i++) { var ' + ($valid) + ' = data[schema' + ($lvl) + '[i]] !== undefined; if (!' + ($valid) + ') break; } if (!' + ($valid) + ') { ';
out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < schema' + ($lvl) + '.length; ' + ($i) + '++) { var ' + ($valid) + ' = ' + ($data) + '[schema' + ($lvl) + '[' + ($i) + ']] !== undefined; if (!' + ($valid) + ') break; } if (!' + ($valid) + ') { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' ';
if (it.opts.verbose) {
@ -99,10 +100,11 @@ module.exports = function anonymous(it) {
}
} else {
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; ';
var $propertyPath = ' + schema' + $lvl + '[i] + ',
var $i = 'i' + $lvl,
$propertyPath = ' + schema' + $lvl + '[' + $i + '] + ',
$missingProperty = '\' + "\'"' + $propertyPath + '"\'" + \'';
it.errorPath = ($currentErrorPath + ' + "[\'"' + $propertyPath + '"\']"').replace('" + "', '');
out += ' for (var i = 0; i < schema' + ($lvl) + '.length; i++) { if (data[schema' + ($lvl) + '[i]] === undefined) { ';
out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < schema' + ($lvl) + '.length; ' + ($i) + '++) { if (' + ($data) + '[schema' + ($lvl) + '[' + ($i) + ']] === undefined) { ';
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' ';
if (it.opts.verbose) {

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.6.1",
"version": "0.6.2",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"scripts": {

View File

@ -0,0 +1,67 @@
[
{
"description": "Required for many properties in inner level (#19)",
"schema": {
"type": "array",
"items": {
"type": "object",
"required": [
"p1",
"p2",
"p3",
"p4",
"p5",
"p6",
"p7",
"p8",
"p9",
"p10",
"p11",
"p12",
"p13",
"p14",
"p15",
"p16",
"p17",
"p18",
"p19",
"p20",
"p21",
"p22"
]
}
},
"tests": [
{
"description": "valid",
"data": [
{
"p1": "test",
"p2": "test",
"p3": "test",
"p4": "test",
"p5": "test",
"p6": "test",
"p7": "test",
"p8": "test",
"p9": "test",
"p10": "test",
"p11": "test",
"p12": "test",
"p13": "test",
"p14": "test",
"p15": "test",
"p16": "test",
"p17": "test",
"p18": "test",
"p19": "test",
"p20": "test",
"p21": "test",
"p22": "test"
}
],
"valid": true
}
]
}
]