From 70622f36a128a4c7eb065beec9ce2fd6f00e4ccf Mon Sep 17 00:00:00 2001 From: Duy Nguyen Date: Tue, 14 Jun 2016 11:34:56 +0700 Subject: [PATCH] fix arrLength validation --- README.md | 8 ++++++++ lib/field.js | 10 +++++----- test/validate.test.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b93e26d..e61c433 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,14 @@ Use "%s" in the message to have the field name or label printed in the message: Checks the field value max length. + arrLength(minLength, maxLength[, messages]) + - minLength (integer): The min character to test for. + - maxLength (integer): The max character to test for. + - messages(array): Custom messages for min/max error + + For example ["Too short string", "Too long string"] + If messages is not provided, default will be ["%s is too short"", "%s is too long""] + Checks the field value in min/max length. *Other* diff --git a/lib/field.js b/lib/field.js index 47e6569..00dea5f 100644 --- a/lib/field.js +++ b/lib/field.js @@ -106,17 +106,17 @@ Field.prototype.array = function () { return this.array(); }; -Field.prototype.arrLength = function (from, to) { - return this.add(function (arr) { +Field.prototype.arrLength = function (from, to, messages) { + return this.add(function (value) { if (value.length < from) { - return { error: message || e.message || "%s is too short" }; + return { error: (messages && messages[0]) || "%s is too short" }; } if (value.length > to) { - return { error: message || e.message || "%s is too long" }; + return { error: (messages && messages[1]) || "%s is too long" }; } return { valid: true }; }); -} +}; // HYBRID METHODS diff --git a/test/validate.test.js b/test/validate.test.js index 6773da1..93efa4b 100644 --- a/test/validate.test.js +++ b/test/validate.test.js @@ -722,5 +722,48 @@ module.exports = { validate("field[even][more][inner]").required().equals("fail") )(request, {}); assert.equal(request.form.errors.length, 2); + }, + + 'validation : arrLength : failed default message': function() { + var request = { body: { field: "value" }}; + form(validate("field").arrLength(10, 20))(request, {}); + assert.equal(request.form.errors.length, 1); + assert.equal(request.form.errors[0], "field is too short"); + + request = { body: { field: "value is so long so it is failed" }}; + form(validate("field").arrLength(4, 20))(request, {}); + assert.equal(request.form.errors.length, 1); + assert.equal(request.form.errors[0], "field is too long"); + + request = { body: { field: "value is so long so it is failed" }}; + form(validate("field").arrLength(10, 20, []))(request, {}); + assert.equal(request.form.errors.length, 1); + assert.equal(request.form.errors[0], "field is too long"); + }, + + 'validation : arrLength : failed custom message': function() { + var request = { body: { field: "value" }}; + form(validate("field").arrLength(10, 20, ["!!! %s SO SHORT!!!", "!!! %s SO LONG!!!"]))(request, {}); + assert.equal(request.form.errors.length, 1); + assert.equal(request.form.errors[0], "!!! field SO SHORT!!!"); + + request = { body: { field: "value is so long so it is failed" }}; + form(validate("field").arrLength(10, 20, ["!!! %s SO SHORT!!!", "!!! %s SO LONG!!!"]))(request, {}); + assert.equal(request.form.errors.length, 1); + assert.equal(request.form.errors[0], "!!! field SO LONG!!!"); + }, + + 'validation : arrLength : success': function() { + var request = { body: { field: "value" }}; + form(validate("field").arrLength(1, 5))(request, {}); + assert.equal(request.form.errors.length, 0); + + request = { body: { field: "value" }}; + form(validate("field").arrLength(1, 15, ["short", "long"]))(request, {}); + assert.equal(request.form.errors.length, 0); + + request = { body: { field: "" }}; + form(validate("field").arrLength(0, 15, ["short", "long"]))(request, {}); + assert.equal(request.form.errors.length, 0); } };