Skip to content

fix: translate time.Duration errors for len, lt, lte, gt and gte in en - #1688

Open
yxshee wants to merge 1 commit into
go-playground:masterfrom
yxshee:fix/duration-translations
Open

yxshee wants to merge 1 commit into
go-playground:masterfrom
yxshee:fix/duration-translations

Conversation

@yxshee

@yxshee yxshee commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Fixes Or Enhances

The English translations only handle time.Duration params for min and max (#1154). For len, lt, lte, gt and gte the param is still passed to strconv.ParseFloat, which fails on values like 1h, so Translate prints a warning and falls back to the raw error string:

type S struct {
	D time.Duration `validate:"gt=1h"`
}
err := validate.Struct(S{D: time.Minute})
fmt.Println(err.(validator.ValidationErrors)[0].Translate(trans))

Actual: warning: error translating FieldError: strconv.ParseFloat: parsing "1h": invalid syntax followed by Key: 'S.D' Error:Field validation for 'D' failed on the 'gt' tag
Expected: D must be greater than 1h

This applies the same time.Duration branch that min and max already have to len, lt, lte, gt and gte. len gets the same lazy ParseFloat closure the others use, so the duration branch is reached before parsing.

Tested with five new time.Duration fields in TestTranslations. On master the test fails with LenDuration must be equal to 1h does not equal Key: 'Test.LenDuration' Error:Field validation for 'LenDuration' failed on the 'len' tag. With the change go test -race ./..., go vet ./... and golangci-lint v2.13.1 all pass.

This PR was AI-assisted: I used an AI coding assistant to help find the bug and write the change, and I reviewed and tested it myself.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@yxshee
yxshee requested a review from a team as a code owner November 8, 2026 21:46
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 72.604% (+0.02%) from 72.581% — yxshee:fix/duration-translations into go-playground:master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants