Predicate().invalid() method
Adds state callbacks which will be invoked after the predicate function execution every time when it returns false
or a Promise
that fulfills with false
.
Syntax
Predicate().invalid(callback1, callback2, /* ..., */ callbackN)Predicate().invalid([callback1, callback2, /* ..., */ callbackN])Predicate().invalid(callback1, [callback2, /* ..., */ callbackN])Predicate().invalid(callback1, [[callback2], /* ..., */ callbackN])Predicate().invalid([callback1, callback2], /* ..., */ callbackN)Predicate().invalid([[callback1], callback2], /* ..., */ callbackN)
Parameters
-
callback1, ..., callbackN
Functions to add. The callback functions will be called with the following arguments:validationResult
AValidationResult
object.
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 false; };const predicate3 = (value) => { log(`predicate3("${value}")`); return false; };
// state callbacksconst 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) .invalid(callback1) // adding state callbacks .invalid(callback2, callback3) // adding state callbacks ) .constraint( Predicate(predicate3) .invalid(callback4) // adding state callbacks ) .validate();
// Output:// predicate1("obj1")// predicate2("obj1")// callback1({ isValid: false })// callback2({ isValid: false })// callback3({ isValid: false })// predicate3("obj1")// callback4({ isValid: false })