From 4eb5c31f36e24d02c974cee1095a1efb3cdd8b88 Mon Sep 17 00:00:00 2001 From: Ali Yousuf Date: Fri, 8 Jan 2016 15:11:49 +0500 Subject: [PATCH 1/4] Changed the execution order of validations - conform, format, enum --- lib/revalidator.js | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 58476b0..8c9bad6 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -275,27 +275,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) { @@ -317,8 +296,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 !== '' }); @@ -389,7 +366,30 @@ } 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) { From b1d1e17c1ff550076b9e79c118e2a4e256911b83 Mon Sep 17 00:00:00 2001 From: Ali Yousuf Date: Sat, 12 Nov 2016 01:04:11 +0500 Subject: [PATCH 2/4] Support dynamic rules for array items --- lib/revalidator.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 8c9bad6..e767530 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -325,7 +325,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 + '.' : '') + From 048010c0b50de5a0b2b8e58f34c0c4c790957fbf Mon Sep 17 00:00:00 2001 From: Ali Yousuf Date: Thu, 15 Dec 2016 20:25:58 +0500 Subject: [PATCH 3/4] Support required value as function --- lib/revalidator.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index e767530..55f12b4 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -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; From fb29202db7dd8b23c0dae2e105d95b2d72aa0017 Mon Sep 17 00:00:00 2001 From: Ali Yousuf Date: Fri, 3 Feb 2017 17:06:36 +0500 Subject: [PATCH 4/4] Add object minItems and maxItems valiedation feature --- lib/revalidator.js | 3 +++ test/validator-test.js | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/revalidator.js b/lib/revalidator.js index 55f12b4..d3eed5f 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -373,6 +373,9 @@ }); 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; } diff --git a/test/validator-test.js b/test/validator-test.js index 195b690..bd1ca3a 100644 --- a/test/validator-test.js +++ b/test/validator-test.js @@ -530,6 +530,7 @@ vows.describe('revalidator', { }, publisher: { type: 'object', + minItems: 2, properties: { name: { type: 'string' }, agents: { @@ -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);