Validation().bind()
Binds the Validation
to a validatable object.
Syntax
Validation().bind(validatableObject)Validation().bind(validatableObject, options)
Parameters
-
validatableObject
optional
An object with an accessible by theoptions.path
value to be validated.
Default: Empty object{}
. -
options
optional
An object that specifies validation options. The available options are:-
path
optional
A string path to the validatable value with dots.
as separators.
Default:undefined
. -
initValue
optional
The initial value is used while validating against constraints added with the parameterkeepValid=true
or if theValidation
was created with the parameteroptional=true
.
Default:undefined
.
-
Return value
The Validation
object.
Exceptions
- If the
validatableObject
is the same as that which theValidation
is already bound to, throws the corresponding error. - If invoked on a grouping
Validation
, throws the corresponding error.
Description
Binds the Validation
object to the validatableObject
. This method is used on a single Validation
when a validatable object is not accessible at creation time. If options.path
and options.initValue
are not set, uses the values that were set at creation time.
This method is used under the hood in the Validation.profile()
method.
Examples
import { Validation } from 'isomorphic-validation';
// validatable objects are not accessible in this context// creating validationsconst validation1 = Validation();const validation2 = Validation();
// predicate functionconst isMeaningOfLife = (value) => (console.log('value:', value), value === 42);
// ...
// later in the code or in another module// validatable objectsconst obj1 = { value: 41 };const obj2 = { value: 42 };
// binding validations to validatable objects and grouping into oneconst validationGr = Validation.group( validation1.bind(obj1), validation2.bind(obj2)).constraint(isMeaningOfLife); // adding the constraint to the grouped validations
// running validation1 by the bound objectawait validationGr.validate(obj1);console.table({ validation1, validation2, validationGr }, ['isValid']);
// running validation2 by the bound objectawait validationGr.validate(obj2);console.table({ validation1, validation2, validationGr }, ['isValid']);
// Output://// value: 41// ββββββββββββββββ¬ββββββββββ// β (index) β isValid β// ββββββββββββββββΌββββββββββ€// β validation1 β false β// β validation2 β false β// β validationGr β false β// ββββββββββββββββ΄ββββββββββ// value: 42// ββββββββββββββββ¬ββββββββββ// β (index) β isValid β// ββββββββββββββββΌββββββββββ€// β validation1 β false β// β validation2 β true β// β validationGr β false β// ββββββββββββββββ΄ββββββββββ
import { Validation } from 'isomorphic-validation';
// validatable objects are not accessible in this context// creating validationsconst validation1 = Validation();const validation2 = Validation();
// predicate functionconst isMeaningOfLife = (value) => (console.log('value:', value), value === 42);
// ...
// later in the code or in another module// validatable objectsconst obj1 = { value: 41 };const obj2 = { value: 42 };
// grouping validations into oneconst validationGr = Validation.group(validation1, validation2) .constraint(isMeaningOfLife) // adding the constraint to the grouped validations .error((err) => console.error(err.message)); // catching errors
// !!! binding validations after groupingvalidation1.bind(obj1); validation2.bind(obj2);
// running validation1 by the bound objectawait validationGr.validate(obj1); // Error! No predicates associated with obj1
// running validation2 by the bound objectawait validationGr.validate(obj2); // Error! No predicates associated with obj2
// running validation1await validation1.validate();console.table({ validation1, validation2, validationGr }, ['isValid']);
// running validation2await validation2.validate();console.table({ validation1, validation2, validationGr }, ['isValid']);
// Output://// There are no predicates associated with the target {"value":41}// There are no predicates associated with the target {"value":42}// value: 41// ββββββββββββββββ¬ββββββββββ// β (index) β isValid β// ββββββββββββββββΌββββββββββ€// β validation1 β false β// β validation2 β false β// β validationGr β false β// ββββββββββββββββ΄ββββββββββ// value: 42// ββββββββββββββββ¬ββββββββββ// β (index) β isValid β// ββββββββββββββββΌββββββββββ€// β validation1 β false β// β validation2 β true β// β validationGr β false β// ββββββββββββββββ΄ββββββββββ