mirror of
https://github.com/romdo/go-validate.git
synced 2026-02-19 08:06:40 +00:00
chore(ci/lint/config): update golangci-lint configuration and workflows
Refactor the golangci-lint configuration to use the latest version and update GitHub Actions workflows. Key changes include: - Update golangci-lint to v2.6 - Update GitHub Actions to use latest checkout and setup-go actions - Update Go versions in test matrix - Remove deprecated cache steps - Update Makefile golangci-lint tool version - Minor documentation formatting improvements
This commit is contained in:
98
validate.go
98
validate.go
@@ -1,13 +1,13 @@
|
||||
// Package validate is yet another Go struct/object validation package, with a
|
||||
// focus on simplicity, flexibility, and full control over validation logic.
|
||||
//
|
||||
// Interface
|
||||
// # Interface
|
||||
//
|
||||
// To add validation to any type, simply implement the Validatable interface:
|
||||
//
|
||||
// type Validatable interface {
|
||||
// Validate() error
|
||||
// }
|
||||
// type Validatable interface {
|
||||
// Validate() error
|
||||
// }
|
||||
//
|
||||
// To mark a object as failing validation, the Validate method simply needs to
|
||||
// return a error.
|
||||
@@ -17,7 +17,7 @@
|
||||
// can be fully validated, and the nested path to each object is tracked and
|
||||
// reported back any validation errors.
|
||||
//
|
||||
// Multiple Errors
|
||||
// # Multiple Errors
|
||||
//
|
||||
// Multiple errors can be reported from the Validate method using one of the
|
||||
// available Append helper functions which append errors together. Under the
|
||||
@@ -25,53 +25,53 @@
|
||||
// a single error return type, and you can in fact just directly use multierr in
|
||||
// the a type's Validate method.
|
||||
//
|
||||
// Structs and Field-specific Errors
|
||||
// # Structs and Field-specific Errors
|
||||
//
|
||||
// When validating a struct, you are likely to have multiple errors for multiple
|
||||
// fields. To specify which field on the struct the error relates to, you have
|
||||
// to return a *validate.Error instead of a normal Go error type. For example:
|
||||
//
|
||||
// type Book struct {
|
||||
// Title string
|
||||
// Author string
|
||||
// }
|
||||
// type Book struct {
|
||||
// Title string
|
||||
// Author string
|
||||
// }
|
||||
//
|
||||
// func (s *Book) Validate() error {
|
||||
// var errs error
|
||||
// func (s *Book) Validate() error {
|
||||
// var errs error
|
||||
//
|
||||
// if s.Title == "" {
|
||||
// errs = validate.Append(errs, &validate.Error{
|
||||
// Field: "Title", Msg: "is required",
|
||||
// })
|
||||
// }
|
||||
// if s.Title == "" {
|
||||
// errs = validate.Append(errs, &validate.Error{
|
||||
// Field: "Title", Msg: "is required",
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// if s.Author == "" {
|
||||
// // Yields the same result as the Title field check above.
|
||||
// errs = validate.AppendFieldError(errs, "Author", "is required")
|
||||
// }
|
||||
// if s.Author == "" {
|
||||
// // Yields the same result as the Title field check above.
|
||||
// errs = validate.AppendFieldError(errs, "Author", "is required")
|
||||
// }
|
||||
//
|
||||
// return errs
|
||||
// }
|
||||
// return errs
|
||||
// }
|
||||
//
|
||||
// With the above example, if you validate a empty *Book:
|
||||
//
|
||||
// err := validate.Validate(&Book{})
|
||||
// for _, e := range validate.Errors(err) {
|
||||
// fmt.Println(e.Error())
|
||||
// }
|
||||
// err := validate.Validate(&Book{})
|
||||
// for _, e := range validate.Errors(err) {
|
||||
// fmt.Println(e.Error())
|
||||
// }
|
||||
//
|
||||
// The following errors would be printed:
|
||||
//
|
||||
// Title: is required
|
||||
// Kind: is required
|
||||
// Title: is required
|
||||
// Kind: is required
|
||||
//
|
||||
// Error type
|
||||
// # Error type
|
||||
//
|
||||
// All errors will be wrapped in a *Error before being returned, which is used
|
||||
// to keep track of the path and field the error relates to. There are various
|
||||
// helpers available to create Error instances.
|
||||
//
|
||||
// Handling Validation Errors
|
||||
// # Handling Validation Errors
|
||||
//
|
||||
// As mentioned above, multiple errors are wrapped up into a single error return
|
||||
// value using go.uber.org/multierr. You can access all errors individually with
|
||||
@@ -79,7 +79,7 @@
|
||||
// function is just wrapper around multierr.Errors(), so you could use that
|
||||
// instead if you prefer.
|
||||
//
|
||||
// Struct Field Tags
|
||||
// # Struct Field Tags
|
||||
//
|
||||
// Fields on a struct which customize the name via a json, yaml, or form field
|
||||
// tag, will automatically have the field name converted to the name in the tag
|
||||
@@ -88,7 +88,7 @@
|
||||
// You can customize the field name conversion logic by creating a custom
|
||||
// Validator instance, and calling FieldNameFunc() on it.
|
||||
//
|
||||
// Nested Validatable Objects
|
||||
// # Nested Validatable Objects
|
||||
//
|
||||
// All items/fields on any structs, maps, slices or arrays which are encountered
|
||||
// will be validated if they implement the Validatable interface. While
|
||||
@@ -103,30 +103,30 @@
|
||||
// As an example, if our Book struct from above is nested within the following
|
||||
// structs:
|
||||
//
|
||||
// type Order struct {
|
||||
// Items []*Item `json:"items"`
|
||||
// }
|
||||
// type Order struct {
|
||||
// Items []*Item `json:"items"`
|
||||
// }
|
||||
//
|
||||
// type Item struct {
|
||||
// Book *Book `json:"book"`
|
||||
// }
|
||||
// type Item struct {
|
||||
// Book *Book `json:"book"`
|
||||
// }
|
||||
//
|
||||
// And we have a Order where the book in the second Item has a empty Author
|
||||
// field:
|
||||
//
|
||||
// err := validate.Validate(&Order{
|
||||
// Items: []*Item{
|
||||
// {Book: &Book{Title: "The Traveler", Author: "John Twelve Hawks"}},
|
||||
// {Book: &Book{Title: "The Firm"}},
|
||||
// },
|
||||
// })
|
||||
// for _, e := range validate.Errors(err) {
|
||||
// fmt.Println(e.Error())
|
||||
// }
|
||||
// err := validate.Validate(&Order{
|
||||
// Items: []*Item{
|
||||
// {Book: &Book{Title: "The Traveler", Author: "John Twelve Hawks"}},
|
||||
// {Book: &Book{Title: "The Firm"}},
|
||||
// },
|
||||
// })
|
||||
// for _, e := range validate.Errors(err) {
|
||||
// fmt.Println(e.Error())
|
||||
// }
|
||||
//
|
||||
// Then we would get the following error:
|
||||
//
|
||||
// items.1.book.Author: is required
|
||||
// items.1.book.Author: is required
|
||||
//
|
||||
// Note how both "items" and "book" are lower cased thanks to the json tags on
|
||||
// the struct fields, while our Book struct does not have a json tag for the
|
||||
|
||||
Reference in New Issue
Block a user