-
-
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.
- Loading branch information
Showing
8 changed files
with
300 additions
and
156 deletions.
There are no files selected for viewing
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,8 +1,13 @@ | ||
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn'; | ||
import * as ruleReturn from 'ruleReturn'; | ||
|
||
export default function runLazyRule( | ||
lazyRule: TLazyRuleMethods, | ||
currentValue: any | ||
): TRuleDetailedResult { | ||
return lazyRule.run(currentValue); | ||
try { | ||
return lazyRule.run(currentValue); | ||
} catch { | ||
return ruleReturn.failing(); | ||
} | ||
} |
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,115 @@ | ||
import enforce from 'enforce'; | ||
|
||
describe('enforce.isArrayOf', () => { | ||
describe('lazy interface', () => { | ||
it('Should return a passing return for an empty array', () => { | ||
expect(enforce.isArrayOf(enforce.isString()).run([])).toEqual({ | ||
pass: true, | ||
}); | ||
}); | ||
|
||
it('Should return a passing return for valid arrays', () => { | ||
expect( | ||
enforce.isArrayOf(enforce.isString()).run(['a', 'b', 'c']) | ||
).toEqual({ pass: true }); | ||
|
||
expect( | ||
enforce | ||
.isArrayOf(enforce.anyOf(enforce.isString(), enforce.isNumber())) | ||
.run([1, 'b', 'c']) | ||
).toEqual({ pass: true }); | ||
|
||
expect( | ||
enforce | ||
.isArrayOf( | ||
enforce.shape({ | ||
id: enforce.isNumber(), | ||
username: enforce.isString(), | ||
}) | ||
) | ||
.run([ | ||
{ id: 1, username: 'b' }, | ||
{ id: 2, username: 'c' }, | ||
]) | ||
).toEqual({ pass: true }); | ||
}); | ||
|
||
it('Should return a failing return for invalid arrays', () => { | ||
expect(enforce.isArrayOf(enforce.isString()).run([1, 2, 3])).toEqual({ | ||
pass: false, | ||
}); | ||
|
||
expect( | ||
enforce | ||
.isArrayOf(enforce.allOf(enforce.isString(), enforce.isNumber())) | ||
.run([1, 2, 3]) | ||
).toEqual({ | ||
pass: false, | ||
}); | ||
|
||
expect( | ||
enforce | ||
.isArrayOf( | ||
enforce.shape({ | ||
id: enforce.isNumber(), | ||
username: enforce.isString(), | ||
}) | ||
) | ||
.run([ | ||
{ id: '1', username: 'b' }, | ||
{ id: '2', username: 'c' }, | ||
{ id: '3', username: 'd' }, | ||
]) | ||
).toEqual({ | ||
pass: false, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('eager interface', () => { | ||
it('Should return silently for an empty array', () => { | ||
enforce([]).isArrayOf(enforce.isString()); | ||
}); | ||
|
||
it('Should return silently for valid arrays', () => { | ||
enforce(['a', 'b', 'c']).isArrayOf(enforce.isString()); | ||
|
||
enforce([1, 'b', 'c']).isArrayOf( | ||
enforce.anyOf(enforce.isString(), enforce.isNumber()) | ||
); | ||
|
||
enforce([ | ||
{ id: 1, username: 'b' }, | ||
{ id: 2, username: 'c' }, | ||
]).isArrayOf( | ||
enforce.shape({ | ||
id: enforce.isNumber(), | ||
username: enforce.isString(), | ||
}) | ||
); | ||
}); | ||
|
||
it('Should throw for invalid arrays', () => { | ||
expect(() => enforce([1, 2, 3]).isArrayOf(enforce.isString())).toThrow(); | ||
|
||
expect(() => | ||
enforce([1, 2, 3]).isArrayOf( | ||
enforce.allOf(enforce.isString(), enforce.isNumber()) | ||
) | ||
).toThrow(); | ||
|
||
expect(() => | ||
enforce([ | ||
{ id: '1', username: 'b' }, | ||
{ id: '2', username: 'c' }, | ||
{ id: '3', username: 'd' }, | ||
]).isArrayOf( | ||
enforce.shape({ | ||
id: enforce.isNumber(), | ||
username: enforce.isString(), | ||
}) | ||
) | ||
).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import enforce from 'enforce'; | ||
|
||
describe('enforce.loose for loose matching', () => { | ||
describe('lazy interface', () => { | ||
it('Should return a passing return when value has non-enforced keys', () => { | ||
expect( | ||
enforce | ||
.loose({ username: enforce.isString(), age: enforce.isNumber() }) | ||
.run({ username: 'ealush', age: 31, foo: 'bar' }) | ||
).toEqual({ pass: true }); | ||
}); | ||
}); | ||
describe('eager interface', () => { | ||
it('Should return sliently return when value has non-enforced keys', () => { | ||
enforce({ username: 'ealush', age: 31, foo: 'bar' }).loose({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber(), | ||
}); | ||
}); | ||
}); | ||
}); |
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,135 @@ | ||
import enforce from 'enforce'; | ||
|
||
// The base behavior of 'loose' and 'shape' is practically the same | ||
// so we cover them using the same tests. | ||
describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => { | ||
describe('lazy interface', () => { | ||
it('Should return a passing return when tests are valid', () => { | ||
expect( | ||
enforce[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
}).run({ username: 'ealush', age: 31 }) | ||
).toEqual({ pass: true }); | ||
}); | ||
|
||
it('Should return a failing return when tests are invalid', () => { | ||
expect( | ||
enforce[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
}).run({ username: null, age: 0 }) | ||
).toEqual({ pass: false }); | ||
}); | ||
|
||
describe('nested shapes', () => { | ||
it('Should return a passing return when tests are valid', () => { | ||
expect( | ||
enforce[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
address: enforce.shape({ | ||
street: enforce.isString(), | ||
city: enforce.isString(), | ||
state: enforce.isString(), | ||
zip: enforce.isNumber(), | ||
}), | ||
}).run({ | ||
username: 'ealush', | ||
age: 31, | ||
address: { | ||
street: '123 Main St', | ||
city: 'New York', | ||
state: 'NY', | ||
zip: 12345, | ||
}, | ||
}) | ||
).toEqual({ pass: true }); | ||
}); | ||
it('Should return a failing return when tests are invalid', () => { | ||
expect( | ||
enforce[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
address: enforce.shape({ | ||
street: enforce.isString(), | ||
city: enforce.isString(), | ||
state: enforce.isString(), | ||
zip: enforce.isNumber(), | ||
}), | ||
}).run({ | ||
username: 'ealush', | ||
age: 31, | ||
address: { | ||
street: '123 Main St', | ||
city: null, | ||
}, | ||
}) | ||
).toEqual({ pass: false }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('eager interface', () => { | ||
it('Should throw an error fora failing return', () => { | ||
expect(() => { | ||
enforce({ username: null, age: 0 })[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
}); | ||
}).toThrow(); | ||
}); | ||
|
||
it('Should return silently for a passing return', () => { | ||
enforce({ username: 'ealush', age: 31 })[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
}); | ||
}); | ||
|
||
describe('nested shapes', () => { | ||
it('Should return silently when tests are valid', () => { | ||
enforce({ | ||
username: 'ealush', | ||
age: 31, | ||
address: { | ||
street: '123 Main St', | ||
city: 'New York', | ||
state: 'NY', | ||
zip: 12345, | ||
}, | ||
})[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
address: enforce.shape({ | ||
street: enforce.isString(), | ||
city: enforce.isString(), | ||
state: enforce.isString(), | ||
zip: enforce.isNumber(), | ||
}), | ||
}); | ||
}); | ||
it('Should throw when tests are invalid', () => { | ||
expect(() => { | ||
enforce({ | ||
username: 'ealush', | ||
age: 31, | ||
address: { | ||
street: '123 Main St', | ||
city: null, | ||
}, | ||
})[methodName]({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber().gt(18), | ||
address: enforce.shape({ | ||
street: enforce.isString(), | ||
city: enforce.isString(), | ||
state: enforce.isString(), | ||
zip: enforce.isNumber(), | ||
}), | ||
}); | ||
}).toThrow(); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.