Comparing version 0.6.0 to 0.6.1
@@ -15,71 +15,111 @@ 'use strict'; | ||
} | ||
if ($schema.length <= 100) { | ||
out += ' if ( '; | ||
var arr1 = $schema; | ||
if (arr1) { | ||
var $property, $i = -1, | ||
l1 = arr1.length - 1; | ||
while ($i < l1) { | ||
$property = arr1[$i += 1]; | ||
if ($i) { | ||
out += ' || '; | ||
var $currentErrorPath = it.errorPath; | ||
if ($breakOnError) { | ||
out += ' var missing' + ($lvl) + '; '; | ||
if ($schema.length <= 20) { | ||
out += ' if ( '; | ||
var arr1 = $schema; | ||
if (arr1) { | ||
var $property, $i = -1, | ||
l1 = arr1.length - 1; | ||
while ($i < l1) { | ||
$property = arr1[$i += 1]; | ||
if ($i) { | ||
out += ' || '; | ||
} | ||
var $prop = it.util.getProperty($property); | ||
out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = \'' + ($prop) + '\') ) '; | ||
} | ||
out += ' ' + ($data) + (it.util.getProperty($property)) + ' === undefined '; | ||
} | ||
} | ||
out += ') { '; | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", ")); | ||
if ($schema.length > 7) { | ||
out += '...'; | ||
out += ') { '; | ||
var $propertyPath = ' + missing' + $lvl, | ||
$missingProperty = '\'' + $propertyPath + ' + \''; | ||
it.errorPath = $currentErrorPath + $propertyPath; | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
} | ||
out += ' are required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
out += ' } else { '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", ")); | ||
if ($schema.length > 7) { | ||
out += '...'; | ||
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; '; | ||
var $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) + ') { '; | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
} | ||
out += ' are required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' } else { '; | ||
} | ||
out += ' } '; | ||
if ($breakOnError) { | ||
out += ' else { '; | ||
} | ||
} else { | ||
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; for (var i = 0; i < schema' + ($lvl) + '.length; i++) { var ' + ($valid) + ' = data[schema' + ($lvl) + '[i]] !== undefined; if (!' + ($valid) + ') break; } if (!' + ($valid) + ') { '; | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", ")); | ||
if ($schema.length > 7) { | ||
out += '...'; | ||
if ($schema.length <= 10) { | ||
var arr2 = $schema; | ||
if (arr2) { | ||
var $property, $i = -1, | ||
l2 = arr2.length - 1; | ||
while ($i < l2) { | ||
$property = arr2[$i += 1]; | ||
var $prop = it.util.getProperty($property), | ||
$missingProperty = $prop; | ||
it.errorPath = ($currentErrorPath + ' + "' + $prop + '"').replace('" + "', ''); | ||
out += ' if (' + ($data) + ($prop) + ' === undefined) { '; | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
} | ||
out += ' } '; | ||
} | ||
} | ||
out += ' are required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", ")); | ||
if ($schema.length > 7) { | ||
out += '...'; | ||
out += ' var schema' + ($lvl) + ' = validate.schema' + ($schemaPath) + '; '; | ||
var $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) { '; | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
} | ||
out += ' are required\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' } } '; | ||
} | ||
out += ' } '; | ||
if ($breakOnError) { | ||
out += ' else { '; | ||
} | ||
} | ||
it.errorPath = $currentErrorPath; | ||
return out; | ||
} |
{ | ||
"name": "ajv", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Another JSON Schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -194,2 +194,10 @@ # ajv - Another JSON Schema Validator | ||
##### 0.6.1 | ||
Errors for "required" keyword validation include missing properties | ||
Better references resolution in schemas without IDs | ||
##### 0.5.9 | ||
@@ -196,0 +204,0 @@ |
@@ -13,3 +13,3 @@ 'use strict'; | ||
ajv = Ajv(); | ||
fullAjv = Ajv({ allErrors: true }); | ||
fullAjv = Ajv({ allErrors: true, beautify: true }); | ||
}); | ||
@@ -69,2 +69,59 @@ | ||
it('errors for required should include missing property in dataPath', function() { | ||
var schema = { | ||
required: ['foo', 'bar', 'baz'] | ||
}; | ||
var data = { foo: 1, bar: 2, baz: 3 } | ||
, invalidData1 = { foo: 1, baz: 3 } | ||
, invalidData2 = { bar: 2 }; | ||
var validate = ajv.compile(schema); | ||
shouldBeValid(validate, data); | ||
shouldBeInvalid(validate, invalidData1); | ||
shouldBeError(validate.errors[0], 'required', '.bar', 'property .bar is required'); | ||
shouldBeInvalid(validate, invalidData2); | ||
shouldBeError(validate.errors[0], 'required', '.foo', 'property .foo is required'); | ||
var fullValidate = fullAjv.compile(schema); | ||
shouldBeValid(fullValidate, data); | ||
shouldBeInvalid(fullValidate, invalidData1); | ||
shouldBeError(fullValidate.errors[0], 'required', '.bar', 'property .bar is required'); | ||
shouldBeInvalid(fullValidate, invalidData2, 2); | ||
shouldBeError(fullValidate.errors[0], 'required', '.foo', 'property .foo is required'); | ||
shouldBeError(fullValidate.errors[1], 'required', '.baz', 'property .baz is required'); | ||
}); | ||
it('required validation and errors for large data/schemas', function() { | ||
var schema = { required: [] } | ||
, data = {} | ||
, invalidData1 = {} | ||
, invalidData2 = {}; | ||
for (var i=0; i<100; i++) { | ||
schema.required.push(''+i); // properties from '0' to '99' are required | ||
data[i] = invalidData1[i] = invalidData2[i] = i; | ||
} | ||
delete invalidData1[1]; // property '1' will be missing | ||
delete invalidData2[2]; // properties '2' and '198' will be missing | ||
delete invalidData2[98]; | ||
var validate = ajv.compile(schema); | ||
shouldBeValid(validate, data); | ||
shouldBeInvalid(validate, invalidData1); | ||
shouldBeError(validate.errors[0], 'required', "['1']", "property '1' is required"); | ||
shouldBeInvalid(validate, invalidData2); | ||
shouldBeError(validate.errors[0], 'required', "['2']", "property '2' is required"); | ||
var fullValidate = fullAjv.compile(schema); | ||
shouldBeValid(fullValidate, data); | ||
shouldBeInvalid(fullValidate, invalidData1); | ||
shouldBeError(fullValidate.errors[0], 'required', "['1']", "property '1' is required"); | ||
shouldBeInvalid(fullValidate, invalidData2, 2); | ||
shouldBeError(fullValidate.errors[0], 'required', "['2']", "property '2' is required"); | ||
shouldBeError(fullValidate.errors[1], 'required', "['98']", "property '98' is required"); | ||
}); | ||
function testSchema1(schema) { | ||
@@ -99,7 +156,9 @@ _testSchema1(ajv, schema); | ||
function shouldBeError(error, keyword, dataPath) { | ||
function shouldBeError(error, keyword, dataPath, message) { | ||
error.keyword .should.equal(keyword); | ||
error.dataPath .should.equal(dataPath); | ||
error.message .should.be.a('string'); | ||
error.dataPath .should.equal(dataPath); | ||
if (message !== undefined) | ||
error.message .should.equal(message); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
362761
9670
232