-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added: isArrayOf rule [N4S] #488 #499
Changes from 4 commits
f557563
b2ffc97
cd5771c
ad85801
974a009
0859bf8
33ab442
d17d07b
1381890
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||
import { isArrayOf } from 'isArrayOf'; | ||||||
import enforse from 'enforce' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo 🙈
Suggested change
|
||||||
|
||||||
describe('Tests isArrayOf rule', () => { | ||||||
|
||||||
it('Should return true if all elements are ture for one or more rules', () => { | ||||||
expect(isArrayOf([3,4,5,'six',7], | ||||||
enforse.greaterThan(2), | ||||||
enforse.isString()) | ||||||
).toBe(true); | ||||||
}); | ||||||
|
||||||
|
||||||
it('Should return false if one element or more fails all rules', () => { | ||||||
expect(isArrayOf([3,4,5,['s','i','x'],7], | ||||||
enforse.greaterThan(2), | ||||||
enforse.isString()) | ||||||
).toBe(false); | ||||||
}); | ||||||
|
||||||
|
||||||
describe('Tests for recursive call', ()=>{ | ||||||
|
||||||
it('Should return true if all elements are ture for one or more rules', () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo 🙈
Suggested change
|
||||||
expect(isArrayOf([3,4,5,['s','i','x'],7], | ||||||
enforse.greaterThan(2), | ||||||
enforse.isArrayOf(enforse.isString())) | ||||||
).toBe(true); | ||||||
}); | ||||||
|
||||||
it('Should return false if one element or more fails all rules', () => { | ||||||
expect( | ||||||
isArrayOf([3,4,5,['s','i','x'],7], | ||||||
enforse.greaterThan(2), | ||||||
enforse.isArrayOf(enforse.isNumber())) | ||||||
).toBe(false); | ||||||
}); | ||||||
}); | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add one integration test that makes sure everything works together with enforce Here's an example, should probably be more thorough. describe('as part of enforce', () => {
it('should return silently when valid', () => {
enforce([1,2,'3']).isArrayOf(enforce.isNumber())
enforce([1,2,'3']).isArrayOf(enforce.isString())
enforce([1,2,'3']).isArrayOf(enforce.isNumeric(), enforce.lessThan(5).greaterThan(0))
});
it('should throw an exception when invalid', () => {
expect(() => enforce([1,2,'3']).isArrayOf(enforce.isNull())).toThrow()
expect(() => enforce([1,2,'3']).isArrayOf(enforce.isNumber(), enforce.greaterThan(5))).toThrow()
});
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the replay,
need to return silently? '3' isn't a number and '1','2' isn't a string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, right. You are correct. I just wrote it as an example. Should be more similar to what you wrote. |
||||||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,4 @@ | ||||||||||||||||||||
export function isArrayOf(value, ...rules) { | ||||||||||||||||||||
if (value.some(element => !rules.some(fn => fn(element)))) {return false}; | ||||||||||||||||||||
return true; | ||||||||||||||||||||
}; | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a couple of things I would consider changing in this function.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import { numberEquals, numberNotEquals } from 'numberEquals'; | |
import { shorterThan } from 'shorterThan'; | ||
import { shorterThanOrEquals } from 'shorterThanOrEquals'; | ||
import { startsWith, doesNotStartWith } from 'startsWith'; | ||
import { isArrayOf } from 'isArrayOf'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this file export would be more suitable with https://github.com/ealush/vest/blob/latest/packages/n4s/src/enforce/compounds/compounds.js |
||
|
||
export default function rules() { | ||
return { | ||
|
@@ -82,5 +83,6 @@ export default function rules() { | |
shorterThan, | ||
shorterThanOrEquals, | ||
startsWith, | ||
isArrayOf, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that you added it to compounds, there's no need to have it here. |
||
}; | ||
} |
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.