Validate a JSON body with an action
In Total.js 5 you declare the input of an action with input. A star in front of a field name (*email) marks it as required, and the type after the colon (Email) is validated by the framework. Your action function runs only when the data is valid.
Routes point to an action with the --> operator. The * prefix means "action" (as opposed to a controller function).
NEWACTION('Subscribe/exec', {
name: 'Subscribe to the newsletter',
input: '*email:Email, name:String',
action: function($, model) {
$.success(model.email);
}
});exports.install = function() {
ROUTE('POST /subscribe/ --> *Subscribe/exec');
};Send a valid body and the action is executed. Send an invalid e-mail address and the framework answers with a validation error without calling your function.