getErrors

function getErrors(verificable, predicate): VerificaError[]

This function assesses if the provided verificable matches or not the provided predicate .

Returns empty array if predicate is matched, otherwise returns array of VerificaError objects.

Examples:

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

const { asVerificable, getErrors, isObject, isString, isNumberInRange, isInteger } = require("verifica");

const user = {
    name: "John Smith",
    age: 27,
    address: {
        street: "Valdemarsgade 10",
        zipCode: 1040
    }
};
const verificable = asVerificable(user);

If verificable matches specified Predicate, function getErrors() returns empty array indicating there were no errors.

getErrors(verificable, isObject);
// []

We can use Verificable object properties that allow us to access any arbitrary path on the object, no matter how deeply nested, and the value of it will be Validatable object as well, representing possible value at the same path on the original object.

This allows us to easily verify nested properties:

getErrors(verificable.address.zipCode, isInteger);
// []

If value represented by provided Verificable object doesn't match the Predicate , function getErrors() will return an array of ValidationError objects describing the underlying error:

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

getErrors(verificable.age, isNumberInRange({ min: 1, max: 20 }));
// [{ type: "isNumberInRange", min: 1, max: 20, path: ["age"] }]

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

The guaranteed fields on a VerificableError object are type being a string defining the error type, and a path property describing what possible nested path value failed the validation.

Last updated