🔍
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. Functions

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
PreviousgetErrorsNextrawValue

Last updated 5 years ago

Was this helpful?