Skip to content

Commit

Permalink
add isPlainObject/isArray validation resolves #9
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrmatos committed Mar 9, 2016
1 parent 4037d11 commit 8e431b5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/validations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var get = require('lodash/get');
var isString = require('lodash/isString');
var isPlainObject = require('lodash/isPlainObject');
var includes = require('lodash/includes');
var validator = require('validator');

Expand Down Expand Up @@ -88,6 +89,17 @@ validations.isAlphanumeric = function isAlphanumeric(paramName, customMessage) {
);
}

validations.isArray = function isPlainObject(paramName, customMessage) {
return function(params) {

return {
field: paramName,
message: customMessage || message(paramName, "must be an array"),
result: Array.isArray(get(params, paramName))
}
}
}

validations.isCreditCard = function isCreditCard(paramName, customMessage) {
return checkParam(
paramName,
Expand Down Expand Up @@ -208,6 +220,17 @@ validations.isURL = function isURL(paramName, options, customMessage) {
);
}

validations.isPlainObject = function isPlainObjectValidator(paramName, customMessage) {
return function(params) {

return {
field: paramName,
message: customMessage || message(paramName, "must be a plain object"),
result: isPlainObject(get(params, paramName))
}
}
}

// TODO: Implement these validators
//
// isAfter(paramName [, date])
Expand Down
13 changes: 13 additions & 0 deletions test/validation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ describe('Validation Helpers', function() {
f(v.isAlphanumeric('i')({ i: '#@' }));
});

it('isArray', function() {
t(v.isArray('i')({ i: [1, 2, 3] }));
f(v.isArray('i')({ i: 'bla' }));

m(v.isArray('i')({ i: 'test' }), '"i" must be an array');
});

it('isCreditCard', function() {
t(v.isCreditCard('i')({ i: '375556917985515' }));
f(v.isCreditCard('i')({ i: '123123' }));
Expand Down Expand Up @@ -283,4 +290,10 @@ describe('Validation Helpers', function() {
f(v.uuid('i')({ i: 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3' }));
f(v.uuid('i')({ i: 'bla' }));
});

it('isPlainObject', function() {
t(v.isPlainObject('i')({ i: { foo: true } }));
f(v.isPlainObject('i')({ i: 'bla' }));
m(v.isPlainObject('i')({ i: 'test' }), '"i" must be a plain object');
});
});

0 comments on commit 8e431b5

Please sign in to comment.