Validation() constructor
The Validation() constructor creates Validation
objects.
Syntax
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 optionalValidation
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
Specified path
Dot separated path
Binding to a file input element
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.