Skip to content

Commit

Permalink
Add allOf rule ealush#521
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 29, 2020
1 parent 7412129 commit 819ab2f
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions jsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"prettier-watch": "onchange '**/*.js' '**/*.json' -- prettier --write {{changed}}",
"dev": "onchange -i './packages/**/src/**/*.js' -- yarn genJSConfig",
"genJSConfig": "node ./scripts/genJsconfig",
"test": "jest --projects ./packages/*",
"test": "jest --projects ./packages/n4s",
"lint": "eslint . --ignore-path .gitignore",
"pretest": "yarn genJSConfig",
"docs": "node scripts/release/steps/updateDocs"
Expand Down
11 changes: 10 additions & 1 deletion packages/n4s/docs/compound.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
Alongside the list of rules that only accept data provided by the user, enforce also supports compound rules - these are rules that accept other rules as their arguments. These rules let you validate more complex scenarios with the ergonomics of enforce.

- [enforce.anyOf() - either/or validations](#anyof)
- [enforce.allOf() - all/and validations](#anyof)
- [enforce.shape() - Object's shape matching](#shape)
- [enforce.optional() - nullable keys](#optional)
- [enforce.optional() - nullable keys](#optional)
- [enforec.loose() - loose shape matching](#loose)
- [enforce.isArrayOf() - array shape matching](#isarrayof)

Expand All @@ -16,6 +17,14 @@ Sometimes a value has more than one valid possibilities, `any` lets us validate
enforce(value).anyOf(enforce.isString(), enforce.isArray()).isNotEmpty();
// A valid value would either an array or a string.
```
## enforce.allOf() - all/and validations :id=allOf

Sometimes we need to make sure that a value for a set of rules, `all` lets us validate that a value passes _all_ of the supplied rules.

```js
enforce(value).allOf(enforce.isString(), enforce.longerThen(5));
// A valid is string and longer then 5.
```

## enforce.shape() - Lean schema validation. :id=shape

Expand Down
2 changes: 1 addition & 1 deletion packages/n4s/docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Username('1234'); // throws
Username('ab'); // throws
```

You can also use templates inside other compound rules, such as `shape`, `isArrayOf` or `anyOf`.
You can also use templates inside other compound rules, such as `shape`, `isArrayOf` ,`anyOf` or `allOf`.

```js
enforce({
Expand Down
62 changes: 62 additions & 0 deletions packages/n4s/src/enforce/compounds/__tests__/allOf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import allOf from 'allOf';
import enforce from 'enforce';

describe('allOf validation', () => {
describe('Base behavior', () => {
it('Should fail when at least one rule fail', () => {
expect(allOf('test', enforce.isString(), enforce.longerThan(10))).toBe(
false
);
});
it('Should succeed when all of the rules applies', () => {
expect(
allOf('test', enforce.isString(), enforce.longerThan(3))
).toBe(true);
});
it('Should fail with no rules', () => {
expect(allOf(3)).toBe(true);
});
});

describe('As part of enforce', () => {
const User = enforce.template(
enforce.loose({
id: enforce.isNumber(),
name: enforce.shape({
first: enforce.isString(),
last: enforce.isString(),
middle: enforce.optional(enforce.isString()),
}),
})
);

const DisabledAccount = enforce.template(
enforce.loose({
disabled: enforce.equals(true)
})
)

it('Should validate allof the rules correctly', () => {
enforce(value).allOf(
{ id: 123, name : { first: 'Albert', last: 'Einstein' } },
{ disabled: true }
);
});

it('Should throw if one of the rules fail', () =>{
expect( () => {
enforce(value).allOf(
{ id: 123, name : { first: 'Albert', last: 0 } },
{ disabled: true }
);
}).toThrow();

expect( () => {
enforce(value).allOf(
{ id: 123, name : { first: 'Albert', last: 'Einstein' } },
{ disabled: false }
);
}).toThrow();
} )
});
});
10 changes: 10 additions & 0 deletions packages/n4s/src/enforce/compounds/allOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import runLazyRules from 'runLazyRules';
import { withFirst } from 'withArgs';

function allOf(value, rules) {
return (
!rules.length || rules.every(ruleGroup => runLazyRules(ruleGroup, value))
);
}

export default withFirst(anyOf);
2 changes: 2 additions & 0 deletions packages/n4s/src/enforce/compounds/compounds.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import anyOf from 'anyOf';
import allOf from 'allOf';
import isArrayOf from 'isArrayOf';
import optional from 'optional';
import { shape, loose } from 'shape';

export default {
anyOf,
allOf,
isArrayOf,
loose,
optional,
Expand Down
4 changes: 4 additions & 0 deletions packages/vest/src/__tests__/test_types/fixtures/enforce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@ enforce
// anyOf
enforce(1).anyOf(enforce.isNumber(), enforce.isArray());
enforce.anyOf(enforce.isNumber(), enforce.isArray()).test(1);

// allOf
enforce('hello').allOf(enforce.isString(), enforce.longerThan(3));
enforce.allOf(enforce.isString(), enforce.longerThan(3)).test('hello');
2 changes: 2 additions & 0 deletions packages/vest/src/typings/enforce.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ interface IEnforceRules<T = {}> {
) => RuleReturn<T>;
isArrayOf: CompoundListOfRules<T>;
anyOf: CompoundListOfRules<T>;
allOf: CompoundListOfRules<T>;
}

interface IEnforce {
Expand Down Expand Up @@ -194,6 +195,7 @@ type TEnforceLazy = {
optional: LazyCopmoundListOfRules;
isArrayOf: LazyCopmoundListOfRules;
anyOf: LazyCopmoundListOfRules;
allOf: LazyCopmoundListOfRules;
};

export type TEnforce = IEnforce & TEnforceLazyReturn;

0 comments on commit 819ab2f

Please sign in to comment.