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.
feat(validators): Add validator for each object in an array
Added a new validator, `each`, which applies a validation function provided in the options to each of the objects in the array being validated. Closes #1
- Loading branch information
John Bufe
committed
Apr 26, 2016
1 parent
6d99563
commit 91169e3
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