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 uses
gopkg.in/go-playground/validator.v9
as validator. It’s simple and effective validation library.
Validation errors are well integrated with aah error handling mechanism.
Table of Contents
- Route Path Param Validation
- Controller Action Parameters Validation
- How to Validate URL Query Parameters
- Adding Your Custom Validation Functions
Route Path Param Validation
Defining route path parameter validation is very easy in aah. Refer to validator documentation.
Syntax
:paramName[validation-rules]
For example -
# User Info
/v1/users/:emailAddress[email]/addresses
# User Info - Requests
/v1/users/sample@sample.com/addresses => would pass
/v1/users/sample@sample/addresses => would fail
# Book Info
/v1/books/:bookId[isbn13]/excerpt
# Book Info - Requests
/v1/books/978-1-56619-909-4/excerpt => would pass
/v1/books/dshgdshgdsjhgdshg/excerpt => would fail
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.
// User contains user information
type User struct {
FirstName string `validate:"required"`
LastName string `validate:"required"`
Age uint8 `validate:"gte=0,lte=130"`
Email string `validate:"required,email"`
FavouriteColor string `validate:"iscolor"` // alias for 'hexcolor|rgb|rgba|hsl|hsla'
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
}
// Address houses a users address information
type Address struct {
Street string `validate:"required"`
City string `validate:"required"`
Planet string `validate:"required"`
Phone string `validate:"required"`
}
// Create method used to create an user.
func (u *UserController) Create(user *User) {
// Implementation goes here
}
// HandleError method called on any errors happens within the user controller.
// More info, read https://docs.aahframework.org/error-handling.html
func (u *UserController) HandleError(e *aah.Error) bool {
// handle error
return true
}
How to Validate URL Query Parameters
Pro Tip: The best way to validate collective query parameters is to bind those values into struct
then aah automatically does validation on struct gives you a result.
You have do it manually for individual values on-demand basics. How to do?
Use method aah.ValidateValue(value, "rules")
. Refer to validator documentation
- It returns true if validation passed otherwise false.
For example -
i := 15
result := aah.ValidateValue(i, "gt=1,lt=10")
emailAddress := "sample@sample"
result := aah.ValidateValue(emailAddress, "email")
numbers := []int{23, 67, 87, 23, 90}
result := aah.ValidateValue(numbers, "unique")
color := "#e25657"
result := aah.ValidateValue(color, "iscolor") // alias for 'hexcolor|rgb|rgba|hsl|hsla'
Adding Your Custom Validation Functions
You can use all capabilities provided by library gopkg.in/go-playground/validator.v9
to add custom validator implementation.
To obtain aah validator instance
validator := aah.Validator()
// Add your validation funcs
Note: The recommended spot/place to register custom validation functions is at init.go
file.