Skip to content

Commit

Permalink
feat(n4s): deeply nested schema result (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jan 8, 2021
1 parent d6a13ba commit 2f948fc
Show file tree
Hide file tree
Showing 92 changed files with 2,971 additions and 876 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
jsconfig.json linguist-generated
jsconfig.json linguist-generated
rules.ts linguist-generated
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- release
- latest

jobs:
build:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
*.log
packages/vest/utilities/
packages/n4s/extended/
playground.mjs

# build artifacts
packages/*/dist
Expand All @@ -14,4 +15,4 @@ packages/*/*.d.ts
packages/*/*.mjs

# Unignore direct js files
!jest.config.js
!jest.config.js
4 changes: 2 additions & 2 deletions config/rollup/genConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ module.exports = function genConfig({
.reduce(
(configs, current) =>
configs.concat(
{ ...current, min: true },
{ ...current },
{ ...current, dev: true },
{ ...current, min: true }
{ ...current, dev: true }
),
[]
)
Expand Down
43 changes: 27 additions & 16 deletions jsconfig.json

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

6 changes: 6 additions & 0 deletions packages/__shared/src/hasOwnProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* A safe hasOwnProperty access
*/
export default function hasOwnProperty(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
4 changes: 4 additions & 0 deletions packages/__shared/src/sharedKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const HAS_WARNINGS = 'hasWarnings';
const HAS_ERRORS = 'hasErrors';

export { HAS_WARNINGS, HAS_ERRORS };
18 changes: 5 additions & 13 deletions packages/__shared/src/withArgs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import setFnName from 'setFnName';

/**
* ES5 Transpilation increases the size of spread arguments by a lot.
* Wraps a function and passes its spread params as an array.
Expand All @@ -8,16 +7,9 @@ import setFnName from 'setFnName';
* @param {String} [fnName]
* @return {Function}
*/
export default function withAgs(cb, fnName) {
return setFnName((...args) => cb(args), fnName || cb.name);
}

/**
* Spreads all the passed arguments, and forwards them as
* first arg, and the rest as an array.
*
* @param {Function} cb
*/
export function withFirst(cb) {
return withAgs(args => cb(args[0], args.slice(1)), cb.name);
export default function withArgs(cb, fnName) {
return setFnName((...args) => {
const right = args.splice(cb.length - 1);
return cb.apply(null, args.concat([right]));
}, fnName || cb.name);
}
14 changes: 14 additions & 0 deletions packages/n4s/config/jest/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
expect.extend({
toPass: res => ({
pass: res.pass,
message: () => 'enforceResult.pass failed validation',
}),
toPassWith: (enforcement, value) => {
return {
pass:
enforcement.run(value).pass === true &&
enforcement.test(value) === true,
message: () => 'enforceResult.pass failed validation',
};
},
});
2 changes: 1 addition & 1 deletion packages/n4s/config/rollup/enforce.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { packageDist, packageNames } = require('../../../../util');
export default genConfig({
libraryName: packageNames.N4S,
distPath: packageDist(packageNames.N4S),
input: 'enforce/enforce.js',
input: 'enforce.js',
});
11 changes: 5 additions & 6 deletions packages/n4s/docs/compound.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +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

## enforce.allOf() - all/and validations :id=allof

`allOf` lets us validate that a value passes _all_ of the supplied rules or templates.

enforce(value).allOf(
enforce.isArray(),
enforce.longerThan(2)
enforce.isArray(),
enforce.longerThan(2)
);

This can be even more useful when combined with shapes and templates:
Expand Down Expand Up @@ -107,7 +108,7 @@ In regular cases, a missing key in the data object would cause an error to be th

enforce.optional will pass validations of a key that's either not defined, undefined or null.

`enforce.optional` takes as its arguments all the rules that the value should pass - only if it is present. If it is not present in the data object.
`enforce.optional` takes as its arguments all the rules that the value must pass.

```js
enforce({
Expand Down Expand Up @@ -183,6 +184,4 @@ value = "1" -> ✅ (value is string)
value = [1, 2] -> ✅ (value is longer than 1)
value = "12" -> 🚨 (value is both a string and longer than 1)
*/

```

147 changes: 147 additions & 0 deletions packages/n4s/src/__tests__/__snapshots__/deep.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Deep enforcements Should match a failing snapshot 1`] = `
RuleResult {
"children": Object {
"age": RuleResult {
"failed": true,
"hasErrors": true,
"hasWarnings": false,
"warn": false,
},
"country": RuleResult {
"failed": true,
"hasErrors": true,
"hasWarnings": false,
"warn": false,
},
"user": RuleResult {
"failed": true,
"hasErrors": true,
},
},
"failed": true,
"hasErrors": true,
"hasWarnings": false,
}
`;

exports[`Deep enforcements Should match a partially failing shapshot 1`] = `
RuleResult {
"children": Object {
"age": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"country": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"user": RuleResult {
"children": Object {
"id": RuleResult {
"failed": true,
"hasErrors": true,
"hasWarnings": false,
"warn": false,
},
"name": RuleResult {
"children": Object {
"first": RuleResult {
"failed": true,
"hasErrors": true,
"hasWarnings": false,
"warn": false,
},
"last": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"middle": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
},
"failed": true,
"hasErrors": true,
"hasWarnings": false,
},
},
"failed": true,
"hasErrors": true,
"hasWarnings": false,
},
},
"failed": true,
"hasErrors": true,
"hasWarnings": false,
}
`;

exports[`Deep enforcements Should match sucessful shapshot 1`] = `
RuleResult {
"children": Object {
"age": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"country": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"user": RuleResult {
"children": Object {
"id": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"name": RuleResult {
"children": Object {
"first": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"last": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
"middle": RuleResult {
"failed": false,
"hasErrors": false,
"hasWarnings": false,
"warn": false,
},
},
"failed": false,
"hasErrors": false,
"hasWarnings": false,
},
},
"failed": false,
"hasErrors": false,
"hasWarnings": false,
},
},
"failed": false,
"hasErrors": false,
"hasWarnings": false,
}
`;
Loading

0 comments on commit 2f948fc

Please sign in to comment.