govalidator
is a Go package that provides a flexible and extensible validation framework with support for localization. It leverages the go-playground/validator
package for validation and mekramy/goi18n
for localization.
- Custom validation rules
- Localized error messages
- Struct and variable validation
- Support for various data types and formats
To install govalidator
, use the following command:
go get github.com/mekramy/govalidator
Here's a basic example of how to use govalidator
:
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/mekramy/goi18n"
"github.com/mekramy/govalidator"
"golang.org/x/text/language"
)
func main() {
// Initialize the validator
v := govalidator.NewValidator(
validator.New(),
govalidator.WithTranslator(
goi18n.NewTranslator("en", language.English),
"",
),
)
// Add custom validation rule
v.AddValidation("is_valid", func(fl validator.FieldLevel) bool {
return fl.Field().String() == "valid"
})
v.AddTranslation("en", "is_valid", "{field} must be valid")
// Validate a variable
err := v.Var("en", "my_field", "valid", "required,is_valid")
if err.HasInternalError() {
fmt.Println("Internal error:", err.InternalError())
} else if err.HasValidationErrors() {
fmt.Println("Validation errors:", err)
} else {
fmt.Println("Validation passed")
}
}
You can also validate structs with govalidator
:
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/mekramy/goi18n"
"github.com/mekramy/govalidator"
"golang.org/x/text/language"
)
type TestStruct struct {
Field string `validate:"required,is_valid"`
}
func main() {
// Initialize the validator
v := govalidator.NewValidator(
validator.New(),
govalidator.WithTranslator(
goi18n.NewTranslator("en", language.English),
"",
),
)
// Add custom validation rule
v.AddValidation("is_valid", func(fl validator.FieldLevel) bool {
return fl.Field().String() == "valid"
})
v.AddTranslation("en", "is_valid", "{field} must be valid")
// Validate a struct
ts := TestStruct{Field: "valid"}
err := v.Struct("en", ts)
if err.HasInternalError() {
fmt.Println("Internal error:", err.InternalError())
} else if err.HasValidationErrors() {
fmt.Println("Validation errors:", err)
} else {
fmt.Println("Validation passed")
}
}
You can add custom validators to govalidator
:
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/mekramy/goi18n"
"github.com/mekramy/govalidator"
"golang.org/x/text/language"
)
func main() {
// Initialize the validator
v := govalidator.NewValidator(
validator.New(),
govalidator.WithTranslator(
goi18n.NewTranslator("en", language.English),
"",
),
)
// Add custom validation rule
v.AddValidation("is_valid", func(fl validator.FieldLevel) bool {
return fl.Field().String() == "valid"
})
v.AddTranslation("en", "is_valid", "{field} must be valid")
// Validate a variable
err := v.Var("en", "my_field", "invalid", "required,is_valid")
if err.HasInternalError() {
fmt.Println("Internal error:", err.InternalError())
} else if err.HasValidationErrors() {
fmt.Println("Validation errors:", err)
} else {
fmt.Println("Validation passed")
}
}
This project is licensed under the ISC License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any changes.