Skip to content

Validation().valid() method

Adds state callbacks which will be invoked during execution of the Validation().validate() method after all predicate groups of the Validation become “valid”, in other words when all predicate functions return true or a Promise that fulfills with true.

Syntax

Validation().valid(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 = ({ isValid }) => { log(`callback1({ isValid: ${isValid} })`); };
const callback2 = ({ isValid }) => { log(`callback2({ isValid: ${isValid} })`); };
const callback3 = ({ isValid }) => { log(`callback3({ isValid: ${isValid} })`); };
const validatableObject = { value: 'obj1' };
Validation(validatableObject)
.constraint(predicate1)
.constraint(predicate2)
.constraint(predicate3)
.valid(callback1) // adding state callbacks
.valid(callback2, callback3) // adding state callbacks
.validate();
// Output:
// predicate1(obj1)
// predicate2(obj1)
// predicate3(obj1)
// callback1({ isValid: true })
// callback2({ isValid: true })
// callback3({ isValid: true })