-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(n4s): add shape and loose validations
- Loading branch information
Showing
15 changed files
with
266 additions
and
22 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import enforce from 'enforce'; | ||
|
||
describe('allOf', () => { | ||
describe('Lazy Assertions', () => { | ||
describe('When all rules are satisfied', () => { | ||
it('Should return a passing result', () => { | ||
expect( | ||
enforce.allOf(enforce.isArray(), enforce.longerThan(2)).run([1, 2, 3]) | ||
).toEqual({ pass: true }); | ||
}); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,8 @@ | ||
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn'; | ||
|
||
export default function runLazyRule( | ||
lazyRule: TLazyRuleMethods, | ||
currentValue: any | ||
): TRuleDetailedResult { | ||
return lazyRule.run(currentValue); | ||
} |
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
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,177 @@ | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('enforce.shape excact matching', () => { | ||
describe('lazy interface', () => { | ||
it('Should return a failing return when value has non-enforced keys', () => { | ||
expect( | ||
enforce | ||
.shape({ username: enforce.isString(), age: enforce.isNumber() }) | ||
.run({ username: 'ealush', age: 31, foo: 'bar' }) | ||
).toEqual({ pass: false }); | ||
}); | ||
}); | ||
describe('eager interface', () => { | ||
it('Should throw an error when value has non-enforced keys', () => { | ||
expect(() => { | ||
enforce({ username: 'ealush', age: 31, foo: 'bar' }).shape({ | ||
username: enforce.isString(), | ||
age: enforce.isNumber(), | ||
}); | ||
}).toThrow(); | ||
}); | ||
}); | ||
}); | ||
|
||
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,21 @@ | ||
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn'; | ||
import * as ruleReturn from 'ruleReturn'; | ||
import runLazyRule from 'runLazyRule'; | ||
|
||
export default function loose( | ||
inputObject: Record<string, any>, | ||
shapeObject: Record<string, TLazyRuleMethods> | ||
): TRuleDetailedResult { | ||
for (const key in shapeObject) { | ||
const currentValue = inputObject[key]; | ||
const currentRule = shapeObject[key]; | ||
|
||
const res = runLazyRule(currentRule, currentValue); | ||
|
||
if (!res.pass) { | ||
return res; | ||
} | ||
} | ||
|
||
return ruleReturn.passing(); | ||
} |
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,8 @@ | ||
import loose from 'loose'; | ||
import shape from 'shape'; | ||
|
||
export default function schema() { | ||
return { shape, loose }; | ||
} | ||
|
||
export type TSchema = ReturnType<typeof schema>; |
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,22 @@ | ||
import hasOwnProperty from 'hasOwnProperty'; | ||
|
||
import loose from 'loose'; | ||
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn'; | ||
import * as ruleReturn from 'ruleReturn'; | ||
|
||
export default function shape( | ||
inputObject: Record<string, any>, | ||
shapeObject: Record<string, TLazyRuleMethods> | ||
): TRuleDetailedResult { | ||
const baseRes = loose(inputObject, shapeObject); | ||
if (!baseRes.pass) { | ||
return baseRes; | ||
} | ||
for (const key in inputObject) { | ||
if (!hasOwnProperty(shapeObject, key)) { | ||
return ruleReturn.failing(); | ||
} | ||
} | ||
|
||
return ruleReturn.passing(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.