Skip to content
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

Merged
merged 9 commits into from
Nov 15, 2020
Merged
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
3 changes: 3 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"compounds": [
"./packages/n4s/src/enforce/compounds/compounds.js"
],
"isArrayOf": [
"./packages/n4s/src/enforce/compounds/isArrayOf.js"
],
"optional": [
"./packages/n4s/src/enforce/compounds/optional.js"
],
Expand Down
61 changes: 61 additions & 0 deletions packages/n4s/src/enforce/compounds/__tests__/isArrayOf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import enforce from 'enforce'
import isArrayOf from 'isArrayOf'

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],
enforce.greaterThan(2),
enforce.isString())
).toBe(true);

expect(isArrayOf([3,4,5,'six',7],
enforce.greaterThan(2),
enforce.isString())
).toBe(true);
});

describe('Tests for recursive call', ()=>{

it('Should return true if all elements are ture for one or more rules', () => {
expect(isArrayOf([3,4,5,['s','i','x'],7],
enforce.greaterThan(2),
enforce.isArrayOf(enforce.isString()))
).toBe(true);

expect(isArrayOf([[1,0,1,0],[0,0,1,0],[0,1,0,0]],
enforce.isArrayOf(enforce.equals(0),enforce.equals(1))
)).toBe(true);
});

it('Should return false if one element or more fails all rules', () => {
expect(
isArrayOf([3,4,5,['s','i','x'],7],
enforce.greaterThan(2),
enforce.isArrayOf(enforce.isNumber()))
).toBe(false);
});

expect(isArrayOf([[1,0,1,0],[0,0,1,'not 0/1'],[0,1,0,0]],
enforce.isArrayOf(enforce.equals(0),enforce.equals(1))
)).toBe(false);
});

describe('as part of enforce', () => {

it('should return silently when valid', () => {
enforce([1,2,'3']).isArrayOf(enforce.isNumber(), enforce.isString());
enforce([1,2,'3']).isArrayOf(enforce.isNumeric(), enforce.lessThan(5).greaterThan(0));
enforce([[0,1,0,1],[1,1,1,1],[0,1,1,0]]).isArrayOf(enforce.isArrayOf(enforce.equals(0),enforce.equals(1)).lengthEquals(4));
});

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()
expect(() => enforce([[0,1,0,1],[1,'not 0/1',1,1],[0,1,1,0]]).isArrayOf(enforce.isArrayOf(enforce.equals(0),enforce.equals(1))).lengthEquals(4)).toThrow()
expect(() => enforce([[0,1,0,1],[1,1,1],[0,1,1,0]]).isArrayOf(enforce.isArrayOf(enforce.equals(0),enforce.equals(1))).lengthEquals(4)).toThrow()
});

});

});
2 changes: 2 additions & 0 deletions packages/n4s/src/enforce/compounds/compounds.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import isArrayOf from 'isArrayOf';
import optional from 'optional';
import shape from 'shape';

export default {
isArrayOf,
optional,
shape,
};
7 changes: 7 additions & 0 deletions packages/n4s/src/enforce/compounds/isArrayOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { isNotArray } from 'isArray'
import runLazyRules from 'runLazyRules';

export default function isArrayOf(value, ...ruleChain) {
if (isNotArray(value)) { return false };
return ( value.every(element => ruleChain.some(rule => runLazyRules(rule, element) )));
};
1 change: 1 addition & 0 deletions packages/vest/src/typings/vest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ type TEnforceLazy = {
[key: string]: TEnforceLazy | TEnforceLazy[];
}) => TEnforceLazy;
optional: <T>(...rules: TEnforceLazy[]) => TEnforceLazy;
isArrayOf: <T>(...rules: TEnforceLazy[]) => TEnforceLazy;
};

declare module 'vest' {
Expand Down