From 3fa8c5940dcfc4e931376c74bb3260f90e6054d2 Mon Sep 17 00:00:00 2001 From: taobig Date: Sun, 5 Jun 2022 11:51:43 +0800 Subject: [PATCH] fix: The validation of tag "options" is not working with int/uint type (#1969) --- core/mapping/unmarshaler.go | 4 ++++ core/mapping/unmarshaler_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index a633453a..7b6df276 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -253,6 +253,10 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(field reflect.StructFi return err } + if err := validateValueInOptions(opts.options(), v); err != nil { + return err + } + if fieldKind == reflect.Ptr { value = value.Elem() } diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index da042490..521e574f 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -1170,6 +1170,20 @@ func TestUnmarshalWithIntOptionsIncorrect(t *testing.T) { assert.NotNil(t, UnmarshalKey(m, &in)) } +func TestUnmarshalWithJsonNumberOptionsIncorrect(t *testing.T) { + type inner struct { + Value string `key:"value,options=first|second"` + Incorrect int `key:"incorrect,options=1|2"` + } + m := map[string]interface{}{ + "value": "first", + "incorrect": json.Number("3"), + } + + var in inner + assert.NotNil(t, UnmarshalKey(m, &in)) +} + func TestUnmarshalWithUintOptionsCorrect(t *testing.T) { type inner struct { Value string `key:"value,options=first|second"`