Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

feat(validators): Add validator for each object in an array #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions specs/validators/each-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
describe('validators.each', function() {
var each = validate.validators.each.bind(validate.validators.each);

var isPositive = function(number) {
if (number > 0) {
return undefined;
} else {
return 'negative';
}
};

afterEach(function() {
delete validate.validators.each.message;
delete validate.validators.each.options;
});

it("allows undefined values", function() {
expect(each(null, {})).not.toBeDefined();
expect(each(undefined, {})).not.toBeDefined();
});

it("does not allow values that aren't arrays", function() {
expect(each({}, {})).toBeDefined();
expect(each(function () {}, {})).toBeDefined();
expect(each("", {})).toBeDefined();
expect(each(1, {})).toBeDefined();
expect(each(true, {})).toBeDefined();
});

it("has a default error message", function() {
expect(each({}, {})).toEqual("must be an array");
});

it("allows for a message to be attached to the validator", function() {
var validatorMessage = "validatorMessage";
validate.validators.each.message = validatorMessage;
expect(each({}, {})).toEqual(validatorMessage);
});

it("allows for a message to be passed as an option to override ", function() {
var optionMessage = "optionMessage";
validate.validators.each.message = "validatorMessage";
expect(each({}, {message : optionMessage})).toEqual(optionMessage);
});

it("accepts the value if no validator function is provided", function () {
expect(each([], {})).not.toBeDefined();
expect(each([], {validator : {}})).not.toBeDefined();
expect(each([], {validator : ""})).not.toBeDefined();
expect(each([], {validator : 1})).not.toBeDefined();
expect(each([], {validator : []})).not.toBeDefined();
expect(each([], {validator : true})).not.toBeDefined();
expect(each([], {validator : null})).not.toBeDefined();
});

it("accepts an empty array", function () {
expect(each([], {validator : function () {}})).not.toBeDefined();
expect(each([], {validator : function () {return 'error';}})).not.toBeDefined();
});

it("accepts a valid array", function () {
var array = [1, 2, 3, 4, 5];
expect(each(array, {validator : isPositive})).not.toBeDefined();
});

it("returns an array of errors if anything fails", function () {
var array = [-1, 2, 3, 4, 5];
expect(each(array, {validator : isPositive})).toEqual(['negative', undefined, undefined, undefined, undefined]);
});

});
32 changes: 32 additions & 0 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,38 @@
if (!PATTERN.exec(value)) {
return message;
}
},

each: function(value, options) {
// Allow null and undefined values
if (!v.isDefined(value)) {
return;
}

options = v.extend({}, this.options, options);

if (!v.isArray(value)) {
return options.message || this.message || "must be an array";
}

if (!v.isFunction(options.validator)) {
return;
}

var errors = [];
var numErrors = 0;
value.forEach(function validateEach (data) {
var error = options.validator(data);
errors.push(error);
if (error) {
numErrors++;
}
});
if (numErrors > 0) {
return errors;
} else {
return;
}
}
};

Expand Down