Skip to content

Commit

Permalink
Added support for isLength assertion. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
DJWassink authored and nettofarah committed Apr 6, 2017
1 parent cee101f commit e292386
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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']`). |
Expand Down
16 changes: 14 additions & 2 deletions lib/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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])
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/validation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
});

0 comments on commit e292386

Please sign in to comment.