abolish
Version:
A javascript object validator.
30 lines (29 loc) • 1.06 kB
JavaScript
;
const types_checker_1 = require("../../src/types-checker");
module.exports = {
name: "inArray",
description: "Check that a value is in an array",
error: ":param does not exists in array [:option]",
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
}
}
// 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 true;
}
return false;
}
};