ensure
function ensure<T>(verificable, predicate): T
This function assesses if the provided verificable
matches or not the provided predicate
.
Returns the underlying value if predicate
is matched, otherwise throws VerificaException
containing list of VerificaError
objects.
Examples:
First, let's define an example object user
that we want to verify and wrap it with asVerificable():
const { asVerificable, ensure, isObject, isString, isInteger } = require("verifica");
const user = {
name: "John Smith",
age: 27,
address: {
street: "Valdemarsgade 10",
zipCode: 1040
}
};
const verificable = asVerificable(user);
Then we can call ensure()
method providing the Verificable
object to be checked, and the Predicate
defining the validation rule. If value behind the Verificable
object doesn't match the Predicate
, ensure()
will throw VerificaException
containing and array of VerificaError
objects. If the value matches the Predicate
, ensure()
will return the value directly.
ensure(verificable, isObject); // { name: "John Smith", (...) }
ensure(verificable.age, isString); // <throws VerificaException>
ensure(verificable.age, isInteger); // 27
Last updated
Was this helpful?