Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame^] | 1 | 'use strict'; |
| 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 | return Object.values(value).every((value) => { |
| 28 | if (!Array.isArray(value)) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | // Make sure the array items are strings |
| 33 | return value.every((item) => { |
| 34 | if (Array.isArray(validator)) { |
| 35 | return validator.some((v) => v(item)); |
| 36 | } |
| 37 | |
| 38 | return validator(item); |
| 39 | }); |
| 40 | }); |
| 41 | }; |