layout | title | parent | nav_order | grand_parent | permalink |
---|---|---|---|---|---|
default |
Data validation |
Data models |
2 |
Getting started |
/setup_root/data_models/validation |
{: .no_toc }
{: .no_toc .text-delta }
- TOC {:toc}
It is possible to add custom asynchronous validation functions with keyword asyncValidatorFunction
.
If there is a model called example
, the attribute id
in the model would be validated. Only if its value is "1"
, it passes the validation.
In specifically, it should be implemented by two steps:
- find a file called
example.js
in foldervalidations
. - add keyword
asyncValidatorFunction
and corresponding asynchronous validation function for attributeid
invalidatorSchema
.
And the example code is as follows:
example.prototype.validatorSchema = {
"$async": true,
"properties": {
"id": {
"asyncValidatorFunction": async function(data) {
if (data === "1") {
return true
} else {
return new Promise(function(resolve, reject) {
return reject(new Ajv.ValidationError([{
keyword: 'asyncValidatorFunction',
message: `${data} is not 1`
}]))
})
}
}
}
}
}