-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: [N4S] allOf compound rule (#533)
- Loading branch information
Showing
10 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
65 changes: 65 additions & 0 deletions
65
packages/n4s/src/enforce/compounds/__tests__/allOf.test.js
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,65 @@ | ||
import allOf from 'allOf'; | ||
import enforce from 'enforce'; | ||
|
||
describe('allOf validation', () => { | ||
describe('Base behavior', () => { | ||
it('Should fail when at least one rule fails', () => { | ||
expect(allOf('test', enforce.isString(), enforce.longerThan(10))).toBe( | ||
false | ||
); | ||
}); | ||
it('Should succeed when all of the rules apply', () => { | ||
expect(allOf('test', enforce.isString(), enforce.longerThan(3))).toBe( | ||
true | ||
); | ||
}); | ||
it('Should pass when no rules are provided', () => { | ||
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({ | ||
id: 123, | ||
name: { first: 'Albert', last: 'Einstein' }, | ||
disabled: true, | ||
}).allOf(User, DisabledAccount); | ||
}); | ||
|
||
it('Should throw if one of the rules fail', () => { | ||
expect(() => { | ||
enforce({ | ||
id: 123, | ||
name: { first: 'Albert', last: 0 }, | ||
disabled: true, | ||
}).allOf(User, DisabledAccount); | ||
}).toThrow(); | ||
|
||
expect(() => { | ||
enforce({ | ||
id: 123, | ||
name: { first: 'Albert', last: 'Einstein' }, | ||
disabled: false, | ||
}).allOf(User, DisabledAccount); | ||
}).toThrow(); | ||
}); | ||
}); | ||
}); |
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
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,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(allOf); |
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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import allOf from 'allOf'; | ||
import anyOf from 'anyOf'; | ||
import isArrayOf from 'isArrayOf'; | ||
import oneOf from 'oneOf'; | ||
import optional from 'optional'; | ||
import { shape, loose } from 'shape'; | ||
|
||
export default { | ||
allOf, | ||
anyOf, | ||
isArrayOf, | ||
loose, | ||
oneOf, | ||
optional, | ||
shape, | ||
oneOf | ||
shape | ||
}; |
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
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
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