Skip to content

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 the options.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 parameter keepValid=true or if the Validation was created with the parameter optional=true.
      Default: undefined.

Return value

The Validation object.

Exceptions

  • If the validatableObject is the same as that which the Validation 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 validations
const validation1 = Validation();
const validation2 = Validation();
// predicate function
const isMeaningOfLife = (value) => (console.log('value:', value), value === 42);
// ...
// later in the code or in another module
// validatable objects
const obj1 = { value: 41 };
const obj2 = { value: 42 };
// binding validations to validatable objects and grouping into one
const validationGr = Validation.group(
validation1.bind(obj1), validation2.bind(obj2)
)
.constraint(isMeaningOfLife); // adding the constraint to the grouped validations
// running validation1 by the bound object
await validationGr.validate(obj1);
console.table({ validation1, validation2, validationGr }, ['isValid']);
// running validation2 by the bound object
await 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 validations
const validation1 = Validation();
const validation2 = Validation();
// predicate function
const isMeaningOfLife = (value) => (console.log('value:', value), value === 42);
// ...
// later in the code or in another module
// validatable objects
const obj1 = { value: 41 };
const obj2 = { value: 42 };
// grouping validations into one
const 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 grouping
validation1.bind(obj1); validation2.bind(obj2);
// running validation1 by the bound object
await validationGr.validate(obj1); // Error! No predicates associated with obj1
// running validation2 by the bound object
await validationGr.validate(obj2); // Error! No predicates associated with obj2
// running validation1
await validation1.validate();
console.table({ validation1, validation2, validationGr }, ['isValid']);
// running validation2
await 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 │
// └──────────────┴─────────┘