Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the execution order of validations - conform, format, enum #115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions lib/revalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,12 @@
}

if (value === undefined) {
if (schema.required && schema.type !== 'any') {
var isRequired = schema.required;
if (typeof isRequired === 'function') {
isRequired = isRequired.bind(object)();
}

if (isRequired && schema.type !== 'any') {
return error('required', property, undefined, schema, errors);
} else {
return;
Expand All @@ -275,27 +280,6 @@
}
}

if (schema.format && options.validateFormats) {
format = schema.format;

if (options.validateFormatExtensions) { spec = validate.formatExtensions[format] }
if (!spec) { spec = validate.formats[format] }
if (!spec) {
if (options.validateFormatsStrict) {
return error('format', property, value, schema, errors);
}
}
else {
if (!spec.test(value)) {
return error('format', property, value, schema, errors);
}
}
}

if (schema['enum'] && schema['enum'].indexOf(value) === -1) {
error('enum', property, value, schema, errors);
}

// Dependencies (see 5.8)
if (typeof schema.dependencies === 'string' &&
object[schema.dependencies] === undefined) {
Expand All @@ -317,8 +301,6 @@
checkType(value, schema.type, function(err, type) {
if (err) return error('type', property, typeof value, schema, errors);

constrain('conform', value, function (a, e) { return e(a, object) });

switch (type || (isArray(value) ? 'array' : typeof value)) {
case 'string':
constrain('allowEmpty', value, function (a, e) { return e ? e : a !== '' });
Expand Down Expand Up @@ -348,7 +330,12 @@
var nestedErrors;
for (var i = 0, l = a.length; i < l; i++) {
nestedErrors = [];
validateProperty(object, a[i], property, e, options, nestedErrors);
var newE = e;
if (typeof e === 'function') {
newE = e(a[i]);
}

validateProperty(object, a[i], property, newE, options, nestedErrors);
nestedErrors.forEach(function (err) {
err.property =
(property ? property + '.' : '') +
Expand Down Expand Up @@ -386,10 +373,36 @@
});
nestedErrors.unshift(0, 0);
Array.prototype.splice.apply(errors, nestedErrors);

constrain('minItems', value, function (a, e) { return Object.keys(a).length >= e });
constrain('maxItems', value, function (a, e) { return Object.keys(a).length <= e });
}
break;
}

constrain('conform', value, function (a, e) { return e(a, object) });
});

if (schema.format && options.validateFormats) {
format = schema.format;

if (options.validateFormatExtensions) { spec = validate.formatExtensions[format] }
if (!spec) { spec = validate.formats[format] }
if (!spec) {
if (options.validateFormatsStrict) {
return error('format', property, value, schema, errors);
}
}
else {
if (!spec.test(value)) {
return error('format', property, value, schema, errors);
}
}
}

if (schema['enum'] && schema['enum'].indexOf(value) === -1) {
error('enum', property, value, schema, errors);
}
}

function checkType(val, type, callback) {
Expand Down
9 changes: 9 additions & 0 deletions test/validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ vows.describe('revalidator', {
},
publisher: {
type: 'object',
minItems: 2,
properties: {
name: { type: 'string' },
agents: {
Expand Down Expand Up @@ -633,6 +634,14 @@ vows.describe('revalidator', {
},
"return an object with `valid` set to false": assertInvalid
},
"and if it has a incorrect property (< minItems)": {
topic: function (object, schema) {
object = clone(object);
delete object.publisher.name;
return revalidator.validate(object, schema);
},
"return an object with `valid` set to false": assertInvalid
},
"and if it has a incorrect unique array property": {
topic: function (object, schema) {
object = clone(object);
Expand Down