Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 467f4e7

Browse files
committedApr 26, 2016
Merge 499f51a into 6d99563
2 parents 6d99563 + 499f51a commit 467f4e7

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
 

‎specs/validators/each-spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
describe('validators.each', function() {
2+
var each = validate.validators.each.bind(validate.validators.each);
3+
4+
var isPositive = function(number) {
5+
if (number > 0) {
6+
return undefined;
7+
} else {
8+
return 'negative';
9+
}
10+
};
11+
12+
afterEach(function() {
13+
delete validate.validators.each.message;
14+
delete validate.validators.each.options;
15+
});
16+
17+
it("allows undefined values", function() {
18+
expect(each(null, {})).not.toBeDefined();
19+
expect(each(undefined, {})).not.toBeDefined();
20+
});
21+
22+
it("does not allow values that aren't arrays", function() {
23+
expect(each({}, {})).toBeDefined();
24+
expect(each(function () {}, {})).toBeDefined();
25+
expect(each("", {})).toBeDefined();
26+
expect(each(1, {})).toBeDefined();
27+
expect(each(true, {})).toBeDefined();
28+
});
29+
30+
it("has a default error message", function() {
31+
expect(each({}, {})).toEqual("must be an array");
32+
});
33+
34+
it("allows for a message to be attached to the validator", function() {
35+
var validatorMessage = "validatorMessage";
36+
validate.validators.each.message = validatorMessage;
37+
expect(each({}, {})).toEqual(validatorMessage);
38+
});
39+
40+
it("allows for a message to be passed as an option to override ", function() {
41+
var optionMessage = "optionMessage";
42+
validate.validators.each.message = "validatorMessage";
43+
expect(each({}, {message : optionMessage})).toEqual(optionMessage);
44+
});
45+
46+
it("accepts the value if no validator function is provided", function () {
47+
expect(each([], {})).not.toBeDefined();
48+
expect(each([], {validator : {}})).not.toBeDefined();
49+
expect(each([], {validator : ""})).not.toBeDefined();
50+
expect(each([], {validator : 1})).not.toBeDefined();
51+
expect(each([], {validator : []})).not.toBeDefined();
52+
expect(each([], {validator : true})).not.toBeDefined();
53+
expect(each([], {validator : null})).not.toBeDefined();
54+
});
55+
56+
it("accepts an empty array", function () {
57+
expect(each([], {validator : function () {}})).not.toBeDefined();
58+
expect(each([], {validator : function () {return 'error';}})).not.toBeDefined();
59+
});
60+
61+
it("accepts a valid array", function () {
62+
var array = [1, 2, 3, 4, 5];
63+
expect(each(array, {validator : isPositive})).not.toBeDefined();
64+
});
65+
66+
it("returns an array of errors if anything fails", function () {
67+
var array = [-1, 2, 3, 4, 5];
68+
expect(each(array, {validator : isPositive})).toEqual(['negative', undefined, undefined, undefined, undefined]);
69+
});
70+
71+
});

‎validate.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,38 @@
11271127
if (!PATTERN.exec(value)) {
11281128
return message;
11291129
}
1130+
},
1131+
1132+
each: function(value, options) {
1133+
// Allow null and undefined values
1134+
if (!v.isDefined(value)) {
1135+
return;
1136+
}
1137+
1138+
options = v.extend({}, this.options, options);
1139+
1140+
if (!v.isArray(value)) {
1141+
return options.message || this.message || "must be an array";
1142+
}
1143+
1144+
if (!v.isFunction(options.validator)) {
1145+
return;
1146+
}
1147+
1148+
var errors = [];
1149+
var numErrors = 0;
1150+
value.forEach(function validateEach (data) {
1151+
var error = options.validator(data);
1152+
errors.push(error);
1153+
if (error) {
1154+
numErrors++;
1155+
}
1156+
});
1157+
if (numErrors > 0) {
1158+
return errors;
1159+
} else {
1160+
return;
1161+
}
11301162
}
11311163
};
11321164

3 commit comments

Comments
 (3)

pradeepkchandra commented on Feb 28, 2017

@pradeepkchandra

Could you please explain how to define the constraints for the array

pradeepkchandra commented on Feb 28, 2017

@pradeepkchandra

do i have to provide constraints for each object in the array?
for me, the items in the array are dynamic based on the selected item from the dropdown

jpbufe3 commented on Feb 28, 2017

@jpbufe3
Author

the each validator is designed to have a single set of validation constraints that applies to all of the individual values in an array. so you would, for example, specify that each value in the array must be a string with length less than 1024.

for further discussion, please go to #124

This repository has been archived.