Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
115 additions
and
0 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
66 changes: 66 additions & 0 deletions
66
packages/n4s/src/enforce/compounds/__tests__/oneOf.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,66 @@ | ||
import enforce from 'enforce'; | ||
import oneOf from 'oneOf'; | ||
|
||
describe('OneOf validation', () => { | ||
describe('Base behavior', () => { | ||
it('Should fail when all rules fail', () => { | ||
expect( | ||
oneOf( | ||
'test', | ||
enforce.isNumber(), | ||
enforce.isUndefined() | ||
) | ||
).toBe(false); | ||
}); | ||
it('Should succeed when EXACTLY one rule applies', () => { | ||
expect( | ||
oneOf( | ||
5, | ||
enforce.isString(), | ||
enforce.isNumber(), | ||
enforce.isUndefined() | ||
) | ||
).toBe(true); | ||
}); | ||
it('Should fail when more than one rule applies', () => { | ||
expect( | ||
oneOf( | ||
5, | ||
enforce.isNumber(), | ||
enforce.isNumber().greaterThan(3), | ||
) | ||
).toBe(false); | ||
}); | ||
it('Should succeed when rule chaining', () => { | ||
expect( | ||
oneOf( | ||
[1, 2, 3], | ||
enforce.isArray().isNotEmpty().longerThan(2), | ||
enforce.isUndefined() | ||
) | ||
).toBe(true); | ||
}); | ||
it('Should fail with no rules', () => { | ||
expect( | ||
oneOf(5) | ||
).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('As part of enforce', () => { | ||
it('Should validate the rules correctly', () => { | ||
enforce(77).oneOf( | ||
enforce.isString(), | ||
enforce.isNumber(), | ||
enforce.isUndefined() | ||
) | ||
|
||
expect(() => | ||
enforce({ test: 4 }).oneOf( | ||
enforce.isNumber(), | ||
enforce.isUndefined() | ||
) | ||
).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,23 @@ | ||
import runLazyRules from 'runLazyRules'; | ||
import { withFirst } from 'withArgs'; | ||
|
||
/** | ||
* @param {*} value Value to be test against rules | ||
* @param {Function[]} rules Rules to validate the value with | ||
*/ | ||
function oneOf(value, rules) { | ||
let count = 0; | ||
|
||
for (let i = 0; i < rules.length; i++) { | ||
if (runLazyRules(rules[i], value)) { | ||
count++; | ||
} | ||
|
||
if (count > 1) { | ||
return false; | ||
} | ||
} | ||
|
||
return count === 1; | ||
} | ||
export default withFirst(oneOf) |
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