Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: unable to translate time.Duration validation error #1069

Closed
2 tasks done
wijayaerick opened this issue Feb 13, 2023 · 1 comment
Closed
2 tasks done

bug: unable to translate time.Duration validation error #1069

wijayaerick opened this issue Feb 13, 2023 · 1 comment

Comments

@wijayaerick
Copy link

  • I have looked at the documentation here first?
  • I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

The package docs mentioned that time.Duration are supported for some validations e.g. min, max, eq, etc.
However the translations code (at least the EN one) fails when time.Duration validation error occurs.
It happens because the translation assumes the value to be convertible to number (float), as seen in code below:

f64, err := strconv.ParseFloat(fe.Param(), 64)

Code sample, to showcase or reproduce:

https://go.dev/play/p/E2e5iYMb2BC

package main

import (
	"fmt"
	"time"

	"github.com/go-playground/locales/en"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	entranslations "github.com/go-playground/validator/v10/translations/en"
)

type Test struct {
	T1 time.Duration `validate:"min=1h30m,max=2h"`
	T2 time.Duration `validate:"min=1h30m,max=2h"`
	T3 time.Duration `validate:"min=1h30m,max=2h"`
}

func main() {
	validate := validator.New()
	enLocale := en.New()
	uniTranslator := ut.New(enLocale, enLocale)
	translator, _ := uniTranslator.GetTranslator("en")
	if err := entranslations.RegisterDefaultTranslations(validate, translator); err != nil {
		panic(err)
	}

	err := validate.Struct(Test{T1: 1 * time.Hour, T2: 2*time.Hour + 10*time.Minute, T3: 1*time.Hour + 40*time.Minute})
	for _, verr := range err.(validator.ValidationErrors) {
		fmt.Printf("  %s (value was '%v')\n", verr.Translate(translator), verr.Value())
	}
	// Expected T1 & T2 to have translated error:
	// - T1 must be 1h30m or more
	// - T2 must be 2h or less
}
@vuon9
Copy link
Contributor

vuon9 commented Feb 13, 2023

Looks like the translation's logic hasn't handled for time.Duration value yet.

nodivbyzero added a commit that referenced this issue Feb 22, 2025
## Fixes Or Enhances

This PR adds `time.Duration` translation and unit test cases.

Connected to: #1069 

**Make sure that you've checked the boxes below before you submit PR:**
- [x] Tests exist or have been written that cover this particular
change.

@go-playground/validator-maintainers
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

No branches or pull requests

3 participants