Validation().bind()
Binds the Validation to a validatable object.
Syntax
Validation().bind(validatableObject)Validation().bind(validatableObject, options)Parameters
-
validatableObjectoptional
An object with an accessible by theoptions.pathvalue to be validated.
Default: Empty object{}. -
optionsoptional
An object that specifies validation options. The available options are:-
pathoptional
A string path to the validatable value with dots.as separators.
Default:undefined. -
initValueoptional
The initial value is used while validating against constraints added with the parameterkeepValid=trueor if theValidationwas created with the parameteroptional=true.
Default:undefined.
-
Return value
The Validation object.
Exceptions
- If the
validatableObjectis the same as that which theValidationis 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 │// └──────────────┴─────────┘