diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d1bc31..fa463bf 100644 --- a/CHANGELOG.md +++ b/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/), 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 ### Added diff --git a/go.mod b/go.mod index 344b30a..478f74b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module code.nochebuena.dev/go/valid go 1.25 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 ) diff --git a/go.sum b/go.sum index 7488def..c790e50 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -code.nochebuena.dev/go/xerrors v0.9.0 h1:8wrDto7e44ZW1YPOnT6JrxYXTqnvNuKpAO1/5bcT4TE= -code.nochebuena.dev/go/xerrors v0.9.0/go.mod h1:mtXo7xscBreCB7w7smlBP5Onv8H1HVohCvF0I/VXbAY= +code.nochebuena.dev/go/xerrors v1.0.0 h1:si24SFGa7cHwAxbu75AAEB+a3qRmF118F/BM2SFI7VI= +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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= diff --git a/valid.go b/valid.go index f0aaf4f..45f160a 100644 --- a/valid.go +++ b/valid.go @@ -2,6 +2,8 @@ package valid import ( "errors" + "reflect" + "strings" "code.nochebuena.dev/go/xerrors" 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. +// Field names in error context use the json struct tag when available, +// falling back to the Go field name. func New(opts ...Option) Validator { cfg := &config{mp: DefaultMessages} for _, o := range opts { 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{ - v: playground.New(), + v: v, mp: cfg.mp, } }