blob: 3150dc3ada89b00e41abed3c5ac9b6bc066e09fd [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
3const _ = 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 */
22module.exports = (validator) => (value) => {
23 if (!_.isPlainObject(value)) {
24 return false;
25 }
26
Tim van der Lippe38208902021-05-11 16:37:59 +010027 return Object.values(value).every((array) => {
28 if (!Array.isArray(array)) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020029 return false;
30 }
31
32 // Make sure the array items are strings
Tim van der Lippe38208902021-05-11 16:37:59 +010033 return array.every((item) => {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020034 if (Array.isArray(validator)) {
35 return validator.some((v) => v(item));
36 }
37
38 return validator(item);
39 });
40 });
41};