Skip to content

Promise based validation

Roberto Prevato edited this page Dec 27, 2017 · 13 revisions

The DataEntry library implements Promise based validation, therefore supporting validation rules resolved asynchronously (e.g. rules involving AJAX requests) as part of validation process. The validation of a whole form corresponds to a Promise, and each field validation is also a Promise that completes when its validation chain completes.

Validation schema

In DataEntry, the validation of a group of fields is a promise, composed of validation chains (one for each field defined in the dataentry schema object). Each validation chain is a promise that completes when either the first validation rule fails (resolved with error result or rejected), or all validation rules succeed. This way, every field can have asynchronous methods as part of their validation and all errors are populated at once (the first error for each field). If AJAX requests are necessary to validate certain fields, they can and should be executed after synchronous validation that may happen on the client side, to avoid AJAX requests for a value that it's already known to be invalid.

Validation and chains

Note about resolution and rejection of promises

Since the official WC3 specification of the ES6 Promise specifies that the rejection should be used only for exceptional situations (ref. Rejections should be exceptional), the DataEntry library always resolve the promises utilized during the validation of fields and forms: returning a value indicating whether a form is valid or not. Rejection should only happen due to bugs in source code or rejection of a validation rule promise (for example, in case a validation rule requires an AJAX request and a web request completes with status 500). Therefore any rejection must be caused by an unhandled exception happened while applying validation logic, and is ultimately related to a bug in the code. In such situations the DataEntry library is designed to decorate the field for which the validation caused exception and consider the whole form invalid.

Clone this wiki locally