🔍
verifica
  • Verifica
  • Getting started
    • 📦Installation
    • 🧑‍🎓 Examples
      • 🧑‍🎓 Basic
      • 🧑‍🎓 Multiple conditions
      • 🧑‍🎓 Optional
      • 🧑‍🎓 Custom rules
      • 🧑‍🎓 With express.js
    • 🟦Typescript
  • Functions
    • asVerificable
    • isValid
    • getErrors
    • ensure
    • rawValue
  • Predicates
    • Built-in Predicates
      • isArray
      • isArrayLength
      • isArrayOf
      • isBoolean
      • isFiniteNumber
      • isInteger
      • isNumber
      • isNumberInRange
      • isObject
      • isOneOf
      • isRegexMatch
      • isString
      • isStringLength
    • Operations on Predicates
      • all
      • optional
    • Custom Predicates
Powered by GitBook
On this page

Was this helpful?

  1. Predicates
  2. Operations on Predicates

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
PreviousallNextCustom Predicates

Last updated 5 years ago

Was this helpful?