cmvalidator
is a custom validation library for Go that extends the popular go-playground/validator/v10
package. This library allows developers to easily specify custom error messages for struct fields using struct tags.
- Custom Error Messages: Specify custom validation messages directly in struct tags.
- Seamless Integration: Built on top of
go-playground/validator/v10
, making it easy to integrate with existing Go projects.
go get github.com/yudai2929/cmvalidator
Add custom validation messages to your struct fields using the customMessage
tag:
package main
import (
"github.com/yudai2929/cmvalidator"
)
type User struct {
FirstName string `validate:"required" customMessage:"First Name is required!"`
LastName string `validate:"required" customMessage:"Last Name is required!"`
Age int `validate:"gte=0,lte=130" customMessage:"Age must be between 0 and 130!"`
Email string `validate:"required,email" customMessage:"Email is invalid!"`
}
Use the cmvalidator
package to validate your struct:
package main
import (
"errors"
"github.com/yudai2929/cmvalidator"
)
func main() {
// Create a new instance of the validator
validate := cmvalidator.New()
// Create a new instance of the struct to validate
newUser := User{
FirstName: "John",
LastName: "Doe",
Age: 200, // invalid age
Email: "john@example.com",
}
// Validate the struct
if err := validate.Struct(newUser); err != nil {
var cmvalidateErrors cmvalidator.CFValidateErrors
if errors.As(err, &cmvalidateErrors) {
// Handle the error
for _, cmvalidateError := range cmvalidateErrors {
// Print the custom message
println("Custom Message:", cmvalidateError.CustomMessage())
}
} else {
// Handle the error
}
}
}
The custom validation errors can be accessed and managed using the CustomMessage
method:
for _, cmvalidateError := range cmvalidateErrors {
println("Custom Message:", cmvalidateError.CustomMessage())
}
This project is licensed under the MIT License - see the LICENSE file for details.