Skip to content

Predicate().valid() method

Adds state callbacks which will be invoked after the predicate function execution every time when it returns true or a Promise that fulfills with true.

Syntax

Predicate().valid(callback1, callback2, /* ..., */ callbackN)

Parameters

  • callback1, ..., callbackN
    Functions to add. The callback functions will be called with the following arguments:

Return value

The Predicate object.

Exceptions

If anything other than a function is passed in the corresponding error will be thrown.

Description

Adds state callbacks to the Predicate object. Once the Predicate in the specified state, the corresponding callbacks will be invoked one by one in the order they were added. The main purpose of state callbacks is to perform side effects related to the state. A ValidationResult object is passed into state callbacks.

Examples

import { Validation, Predicate } from "isomorphic-validation";
const { log } = console;
// predicate functions
const predicate1 = (value) => { log(`predicate1("${value}")`); return true; };
const predicate2 = (value) => { log(`predicate2("${value}")`); return true; };
const predicate3 = (value) => { log(`predicate3("${value}")`); return true; };
// state callbacks
const callback1 = ({ isValid }) => { log(`callback1({ isValid: ${isValid} })`); };
const callback2 = ({ isValid }) => { log(`callback2({ isValid: ${isValid} })`); };
const callback3 = ({ isValid }) => { log(`callback3({ isValid: ${isValid} })`); };
const callback4 = ({ isValid }) => { log(`callback4({ isValid: ${isValid} })`); };
const validatableObject = { value: 'obj1' };
Validation(validatableObject)
.constraint(predicate1)
.constraint(
Predicate(predicate2)
.valid(callback1) // adding state callbacks
.valid(callback2, callback3) // adding state callbacks
)
.constraint(
Predicate(predicate3)
.valid(callback4) // adding state callbacks
)
.validate();
// Output:
// predicate1("obj1")
// predicate2("obj1")
// callback1({ isValid: true })
// callback2({ isValid: true })
// callback3({ isValid: true })
// predicate3("obj1")
// callback4({ isValid: true })