Skip to content

Commit 4ee8b8f

Browse files
tlouissedaKmoR
authored andcommitted
feat(validate): added isValidatorApplied util
1 parent 04adca4 commit 4ee8b8f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* TODO: refactor validators to classes, putting needed meta info on instance.
3+
* Note that direct function comparison (Validator[0] === minDate) doesn't work when code
4+
* is transpiled
5+
* @param {String} name - a name like minDate, maxDate, minMaxDate
6+
* @param {Function} fn - the validator function to execute provided in [fn, param, config]
7+
* @param {Function} requiredSignature - arguments needed to execute fn without failing
8+
* @returns {Boolean} - whether the validator (name) is applied
9+
*/
10+
export function isValidatorApplied(name, fn, requiredSignature) {
11+
let result;
12+
try {
13+
result = Object.keys(fn(new Date(), requiredSignature))[0] === name;
14+
} catch (e) {
15+
result = false;
16+
}
17+
return result;
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from '@open-wc/testing';
2+
import { isValidatorApplied } from '../src/isValidatorApplied.js';
3+
4+
describe('isValidatorApplied', () => {
5+
it(`checks if validator (provided name string) is applied`, async () => {
6+
const myValFn = (val, param) => ({ myValFn: param === 'x' });
7+
const myOtherValFn = (val, param) => ({ myOtherValFn: param === 'x' });
8+
9+
expect(isValidatorApplied('myValFn', myValFn, 'x')).to.equal(true);
10+
expect(isValidatorApplied('myValFn', myValFn, 'y')).to.equal(true);
11+
12+
expect(isValidatorApplied('myValFn', myOtherValFn, 'x')).to.equal(false);
13+
expect(isValidatorApplied('myValFn', myOtherValFn, 'y')).to.equal(false);
14+
});
15+
});

0 commit comments

Comments
 (0)