How it works...

We handle validation by passing in a closure to our controller structure. For any input that the controller might need to validate, we'd need one of these closures. The advantage to this approach is that we can mock and replace the validation functions at runtime, so testing becomes far simpler. In addition, we're not bound to a single function signature and we can pass in things such as a database connection to our validation functions.

The other thing this recipe demonstrates is returning a typed error called Verror. This type holds validation error messages that can be displayed to users. One shortcoming of this approach is that it doesn't handle multiple validation messages at once. This would be possible by modifying the Verror type to allow for more state, for example, by including a map, in order to house a number of validation errors before it returns from our ValidatePayload function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.240.142