Predicate() constructor
The Predicate() constructor creates Predicate
objects.
Syntax
Predicate(predicateFunction)Predicate(Predicate(predicateFunction))Predicate(predicateFunction, options)Predicate(Predicate(predicateFunction), options)Predicate(Predicate(predicateFunction, options), options)
Parameters
-
predicateFunction
A function which returns aBoolean
value or aPromise
that fulfills with aBoolean
value. -
options
optional
An object that specifies predicate options. The available properties are:...anyData
optional
Any type of data under any names can be passed. It will be accessible under the specified names through theValidation().constraints
property and inValidationResult
. This can be used, for instance, to associate validation error messages withpredicateFunction
.
Default:undefined
.
Return value
A new Predicate
instance.
Exceptions
If neither a function nor another Predicate
is passed as the only parameter throws the corresponding error.
Description
Creates a new Predicate
instance. It is just a container for a predicate function which will be unpacked in the Validation().constraint()
method. The main purpose of it is attaching state callbacks and any additional data (e.g. validation error messages) to a predicate function before passing it into the Validation().constraint()
method. If another Predicate
is passed as the first parameter, a new (cloned) instance will be returned with the predicate function and state callbacks of the passed Predicate
. If also an optional object with additional data is passed, it overrides the one from the passed Predicate
if contains the same properties and, in turn, can be overriden by the optional object with the same property names passed in the Validation().constraint()
method.
Examples
Passing a predicate function with state callbacks
import { Validation, Predicate } from "isomorphic-validation";
// predicate functionconst isMeaningOfLife = (value) => value === 42;
// state callbacksconst logValid = () => console.log('Right.');const logInvalid = () => console.log('Sorry, return in 7.5 milliones years.');
const validatableObject = { value: 42,};
Validation(validatableObject) .constraint( Predicate(isMeaningOfLife) .valid(logValid) // adding state callbacks .invalid(logInvalid) // adding state callbacks ) .validate(); // -> "Right."
Cloning a Predicate
In this example there are two Predicates
which have the same predicate function and valid and invalid state callbacks. But the second one additinaly have started and validated state callbacks added. In other words the second one has everything what the first one has plus two more state callbacks. So we clone the first one and add two extra state callbacks. It spares us from writing a pair of lines of code, but there could be much more.
import { Validation, Predicate } from "isomorphic-validation";
// predicate functionconst isMeaningOfLife = (value) => value === 42;
// state callbacksconst logValid = () => console.log('Right.');const logInvalid = () => console.log('Sorry, return in 7.5 milliones years.');const logStarted = () => console.log('Start processing...')const logValidated = () => console.log('Processing ended.');
const obj1 = { value: 42 };const obj2 = { value: null };
const p1 = Predicate(isMeaningOfLife) .valid(logValid) .invalid(logInvalid);
// We could repeat the code of p1 and add two more callbacks// const p2 = Predicate(isMeaningOfLife)// .valid(logValid)// .invalid(logInvalid)// .started(logStarted)// .validated(logValidated);
// But insteadconst p2 = Predicate(p1) // we clone p1 with its predicate function and state callbacks, .started(logStarted) // add started state callback .validated(logValidated); // and validated state callback
const v1 = Validation(obj1) .constraint(p1);
const v2 = Validation(obj2) .constraint(p2);
v1.validate(); // -> "Right."
v2.validate(); // -> "Start processing..." // -> "Sorry, return in 7.5 milliones years." // -> "Processing ended."