bajaux Validators are used to validate data.
new (require("bajaux/Validators"))(eventHandler)
Manages a collection of Validator functions.
Parameters:
Name | Type | Description |
---|---|---|
eventHandler |
The associated eventHandler that has a 'trigger' method. |
Requires
- module:Promise
- module:bajaux/events
Methods
-
add(validator)
-
Add a Validator function. This is used to validate
the a read value before it can be saved.When a Validator function is invoked, the first argument will be the
value. If the function throws or returns a promise that rejects, the value
is considered to be invalid. If the function returns anything else
(including a promise that resolves), the value is considered to be valid.Please note, when saving a modified widget, the value will be read
from the widget, then validated and finally saved. Therefore, the
data passed into the validator for validation will the read data.Parameters:
Name Type Description validator
function - See:
Returns:
Example
validators.add(function (value) { if (!isAcceptable(value)) { return Promise.reject('value not acceptable'); } });
-
get()
-
Return an array copy of the validators.
Returns:
array of Validator functions.
- Type
- Array
-
remove(validator)
-
Remove a Validator function.
Parameters:
Name Type Description validator
function Returns:
-
validate(readValue)
-
Called to validate some data. The read value run against each registered
Validator.To validate a widget, please call module:bajaux/Widget#validate
instead. This method will extract the read value and then call this method.This method should not be overridden. Instead Validators functions
should be added using module:bajaux/Validators#add.When saving a modified widget, the widget will be read, validated
and then saved. The read data is the current representation of
the widget and is passed to the validators. The read data will
not be the same as the loaded current value of the widget that
is used in the save process.Parameters:
Name Type Description readValue
The read value that needs to be validated.
- See:
Returns:
A promise to be resolved with the validated value
- Type
- Promise