This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
467f4e7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain how to define the constraints for the array
467f4e7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
467f4e7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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