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

how to validate inner slice item use required_if? #1210

Open
2 tasks done
Orocker opened this issue Jan 9, 2024 · 0 comments
Open
2 tasks done

how to validate inner slice item use required_if? #1210

Orocker opened this issue Jan 9, 2024 · 0 comments

Comments

@Orocker
Copy link

Orocker commented Jan 9, 2024

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

Package version v10:

v10.14.0

Issue, Question or Enhancement:

Now expecting validation to fail because of discount_type=discount outside

Code sample, to showcase or reproduce:

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

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

type StorePromotion struct {
	BatchStair   []StairItem `json:"batch_stair" validate:"required,dive"`
	DiscountType string      `json:"discount_type" validate:"required,oneof=amount discount"`
}

type StairItem struct {
	SubjectNum int8  `json:"subject_num" validate:"required"`
	Value      int64 `json:"value" validate:"required"`
	Limit      int64 `json:"limit" validate:"required_if=DiscountType discount"` # It doesn't work
}

func main() {
	validate := validator.New()
	s := &StorePromotion{
		DiscountType: "discount",
		BatchStair: []StairItem{
			{SubjectNum: 1, Value: 100, Limit: 100},
			{SubjectNum: 2, Value: 90},
			{SubjectNum: 3, Value: 80},
		},
	}
	err := validate.Struct(s)
	fmt.Println(err)
}
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

1 participant