aah Data Validation
aah provides well integrated and seamless data validation feature for your application. It could be applied for URL Path Param, Form, JSON and XML.
- aah internally uses
gopkg.in/go-playground/validator.v9
as validator. It’s simple and effective validation library. Upcoming
planning to externalize the validation feature so that third-party libraries could be registered into aah.
Validation errors are well integrated with aah error handling mechanism.
Table of Contents
- Controller Action Parameters Validation
- How to Validate URL Query Parameters
- Adding Custom Validation Functions
Controller Action Parameters Validation
Action parameter validation applied on struct
for Form
, JSON
, XML
and any struct binding. Basically after successful bind on action parameter type struct
aah applies the validation.
Syntax
Basically you have to define Tag validate
with rules
for the struct fields. Refer to validator documentation.
How to Validate URL Query Parameters
Tip: The best way to validate collective query parameters is to bind those values into struct
then aah automatically does validation on struct and gives a result.
Doing it manually for individual values on-demand basics.
Use method aah.App().ValidateValue(value, "rules")
. Refer to validator documentation
- It returns true if validation passed otherwise false.
For example -
Adding Custom Validation Functions
All capabilities provided by library gopkg.in/go-playground/validator.v9
to add custom validation function.
To obtain aah validator instance
Note: The recommended spot/place to register custom validation functions is at <app-base-dir>/app/init.go
file.