Validation().validate() method
Validates a validatableObject
of the Validation
against constraints added by the Validation().constraint()
method. Runs state callbacks.
Syntax
Parameters
validatableObject
optional
An object to which aValidation
is bound during creation time or via theValidation().bind()
method.
Default:undefined
.
Return value
A Promise
that fufills/rejects depending on the success/fail of the operation:
-
In case of successful execution of the method the
Promise
will be fulfilled with aValidationResult
object no matter valid or invalid. -
If the
Validation
object does not contain predicate groups associated with thevalidatableObject
passed in, thePromise
will be rejected with the error message. -
If an
async
predicate function returns aPromise
that fulfills with any value other thanBoolean
, thePromise
returned by this method will be rejected with the error message. -
If an error occurs in an
async
predicate function, thePromise
will be rejected with the error message.
Exceptions
Any synchronous error that occurs in a predicate function or a state callback will be thrown synchronously unless an error state callback was added, in this case the error will be catched.
Description
For a grouping Validation
, when a validatableObject
is passed in only the predicate group associated with this object will be executed. When nothing is passed in all predicate groups will be executed. The passed in validatableObject
is assigned to the property target
of the ValidationResult
object.
For a single Validation
, it doesn’t matter whether validatableObject
is passed or not, since it contains only one predicate group, and the property target
in this case is always set to the object the Validation
is bound to.
For the reason that predicate functions can be asynchronous with deferred execution, validatable values can be possibly changed during execution of predicate groups, so before running them it preserves current validatable values to ensure data consistency. This means, all predicate functions will be supplied with that data which were at the momement validation process started regardless of their delay.
This method is performed under the hood when a Validation
object is used as a middleware and event handler.
Examples
This example demonstrates the logic described above.