all

function all(predicate1, predicate2, ...): Predicate

This function takes as a parameters arbitrary number of Predicates, and returns a Predicate that matches only if all of the Predicates provided as arguments match.

If any of the Predicates provided as arguments doesn't match the value, the error(s) returned by that first (from left hand side) failing Predicate are returned from the combined Predicate.

Examples:

Failing example:

First, let's define an example value that we want to verify and wrap it with asVerificable():

const { asVerificable, all, isNumber, isString, isArray, getErrors } = require("verifica");

const value = 123;
const verificable = asVerificable(value);

Next, let's use the all() function to combine multiple predicates into one:

const combinedPredicate = all(
    isNumber,
    isString,
    isArray,
);

Only the error(s) from the first failing Predicate (isString) are returned from the combinedPredicate. Errors from the next failing predicates (isArray) are not returned:

getErrors(verificable, combinedPredicate);
// [{ type: "isString", path: [] }]

Matching example:

const { asVerificable, all, isInteger, isNumberInRange, isValid } = require("verifica");

const value = 123;
const verificable = asVerificable(value);

We can also create more useful combined predicate like:

const combinedPredicate2 = all(
    isInteger,
    isNumberInRange({ min: 100, max: 200})
);

And test the value against this combined Predicate:

isValid(verificable, combinedPredicate2); // true

Last updated