Predicate().validated() method
Adds state callbacks which will be invoked after the predicate function execution.
Syntax
Predicate().validated(callback1, callback2, /* ..., */ callbackN)Predicate().validated([callback1, callback2, /* ..., */ callbackN])Predicate().validated(callback1, [callback2, /* ..., */ callbackN])Predicate().validated(callback1, [[callback2], /* ..., */ callbackN])Predicate().validated([callback1, callback2], /* ..., */ callbackN)Predicate().validated([[callback1], callback2], /* ..., */ callbackN)Parameters
-
callback1, ..., callbackN
Functions to add. The callback functions will be called with the following arguments:validationResult
AValidationResultobject.
Return value
The Predicate object.
Exceptions
If anything other than a function was passed in or an Array that contains not a function the corresponding error will be thrown.
Description
Accepts functions or arrays of functions of any nesting level or both in any combination.
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 functionsconst predicate1 = (value) => { log(`predicate1("${value}")`); return true; };const predicate2 = (value) => { log(`predicate2("${value}")`); return true; };const predicate3 = (value) => { log(`predicate3("${value}")`); return true; };
// state callbacksconst callback1 = (value) => { log(`callback1(${value})`); };const callback2 = (value) => { log(`callback2(${value})`); };const callback3 = (value) => { log(`callback3(${value})`); };const callback4 = (value) => { log(`callback4(${value})`); };
const validatableObject = { value: 'obj1' };
Validation(validatableObject) .constraint(predicate1) .constraint( Predicate(predicate2) .validated(callback1) // adding state callbacks .validated(callback2, callback3) // adding state callbacks ) .constraint( Predicate(predicate3) .validated(callback4) // adding state callbacks ) .validate();
// Output:// predicate1("obj1")// predicate2("obj1")// callback1([object ValidationResult])// callback2([object ValidationResult])// callback3([object ValidationResult])// predicate3("obj1")// callback4([object ValidationResult])