Skip to content

Validation().started() method

Adds state callbacks which will be invoked during the execution of the Validation().validate() method before predicates execution.

Syntax

Validation().started(callback1, callback2, /* ..., */ callbackN)

Parameters

  • callback1, ..., callbackN
    Functions to add. The callback functions will be called with the following arguments:

Return value

The Validation object.

Exceptions

If anything other than a function is passed in the corresponding error will be thrown.

Description

Adds state callbacks to the Validation object. Once the Validation in the specified state, the corresponding callbacks will be invoked one by one in the order they were added. The main purpose of state callbacks is to perform side effects related to the state. A ValidationResult object is passed into state callbacks.

Examples

import { Validation } from "isomorphic-validation";
const { log } = console;
// predicate functions
const predicate1 = (value) => { log(`predicate1(${value})`); return true; };
const predicate2 = (value) => { log(`predicate2(${value})`); return true; };
const predicate3 = (value) => { log(`predicate3(${value})`); return true; };
// state callbacks
const callback1 = (value) => { log(`callback1(${value})`); };
const callback2 = (value) => { log(`callback2(${value})`); };
const callback3 = (value) => { log(`callback3(${value})`); };
const validatableObject = { value: 'obj1' };
Validation(validatableObject)
.constraint(predicate1)
.constraint(predicate2)
.constraint(predicate3)
.started(callback1) // adding state callbacks
.started(callback2, callback3) // adding state callbacks
.validate();
// Output:
// callback1([object ValidationResult])
// callback2([object ValidationResult])
// callback3([object ValidationResult])
// predicate1(obj1)
// predicate2(obj1)
// predicate3(obj1)