diff --git a/README.md b/README.md index 25978e1..d76f6a2 100644 --- a/README.md +++ b/README.md @@ -393,6 +393,7 @@ Here's a list of currently supported helpers: |isUUID(paramName [, version])| check if the string is a UUID (version 3, 4 or 5). | |matches(paramName, pattern [, modifiers])| check if string matches the pattern. Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`. | |isPlainObject(paramName)| check if the current param is a plain object. | +| isLength(paramName, options) | check if the string's length falls in a range. Note: this function takes into account surrogate pairs. | ### Not currently supported These are a few other helpers avaliable in [validator.js](https://github.com/chriso/validator.js) that could be used in property-validator. @@ -417,7 +418,6 @@ Feel free to submit a PR if you need any of these functions. | isISBN(paramName [, version]) | check if the string is an ISBN (version 10 or 13). | | isISIN(paramName) | check if the string is an [ISIN][ISIN] (stock/security identifier). | | isISO8601(paramName) | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date. | -| isLength(paramName, min [, max]) | check if the string's length falls in a range. Note: this function takes into account surrogate pairs. | | isLowercase(paramName) | check if the string is lowercase. | | isMACAddress(paramName) | check if the string is a MAC address. | | isMobilePhone(paramName, locale) | check if the string is a mobile phone number, (locale is one of `['zh-CN', 'zh-TW', 'en-ZA', 'en-AU', 'en-HK', 'pt-PT', 'fr-FR', 'el-GR', 'en-GB', 'en-US', 'en-ZM', 'ru-RU', 'nb-NO', 'nn-NO', 'vi-VN', 'en-NZ', 'en-IN']`). | diff --git a/lib/validations.js b/lib/validations.js index 7806886..72a01fd 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -31,7 +31,7 @@ function checkParam(paramName, message, validator) { return { field: paramName, message: message, - result: validator.apply(null, [param + ""].concat(extraArgs)) + result: (typeof param !== 'undefined') && validator.apply(null, [param + ""].concat(extraArgs)) } } } @@ -232,6 +232,19 @@ validations.isPlainObject = function isPlainObject(paramName, customMessage) { } } +validations.isLength = validations.length = function isLength(paramName, options, customMessage) { + var errMessage = message(paramName, "length should be equals or greater then " + options.min + " and equals or less then " + options.max); + if (typeof options.min === 'undefined') errMessage = message(paramName, "length should be equals or less then " + options.max); + if (typeof options.max === 'undefined') errMessage = message(paramName, "length should be equals or greater then " + options.min); + + return checkParam( + paramName, + customMessage || errMessage, + validator.isLength, + options + ); +} + // TODO: Implement these validators // // isAfter(paramName [, date]) @@ -251,7 +264,6 @@ validations.isPlainObject = function isPlainObject(paramName, customMessage) { // isISBN(paramName [, version]) // isISIN(paramName) // isISO8601(paramName) -// isLength(paramName, min [, max]) // isLowercase(paramName) // isMACAddress(paramName) // isMobilePhone(paramName, locale) diff --git a/test/validation_test.js b/test/validation_test.js index 4417d79..4c66165 100644 --- a/test/validation_test.js +++ b/test/validation_test.js @@ -337,4 +337,16 @@ describe('Validation Helpers', function() { f(v.isPlainObject('i')({ i: 'bla' })); m(v.isPlainObject('i')({ i: 'test' }), '"i" should be a plain object'); }); + + it('isLength', function() { + t(v.isLength('i', {min: 9})({ i: 'something' })); + t(v.isLength('i', {max: 9})({ i: 'something' })); + t(v.isLength('i', {min: 9, max: 9})({ i: 'something' })); + f(v.isLength('i', {min: 10})({ i: 'something' })); + f(v.isLength('i', {max: 8})({ i: 'something' })); + f(v.isLength('i', {min: 10, max: 9})({ i: 'something' })); + m(v.isLength('i', {min: 10})({ i: 'something' }), '"i" length should be equals or greater then 10'); + m(v.isLength('i', {max: 5})({ i: 'something' }), '"i" length should be equals or less then 5'); + m(v.isLength('i', {min: 10, max: 5})({ i: 'something' }), '"i" length should be equals or greater then 10 and equals or less then 5'); + }) });