isArrayLength

isArrayLength({ min, max })

Verifies if provided Verificable represents an array with length (number of items) is within specified range. Both min and max arguments are optional - not providing any of them means value on that 'end' will not be checked.

Arguments:

  • min - optional, of type number

  • max - optional, of type number

Possible errors:

If provided Verificable is not an array:

{
    type: "isArray",
    path: [/* ... */]
}

If provided Verificable is an array with number of items outside the range:

{
    type: "isArrayLength",
    path: [/* ... */],
    min: /* provided min value */,
    max: /* provided max value */,
}

Examples:

Each of examples uses this import statement:

const { asVerificable, isValid, isArrayLength, getErrors } = require("verifica");

Non-array values (like string in this example) are not a valid array, and will fail with an isArray error:

const verificable = asVerificable("something");
isValid(verificable, isArrayLength({ min: 2, max: 4 })); // false
getErrors(verificable, isArrayLength({ min: 2, max: 4 }));
// [ { type: "isArray", path: [] }]

Array with number of items outside of requested range will fail with isArrayLength error:

const verificable = asVerificable(["item"]);
isValid(verificable, isArrayLength({ min: 2, max: 4 })); // false
getErrors(verificable, isArrayLength({ min: 2, max: 4 }));
// [ { type: "isArrayLength", path: [], min: 2, max: 4 }]

Not providing min, max or both constrains will mean no requirement on those missing 'ends' of the range:

const verificable = asVerificable(["item"]);
isValid(verificable, isArrayLength({ max: 4 })); // true

Last updated