From 8cf4f8c311b4e43ef9ec33db5c170f9cd5225183 Mon Sep 17 00:00:00 2001 From: Jahanvi Aggarwal Date: Sun, 10 Jul 2022 18:17:38 +0530 Subject: [PATCH] Adding Support for SkipUnless validator and testing the same --- non-standard/validators/notblank_test.go | 2 +- translations/it/it.go | 4 +- validator_test.go | 76 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/non-standard/validators/notblank_test.go b/non-standard/validators/notblank_test.go index 78c8781..cc9d0c1 100644 --- a/non-standard/validators/notblank_test.go +++ b/non-standard/validators/notblank_test.go @@ -3,8 +3,8 @@ package validators import ( "testing" - "github.com/go-playground/validator/v10" "github.com/go-playground/assert/v2" + "github.com/go-playground/validator/v10" ) type test struct { diff --git a/translations/it/it.go b/translations/it/it.go index 0b46fc4..35b5d80 100644 --- a/translations/it/it.go +++ b/translations/it/it.go @@ -124,7 +124,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "min", customRegisFunc: func(ut ut.Translator) (err error) { - + if err = ut.Add("min-string", "{0} deve essere lungo almeno {1}", false); err != nil { return } @@ -432,7 +432,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "lte", customRegisFunc: func(ut ut.Translator) (err error) { - + if err = ut.Add("lte-string", "{0} deve essere lungo al massimo {1}", false); err != nil { return } diff --git a/validator_test.go b/validator_test.go index 0d78a8b..87bce40 100644 --- a/validator_test.go +++ b/validator_test.go @@ -10492,6 +10492,82 @@ func TestRequiredUnless(t *testing.T) { _ = validate.Struct(test3) } +func TestSkipUnless(t *testing.T) { + type Inner struct { + Field *string + } + + fieldVal := "test1" + test := struct { + Inner *Inner + FieldE string `validate:"omitempty" json:"field_e"` + FieldER string `validate:"skip_unless=FieldE test" json:"field_er"` + Field1 string `validate:"omitempty" json:"field_1"` + Field2 *string `validate:"skip_unless=Field1 test" json:"field_2"` + Field3 map[string]string `validate:"skip_unless=Field2 test" json:"field_3"` + Field4 interface{} `validate:"skip_unless=Field3 1" json:"field_4"` + Field5 int `validate:"skip_unless=Inner.Field test" json:"field_5"` + Field6 uint `validate:"skip_unless=Field5 2" json:"field_6"` + Field7 float32 `validate:"skip_unless=Field6 1" json:"field_7"` + Field8 float64 `validate:"skip_unless=Field7 1.0" json:"field_8"` + Field9 bool `validate:"omitempty" json:"field_9"` + Field10 string `validate:"skip_unless=Field9 false" json:"field_10"` + }{ + FieldE: "test1", + Field2: &fieldVal, + Field3: map[string]string{"key": "val"}, + Field4: "test", + Field5: 3, + Field9: true, + } + + validate := New() + + errs := validate.Struct(test) + Equal(t, errs, nil) + + test2 := struct { + Inner *Inner + Inner2 *Inner + FieldE string `validate:"omitempty" json:"field_e"` + FieldER string `validate:"skip_unless=FieldE test" json:"field_er"` + Field1 string `validate:"omitempty" json:"field_1"` + Field2 *string `validate:"skip_unless=Field1 test" json:"field_2"` + Field3 map[string]string `validate:"skip_unless=Field2 test" json:"field_3"` + Field4 interface{} `validate:"skip_unless=Field2 test" json:"field_4"` + Field5 string `validate:"skip_unless=Field3 0" json:"field_5"` + Field6 string `validate:"skip_unless=Inner.Field test" json:"field_6"` + Field7 string `validate:"skip_unless=Inner2.Field test" json:"field_7"` + Field8 bool `validate:"omitempty" json:"field_8"` + Field9 string `validate:"skip_unless=Field8 true" json:"field_9"` + }{ + Inner: &Inner{Field: &fieldVal}, + FieldE: "test1", + Field1: "test1", + } + + errs = validate.Struct(test2) + NotEqual(t, errs, nil) + + ve := errs.(ValidationErrors) + Equal(t, len(ve), 1) + AssertError(t, errs, "Field5", "Field5", "Field5", "Field5", "skip_unless") + + defer func() { + if r := recover(); r == nil { + t.Errorf("test3 should have panicked!") + } + }() + + test3 := struct { + Inner *Inner + Field1 string `validate:"skip_unless=Inner.Field" json:"field_1"` + }{ + Inner: &Inner{Field: &fieldVal}, + } + _ = validate.Struct(test3) +} + func TestRequiredWith(t *testing.T) { type Inner struct { Field *string