From ab11fd2ace796c7aead0aeb45fcf941da9182292 Mon Sep 17 00:00:00 2001 From: Rene Nochebuena Guerrero Date: Mon, 11 May 2026 18:18:05 -0600 Subject: [PATCH] =?UTF-8?q?feat(valid)!:=20promote=20to=20v1.0.0=20?= =?UTF-8?q?=E2=80=94=20JSON=20tag=20name=20resolution,=20bump=20xerrors=20?= =?UTF-8?q?to=20v1.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- CHANGELOG.md | 19 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- valid.go | 14 +++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-) 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, } }