Skip to content

Validation.glue() method

Groups Validation objects into one to add shared validators which when invoked on one of them invoke the rest.

Syntax

Validation.glue(validation1, validation2, /* ..., */ validationN)
Validation.glue([validation1, validation2, /* ..., */ validationN])
Validation.glue([validation1, validation2], /* ..., */ validationN)
Validation.glue(validation1, [validation2, /* ..., */ validationN])
Validation.glue([[validation1, validation2], /* ..., */ validationN])
Validation.glue(validation1, [[validation2], /* ..., */ validationN])
Validation.glue(validation1, [validation2], /* ..., */ [validationN])

Parameters

  • validation1, ..., validationN
    A Validation object.

Return value

A new grouping Validation object.

Exceptions

If not a Validation was passed in or an Array that contains not a Validation, throws the corresponding error.

Description

Accepts Validation objects or arrays of Validation objects of any nesting level or both in any combination. The Validation objects passed in can be single as well as other grouping Validation objects. Repetitive Validation objects will be ignored, only the unique ones will be grouped. The returned grouping Validation object “subscribes” to state changes of the grouped Validation objects and its validity state depends on them.

The resulting grouping Validation object differs from the one created with the Validation.group() method in the way of adding constraints and passing validatable values to executed predicate functions. That is, when a predicate function is added to it with the Validation().constraint() method, validity states of all the grouped Validation objects start depending on the result of that function execution no matter which one is being validated and validatable values of them all are passed as arguments to the function.

The main purpose of this method is to group mutually dependant validations. A typical use case is validating equality of a password and password confirmation.

Examples

Comparison of .glue() and .group() methods

Consider these two code samples.

validation1 and validation2 are bound to obj1 and obj2 respectively. Then they are “glued” in the first and grouped in the second sample. predicate is added to them through the “gluing”/grouping Validation object. Then they get validated one after another.

When added to the “glued” validations, predicate accepts their both validatable values while being executed whereas on the grouped validations it accepts the only one corresponding value. When invoked on validation1 it makes its counterpart from validation2 invoked and vice versa.

In case of “glued” validations, when validation1 gets validated first time, both validation1 and validation2 become “valid”, so do validationGr as a result. In other words the predicate function executed on one of them also makes the other one execute which affects both validations, that is why they are “glued”.

In case of grouped validations, the predicate function affects only that Validation object it is being called on.

import { Validation } from 'isomorphic-validation';
// validatable objects
const obj1 = { value: 'obj1' };
const obj2 = { value: 'obj2' };
// predicate function
const predicate = (...args) => {
console.log('executing predicate with args:', ...args);
return true;
};
const validation1 = Validation(obj1);
const validation2 = Validation(obj2);
const validationGr = Validation.glue(validation1, validation2)
.constraint(predicate); // adding predicate to the "glued" validations
console.table({validation1, validation2, validationGr}, ['isValid']);
console.log('validating validation1:'); validation1.validate();
console.table({validation1, validation2, validationGr}, ['isValid']);
console.log('validating validation2:'); validation2.validate();
console.table({validation1, validation2, validationGr}, ['isValid']);
// Output
// ┌──────────────┬─────────┐
// │ (index) │ isValid │
// ├──────────────┼─────────┤
// │ validation1 │ false
// │ validation2 │ false
// │ validationGr │ false
// └──────────────┴─────────┘
// validating validation1:
// executing predicate with args: obj1 obj2
// executing predicate with args: obj1 obj2
// ┌──────────────┬─────────┐
// │ (index) │ isValid │
// ├──────────────┼─────────┤
// │ validation1 │ true
// │ validation2 │ true
// │ validationGr │ true
// └──────────────┴─────────┘
// validating validation2:
// executing predicate with args: obj1 obj2
// executing predicate with args: obj1 obj2
// ┌──────────────┬─────────┐
// │ (index) │ isValid │
// ├──────────────┼─────────┤
// │ validation1 │ true
// │ validation2 │ true
// │ validationGr │ true
// └──────────────┴─────────┘

Validating password and password confirmation

In this example areTwoEqual predicate function accepts values from both password and password confirmation fields and changes their both validity status regardless which one is being validated.

Validating one field depending on another

Unlike the previous example where validity of data in password and password confirmation fields is mutually dependent, in this example the dependency is unidirectional, so that validity of data in the experience field depends on data in the age field. Here experienceV is “glued” to an auxiliary validation object bound to the age field and isPossibleExperience accepts data from both fields.