asVerificable

function asVerificable(value, path = []): Verificable

This function wraps provided value into a Verificable object.

Verificable object can be later used with functions like isValid(...) , getErrors(...) , ensure(...) to validate the value against provided predicate .

Verificable object is infinitely-deeply nested, with infinite number of properties of all possible names. Each of it's properties is a Verificable object as well. Each of it's properties corresponds a possible property on passed in value object. That means you can verify any arbitrary, no matter how deep nested, even not existing ones!

The optional path parameter allows to define a base path, that will be prepended to all paths in ValidationError objects coming from this Validatable .

Examples:

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

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

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

const verificable = asVerificable(user);

Thanks to Verificable special properties, properties with any name can be accessed on the Verificable object, and the values of them are Verificable objects as well. This allows us to verify any level of deep-nested properties:

isValid(verificable, isString); // false
isValid(verificable.name, isString); // true
isValid(verificable.address.street, isString); // true
isValid(verificable.address.zipCode, isString); // false

Even properties that doesn't exist, nested multiple levels down, can be verified - and will return ValidationError of type missing with path specifying the path that was tried to be accessed:

isValid(verificable.some.non.existing.nested.object, isString); // false

getErrors(verificable.some.non.existing.nested.object, isString);
// [{ type: "missing", path: ["some", "non", "existing", "nested", "object"] }]

Last updated