feat(validate): added isValidatorApplied util
This commit is contained in:
parent
04adca4cb6
commit
4ee8b8f0df
2 changed files with 33 additions and 0 deletions
18
packages/validate/src/isValidatorApplied.js
Normal file
18
packages/validate/src/isValidatorApplied.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* TODO: refactor validators to classes, putting needed meta info on instance.
|
||||
* Note that direct function comparison (Validator[0] === minDate) doesn't work when code
|
||||
* is transpiled
|
||||
* @param {String} name - a name like minDate, maxDate, minMaxDate
|
||||
* @param {Function} fn - the validator function to execute provided in [fn, param, config]
|
||||
* @param {Function} requiredSignature - arguments needed to execute fn without failing
|
||||
* @returns {Boolean} - whether the validator (name) is applied
|
||||
*/
|
||||
export function isValidatorApplied(name, fn, requiredSignature) {
|
||||
let result;
|
||||
try {
|
||||
result = Object.keys(fn(new Date(), requiredSignature))[0] === name;
|
||||
} catch (e) {
|
||||
result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
15
packages/validate/test/isValidatorApplied.test.js
Normal file
15
packages/validate/test/isValidatorApplied.test.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { expect } from '@open-wc/testing';
|
||||
import { isValidatorApplied } from '../src/isValidatorApplied.js';
|
||||
|
||||
describe('isValidatorApplied', () => {
|
||||
it(`checks if validator (provided name string) is applied`, async () => {
|
||||
const myValFn = (val, param) => ({ myValFn: param === 'x' });
|
||||
const myOtherValFn = (val, param) => ({ myOtherValFn: param === 'x' });
|
||||
|
||||
expect(isValidatorApplied('myValFn', myValFn, 'x')).to.equal(true);
|
||||
expect(isValidatorApplied('myValFn', myValFn, 'y')).to.equal(true);
|
||||
|
||||
expect(isValidatorApplied('myValFn', myOtherValFn, 'x')).to.equal(false);
|
||||
expect(isValidatorApplied('myValFn', myOtherValFn, 'y')).to.equal(false);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue