blu is struct value validation based on provided tags.
Note: As of the current version, "blu" is intended for personal use.
Install blu
go get github.com/n4x2/blu
Then import it:
import "github.com/n4x2/blu"
Define struct with validation tags:
type MyStruct struct {
FieldA string `validate:"required"`
// ...
}
Create a new validator instance:
v := blu.NewValidator()
Validate the struct using the created validator:
err := v.Validate(MyStruct)
if err != nil {
// Handle error
}
Create custom rule by implementing the Rule
interface:
type CustomRule struct {}
func (r *CustomRule) Name() string {
return "custom"
}
func (r *CustomRule) Validate(field string, value string, param string) error {
// Implement your custom validation logic here.
// Return an error if the validation fails.
return nil
}
Register the custom rule:
customRule := &CustomRule{}
err := v.RegisterRule(customRule)
if err != nil {
// Handle error (e.g., duplicated rule name).
}
Use the "custom" rule in struct validation tags:
type MyStruct struct {
FieldA string `validate:"custom"`
// ...
}