optional

function optional(predicate): Predicate

This function takes a Predicate as an argument and returns Predicate that matches on the same conditions as the original one, but allows null and undefined values as well. This predicate doesn't consider missing (i.e. not existing) values as valid.

Examples:

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

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

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

Next, let's use the optional() function to create Predicate that allows optional values:

const isOptionalString = optional(isString);

Now, let's test the new Predicate compared to the original one:

isValid(verificable, isString); // false

isValid(verificable, isOptionalString); // true

Last updated