Validation() constructor
The Validation() constructor creates Validation
objects.
Syntax
Validation()Validation(validatableObject)Validation(validatableObject, options)
Parameters
-
validatableObject
optional
An object with an accessible by theoptions.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 parameterkeepValid=true
or if theValidation
was created with the parameteroptional=true
.
Default: empty string''
. -
optional
optional
When set totrue
, the predicate group will be executed only if the validatable value is not equal toinitValue
orundefined
otherwise it will be in “valid” state, in other words, makinginitValue
andundefined
valid values but any other value is required to be validated. An “optional”Validation
by default is in “valid” state after creation. On the contrary, for a “non-optional”Validation
, the validatable value equal to the initial value is treated as invalid even if it is in the range of valid values of the added predicate functions.
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.valueconst validation = Validation(validatableObject);
Specified path
import { Validation } from "isomorphic-validation";
const validatableObject = { meaningOfLife: '42',};
// will validate validatableObject.meaningOfLifeconst 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 yetconst validation = Validation(obj, { path: 'a.1.c' });
// ...
// laterobj.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 yetconst fileValidation = Validation(inputFile, { path: 'files.0' });
// ... add file type and size constraints
// validate on changeinputFile.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”, “Age” and “Secret question” Validations
are grouped into one Validation
object which is added as the form’s input event handler. Fill out “First name”, “Last name” and “Secret question” 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. The “Secret question” field has the only constraint that requires its content to be shorter than 250 characters. Now if you clear this field it becomes invalid despite 0 characters being less than 250. This is because for a “non-optional” (required) validation, the initial value is considered as invalid.