Skip to content

Validation().validate() method

Validates a validatableObject of the Validation against constraints added by the Validation().constraint() method. Runs state callbacks.

Syntax

Validation().validate()
Validation().validate(validatableObject)

Parameters

Return value

A Promise that fufills/rejects depending on the success/fail of the operation:

  • In case of successful execution of the method the Promise will be fulfilled with a ValidationResult object no matter valid or invalid.

  • If the Validation object does not contain predicate groups associated with the validatableObject passed in, the Promise will be rejected with the error message.

  • If an async predicate function returns a Promise that fulfills with any value other than Boolean, the Promise returned by this method will be rejected with the error message.

  • If an error occurs in an async predicate function, the Promise will be rejected with the error message.

Exceptions

Any synchronous error that occurs in a predicate function or a state callback will be thrown synchronously unless an error state callback was added, in this case the error will be catched.

Description

For a grouping Validation, when a validatableObject is passed in only the predicate group associated with this object will be executed. When nothing is passed in all predicate groups will be executed. The passed in validatableObject is assigned to the property target of the ValidationResult object.

For a single Validation, it doesn’t matter whether validatableObject is passed or not, since it contains only one predicate group, and the property target in this case is always set to the object the Validation is bound to.

For the reason that predicate functions can be asynchronous with deferred execution, validatable values can be possibly changed during execution of predicate groups, so before running them it preserves current validatable values to ensure data consistency. This means, all predicate functions will be supplied with that data which were at the momement validation process started regardless of their delay.

This method is performed under the hood when a Validation object is used as a middleware and event handler.

Examples

This example demonstrates the logic described above.

import { Validation } from "isomorphic-validation";
// predicate functions
const isString = (value) => {
console.log('isString(), value:', value);
return typeof value === 'string';
};
const isObj = (num) => (value) => {
console.log(`isObj(${num}), value:`, value);
return value === `isObj${num}`;
};
// validatable objects
const obj1 = { value: 'obj1' };
const obj2 = { value: 'obj2' };
const v = Validation.group(
Validation(obj1)
.constraint(isObj(1)),
Validation(obj2)
.constraint(isObj(2))
)
.constraint(isString);
console.log('\n--> executing a predicate group, associated with obj1');
const { target: target1 } = await v.validate(obj1);
console.log('\n--> executing a predicate group, associated with obj2');
const { target: target2 } = await v.validate(obj2);
console.log('\n--> executing all predicate groups');
const { target: target3 } = await v.validate();
console.log('\n--> no predicate group associated with this object');
await v.validate({ value: 'dummy' }).catch(err => console.log(err.message));
console.log('\n--> targets:', target1, target2, target3);
// Output:
// --> executing a predicate group, associated with obj1
// isObj(1), value: obj1
// isString(), value: obj1
//
// --> executing a predicate group, associated with obj2
// isObj(2), value: obj2
// isString(), value: obj2
//
// --> executing all predicate groups
// isObj(1), value: obj1
// isString(), value: obj1
// isObj(2), value: obj2
// isString(), value: obj2
//
// --> no predicate group associated with this object
// There are no predicates assosiated with the target {"value":"dummy"}
//
// --> targets: { value: 'obj1' } { value: 'obj2' } undefined