generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add disabling of validators (#125)
- Loading branch information
1 parent
007cc3d
commit e17af95
Showing
11 changed files
with
182 additions
and
1 deletion.
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
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,16 @@ | ||
let validationEnabled = true; | ||
|
||
/** | ||
* Sets whether validators should run on the input, or if the input should be passed through. | ||
* @param enabled Whether validation should be done on inputs | ||
*/ | ||
export function setGlobalValidationEnabled(enabled: boolean) { | ||
validationEnabled = enabled; | ||
} | ||
|
||
/** | ||
* @returns Whether validation is enabled | ||
*/ | ||
export function getGlobalValidationEnabled() { | ||
return validationEnabled; | ||
} |
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
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,87 @@ | ||
import { s, setGlobalValidationEnabled } from '../../src'; | ||
|
||
describe('Validation enabled and disabled configurations', () => { | ||
const stringPredicate = s.string.lengthGreaterThan(5); | ||
const arrayPredicate = s.array(s.string).lengthGreaterThan(2); | ||
const mapPredicate = s.map(s.string, s.number); | ||
const objectPredicate = s.object({ | ||
owo: s.boolean | ||
}); | ||
const recordPredicate = s.record(s.number); | ||
const setPredicate = s.set(s.number); | ||
const tuplePredicate = s.tuple([s.string, s.number]); | ||
|
||
describe('Global configurations', () => { | ||
beforeAll(() => { | ||
setGlobalValidationEnabled(false); | ||
}); | ||
|
||
afterAll(() => { | ||
setGlobalValidationEnabled(true); | ||
}); | ||
|
||
test.each([ | ||
// | ||
['string', stringPredicate, ''], | ||
['array', arrayPredicate, []], | ||
['map', mapPredicate, new Map([[0, '']])], | ||
['object', objectPredicate, { owo: 'string' }], | ||
['record', recordPredicate, { one: 'one' }], | ||
['set', setPredicate, new Set(['1'])], | ||
['tuple', tuplePredicate, [0, 'zero']] | ||
])('GIVEN globally disabled %s predicate THEN returns the input', (_, inputPredicate, input) => { | ||
expect(inputPredicate.parse(input)).toStrictEqual(input); | ||
}); | ||
}); | ||
|
||
describe('Validator level configurations', () => { | ||
test.each([ | ||
// | ||
['string', stringPredicate, ''], | ||
['array', arrayPredicate, []], | ||
['map', mapPredicate, new Map([[0, '']])], | ||
['object', objectPredicate, { owo: 'string' }], | ||
['record', recordPredicate, { one: 'one' }], | ||
['set', setPredicate, new Set(['1'])], | ||
['tuple', tuplePredicate, [0, 'zero']] | ||
])('GIVEN disabled %s predicate THEN returns the input', (_, inputPredicate, input) => { | ||
const predicate = inputPredicate.setValidationEnabled(false); | ||
|
||
expect(predicate.parse(input)).toStrictEqual(input); | ||
}); | ||
|
||
test("GIVEN disabled predicate THEN checking if it's disabled should return true", () => { | ||
const predicate = s.string.setValidationEnabled(false); | ||
|
||
expect(predicate.getValidationEnabled()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Globally disabled but locally enabled', () => { | ||
beforeAll(() => { | ||
setGlobalValidationEnabled(false); | ||
}); | ||
|
||
afterAll(() => { | ||
setGlobalValidationEnabled(true); | ||
}); | ||
|
||
test.each([ | ||
// | ||
['string', stringPredicate, ''], | ||
['array', arrayPredicate, []], | ||
['map', mapPredicate, new Map([[0, '']])], | ||
['object', objectPredicate, { owo: 'string' }], | ||
['record', recordPredicate, { one: 'one' }], | ||
['set', setPredicate, new Set(['1'])], | ||
['tuple', tuplePredicate, [0, 'zero']] | ||
])( | ||
'GIVEN enabled %s predicate while the global option is set to false THEN it should throw validation errors', | ||
(_, inputPredicate, input) => { | ||
const predicate = inputPredicate.setValidationEnabled(true); | ||
|
||
expect(() => predicate.parse(input)).toThrowError(); | ||
} | ||
); | ||
}); | ||
}); |