UNPKG

abolish

Version:

A javascript object validator.

33 lines (32 loc) 1.18 kB
"use strict"; const types_checker_1 = require("../../src/types-checker"); module.exports = { name: "notInArray", error: ":param is not allowed", description: "Check that a value is not in an array", validator: (value, option) => { (0, types_checker_1.assertType)(option, ["array", "function"]); /** * if option is a function * we run the function and check if the result is in the array */ if (typeof option === "function") { const result = option(value); if (typeof result === "boolean") return result; else if (Array.isArray(result)) { option = result; // We replace the option with the result } else { throw new Error(`The result of [notInArray] function must be a boolean or an array`); } } // Now we are sure that option is an array // we can loop through the array and check if the value is in the array for (let i = 0; i < option.length; i++) { if (option[i] === value) return false; } return true; } };