feat(validate): added isValidatorApplied util

This commit is contained in:
Thijs Louisse 2019-05-15 16:51:22 +02:00 committed by Thomas Allmer
parent 04adca4cb6
commit 4ee8b8f0df
2 changed files with 33 additions and 0 deletions

View 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;
}

View 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);
});
});