Predicate().changed() method
Adds state callbacks which will be invoked after the predicate function execution if its current return value differs from the previous one.
Syntax
Predicate().changed(callback1, callback2, /* ..., */ callbackN)Predicate().changed([callback1, callback2, /* ..., */ callbackN])Predicate().changed(callback1, [callback2, /* ..., */ callbackN])Predicate().changed(callback1, [[callback2], /* ..., */ callbackN])Predicate().changed([callback1, callback2], /* ..., */ callbackN)Predicate().changed([[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;const logFn = (fn) => (value) => (log(`${fn.name}("${value}")`), fn(value));
// predicate functionsconst hasLetterA = (value) => /[Aa]+/.test(value);const hasLetterB = (value) => /[Bb]+/.test(value);const hasLetterC = (value) => /[Cc]+/.test(value);
// state callbacksconst startedCB = ({ isValid }) => { log(`startedCB({ isValid: ${isValid} }) <-- before`);};
const validCB = ({ isValid }) => { log(`validCB({ isValid: ${isValid} }) <-- after`);};
const invalidCB = ({ isValid }) => { log(`invalidCB({ isValid: ${isValid} }) <-- after`);};
const changedCB = ({ isValid }) => { log(`changedCB({ isValid: ${isValid} }) <-- changes`);};
const validatableObject = { value: '' };
const v = Validation(validatableObject) .constraint( logFn(hasLetterA) ) .constraint( logFn(hasLetterB) ) .constraint( Predicate( logFn(hasLetterC) ) .started(startedCB) // adding state callbacks .valid(validCB) // adding state callbacks .invalid(invalidCB) // adding state callbacks .changed(changedCB) // adding state callbacks ) .started(() => log('---'));
validatableObject.value = 'A';await v.validate(); // hasLetterC() -> false
validatableObject.value = 'Abc';await v.validate(); // hasLetterC() -> true, changedCB will be called
validatableObject.value = 'ABCabc';await v.validate(); // hasLetterC() -> true
validatableObject.value = 'ABab';await v.validate(); // hasLetterC() -> false, changedCB will be called
// Output:// ---// hasLetterA("A")// hasLetterB("A")// startedCB({ isValid: false }) <-- before// hasLetterC("A")// invalidCB({ isValid: false }) <-- after// ---// hasLetterA("Abc")// hasLetterB("Abc")// startedCB({ isValid: false }) <-- before// hasLetterC("Abc")// changedCB({ isValid: true }) <-- changes// validCB({ isValid: true }) <-- after// ---// hasLetterA("ABCabc")// hasLetterB("ABCabc")// startedCB({ isValid: true }) <-- before// hasLetterC("ABCabc")// validCB({ isValid: true }) <-- after// ---// hasLetterA("ABab")// hasLetterB("ABab")// startedCB({ isValid: true }) <-- before// hasLetterC("ABab")// changedCB({ isValid: false }) <-- changes// invalidCB({ isValid: false }) <-- after