1 | ;
|
2 |
|
3 | const _ = require('lodash');
|
4 |
|
5 | /**
|
6 | * @template T
|
7 | * @typedef {(i: T) => boolean} Validator
|
8 | */
|
9 |
|
10 | /**
|
11 | * Check whether the variable is an object and all it's properties are arrays of string values:
|
12 | *
|
13 | * ignoreProperties = {
|
14 | * value1: ["item11", "item12", "item13"],
|
15 | * value2: ["item21", "item22", "item23"],
|
16 | * value3: ["item31", "item32", "item33"],
|
17 | * }
|
18 | * @template T
|
19 | * @param {Validator<T>|Validator<T>[]} validator
|
20 | * @returns {(value: {[k: any]: T|T[]}) => boolean}
|
21 | */
|
22 | module.exports = (validator) => (value) => {
|
23 | if (!_.isPlainObject(value)) {
|
24 | return false;
|
25 | }
|
26 |
|
27 | // TODO: Issue #4985
|
28 | // eslint-disable-next-line no-shadow
|
29 | return Object.values(value).every((value) => {
|
30 | if (!Array.isArray(value)) {
|
31 | return false;
|
32 | }
|
33 |
|
34 | // Make sure the array items are strings
|
35 | return value.every((item) => {
|
36 | if (Array.isArray(validator)) {
|
37 | return validator.some((v) => v(item));
|
38 | }
|
39 |
|
40 | return validator(item);
|
41 | });
|
42 | });
|
43 | };
|