Skip to content

Validation() constructor

The Validation() constructor creates Validation objects.

Syntax

Validation()
Validation(validatableObject)
Validation(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: string 'value'.

    • 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: empty string ''.

    • optional optional
      When set to true, the predicate group will be executed only if the validatable value is not equal to initValue or undefined otherwise it will be in “valid” state, in other words, making initValue and undefined valid values but any other value is required to be validated. An optional Validation by default is in “valid” state after creation.
      Default: false.

Return value

A new Validation instance.

Description

Creates a new single Validation instance bound to the validatableObject. The path evaluates while the Validation().validate() method is being called or when the created instance is being invoked as a middleware or event handler. So it is acceptable for the property that is being accessed by the path to not exist at the creation time. This may be useful for example when validating file meta data on a file input element <input type="file" /> since particular properties do not exist until a file is opened.

Examples

Default path

import { Validation } from "isomorphic-validation";
const validatableObject = {
value: '42',
};
// will validate validatableObject.value
const validation = Validation(validatableObject);

Specified path

import { Validation } from "isomorphic-validation";
const validatableObject = {
meaningOfLife: '42',
};
// will validate validatableObject.meaningOfLife
const validation = Validation(validatableObject, { path: 'meaningOfLife' });

Dot separated path

import { Validation } from "isomorphic-validation";
const obj = {
a: [
{
b: 'b', // obj.a[0].b
},
],
};
// will validate obj.a[1].c - which does not exist yet
const validation = Validation(obj, { path: 'a.1.c' });
// ...
// later
obj.a.push({ c: 'c' });
validation.validate();

Binding to a file input element

<form name="form">
<input type="file" name="file">
</form>
import { Validation } from "isomorphic-validation";
const inputFile = document.form.file;
// will validate metadata of inputFile.files[0] - which does not exist yet
const fileValidation = Validation(inputFile, { path: 'files.0' });
// ... add file type and size constraints
// validate on change
inputFile.addEventListener('change', (e) => {
// now inputFile.files[0] exists
if (inputFile.files.length) {
fileValidation.validate();
}
});

Parameter “optional”

Some libraries offer marking form fields as “required” in one way or another. HTML input elements have the required attribute. In fact, in most cases form fields are required to be filled out. This library has the opposite approach. Instead of marking almost every field as “required”, it offers making only needed validations “optional”.

In this example “First name”, “Last name”, and “Age” Validations are grouped into one Validation object which is added as the form’s input event handler. Fill out “First name” and “Last name” fields, the submit button gets enabled even though the “Age” field is empty. Start filling out the “Age” field, the submit button gets disabled. As this field is optional, it should either be left empty or contain a valid number value.