Validating input for Go structs and user inputs
Validation for web can be a difficult problem. This recipe will explore using closures to support easy mocking of validation functions and to allow flexibility in the type of validation performed when initializing a controller struct as described by the previous recipe.
We'll perform this validation on a struct, but not explore how to populate the struct. We can assume that the data will be populated by parsing a JSON payload, populating explicitly from the form input, or other methods.
Getting ready
Refer to the steps given in the Getting ready section of the Working with web handlers, requests, and ResponseWriters recipe.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create and navigate to the
chapter7/validation
directory. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter7/validation or use this as an exercise to write some of your own code.
- Create a file...