Skip to content

Predicate().restored() method

Adds state callbacks which will be invoked after the predicate function execution when it returns false or a Promise that fulfills with false after restoration of the previous valid value if the predicate function was added with the parameter keepValid=true of the Validation().constraint() method.

Syntax

Predicate().restored(callback1, callback2, /* ..., */ callbackN)
Predicate().restored([callback1, callback2, /* ..., */ callbackN])
Predicate().restored(callback1, [callback2, /* ..., */ callbackN])
Predicate().restored(callback1, [[callback2], /* ..., */ callbackN])
Predicate().restored([callback1, callback2], /* ..., */ callbackN)
Predicate().restored([[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 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

Until there is a valid value to restore, the validatable value is restored back to the initial value and since it is not valid, β€œinvalid” state callbacks are called after the value restoration. Once the validatable value gets valid, it is saved and any attempt to assign an invalid value leads to restoration to that previous valid value and calling β€œvalid” state callbacks. The only invalid value that is possible to assign after that is the initial value which causes resetting of the previously saved valid value.

Also see the example of using the parameter keepValid.

import { Validation, Predicate } from "isomorphic-validation";
const { log } = console;
// predicate function
const isMeaningOfLife = (value) => {
log(`isMeaningOfLife("${value}")`);
return value === '42';
};
// state callbacks
const validCB = ({ isValid }) => { log(`validCB({ isValid: ${isValid} })`); };
const invalidCB = ({ isValid }) => { log(`invalidCB({ isValid: ${isValid} })`); };
const restoredCB = ({ isValid }) => { log(`restoredCB({ isValid: ${isValid} })`); };
const validatableObject = { value: '' };
const v = Validation(validatableObject)
.constraint(
Predicate(isMeaningOfLife)
.valid(validCB) // adding state callbacks
.invalid(invalidCB) // adding state callbacks
.restored(restoredCB), // adding state callbacks
{ keepValid: true }
)
.started(() => log('---'));
validatableObject.value = 'πŸ₯‡';
await v.validate();
validatableObject.value = 'πŸ’²';
await v.validate();
validatableObject.value = '42';
await v.validate();
validatableObject.value = 'πŸ’•';
await v.validate();
validatableObject.value = '🐣';
await v.validate();
validatableObject.value = ''; // initial value
await v.validate();
// Output:
// ---
// isMeaningOfLife("πŸ₯‡")
// restoredCB({ isValid: false })
// isMeaningOfLife("")
// invalidCB({ isValid: false })
// ---
// isMeaningOfLife("πŸ’²")
// restoredCB({ isValid: false })
// isMeaningOfLife("")
// invalidCB({ isValid: false })
// ---
// isMeaningOfLife("42")
// validCB({ isValid: true })
// ---
// isMeaningOfLife("πŸ’•")
// restoredCB({ isValid: false })
// isMeaningOfLife("42")
// validCB({ isValid: true })
// ---
// isMeaningOfLife("🐣")
// restoredCB({ isValid: false })
// isMeaningOfLife("42")
// validCB({ isValid: true })
// ---
// isMeaningOfLife("")
// restoredCB({ isValid: false })
// isMeaningOfLife("")
// invalidCB({ isValid: false })