feat(valid)!: promote to v1.0.0 — JSON tag name resolution, bump xerrors to v1.0.0
Resolve JSON tag name resolution roadmap item: field names in error context now use the json struct tag when available, falling back to the Go field name. Commits MessageProvider interface as stable. Bumps xerrors dependency from v0.9.0 to v1.0.0. API committed as stable.
This commit is contained in:
19
CHANGELOG.md
19
CHANGELOG.md
@@ -5,6 +5,25 @@ All notable changes to this module will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [1.0.0] — 2026-05-12
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- JSON tag name resolution — field names in error context now use the `json`
|
||||||
|
struct tag when available (e.g. `"email_address"` instead of `"EmailAddress"`),
|
||||||
|
falling back to the Go field name when no json tag is defined or the tag is `"-"`.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `xerrors` dependency bumped from v0.9.0 to v1.0.0.
|
||||||
|
|
||||||
|
### Unchanged
|
||||||
|
|
||||||
|
All existing API (`Validator`, `New`, `WithMessageProvider`, `MessageProvider`,
|
||||||
|
`DefaultMessages`, `SpanishMessages`) is API-compatible with v0.9.0.
|
||||||
|
|
||||||
|
[1.0.0]: https://code.nochebuena.dev/go/valid/releases/tag/v1.0.0
|
||||||
|
|
||||||
## [0.9.0] - 2026-03-18
|
## [0.9.0] - 2026-03-18
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module code.nochebuena.dev/go/valid
|
|||||||
go 1.25
|
go 1.25
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.nochebuena.dev/go/xerrors v0.9.0
|
code.nochebuena.dev/go/xerrors v1.0.0
|
||||||
github.com/go-playground/validator/v10 v10.30.1
|
github.com/go-playground/validator/v10 v10.30.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
|||||||
code.nochebuena.dev/go/xerrors v0.9.0 h1:8wrDto7e44ZW1YPOnT6JrxYXTqnvNuKpAO1/5bcT4TE=
|
code.nochebuena.dev/go/xerrors v1.0.0 h1:si24SFGa7cHwAxbu75AAEB+a3qRmF118F/BM2SFI7VI=
|
||||||
code.nochebuena.dev/go/xerrors v0.9.0/go.mod h1:mtXo7xscBreCB7w7smlBP5Onv8H1HVohCvF0I/VXbAY=
|
code.nochebuena.dev/go/xerrors v1.0.0/go.mod h1:mtXo7xscBreCB7w7smlBP5Onv8H1HVohCvF0I/VXbAY=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||||
|
|||||||
14
valid.go
14
valid.go
@@ -2,6 +2,8 @@ package valid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.nochebuena.dev/go/xerrors"
|
"code.nochebuena.dev/go/xerrors"
|
||||||
playground "github.com/go-playground/validator/v10"
|
playground "github.com/go-playground/validator/v10"
|
||||||
@@ -33,13 +35,23 @@ func WithMessageProvider(mp MessageProvider) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New returns a Validator. Without options, DefaultMessages (English) is used.
|
// New returns a Validator. Without options, DefaultMessages (English) is used.
|
||||||
|
// Field names in error context use the json struct tag when available,
|
||||||
|
// falling back to the Go field name.
|
||||||
func New(opts ...Option) Validator {
|
func New(opts ...Option) Validator {
|
||||||
cfg := &config{mp: DefaultMessages}
|
cfg := &config{mp: DefaultMessages}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(cfg)
|
o(cfg)
|
||||||
}
|
}
|
||||||
|
v := playground.New()
|
||||||
|
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
||||||
|
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
|
||||||
|
if name == "" || name == "-" {
|
||||||
|
return fld.Name
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
})
|
||||||
return &validator{
|
return &validator{
|
||||||
v: playground.New(),
|
v: v,
|
||||||
mp: cfg.mp,
|
mp: cfg.mp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user