fix: range validation on mapping (#2317)

master
Kevin Wan 2 years ago committed by GitHub
parent 1b477bbef9
commit a1466e1707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -281,6 +281,58 @@ func TestMarshal_RangeIllegal(t *testing.T) {
} }
} }
func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
tests := []struct {
name string
value interface{}
err error
}{
{
name: "left inclusive, right inclusive",
value: struct {
Int int `json:"int,range=[2:2]"`
}{
Int: 2,
},
},
{
name: "left inclusive, right exclusive",
value: struct {
Int int `json:"int,range=[2:2)"`
}{
Int: 2,
},
err: errNumberRange,
},
{
name: "left exclusive, right inclusive",
value: struct {
Int int `json:"int,range=(2:2]"`
}{
Int: 2,
},
err: errNumberRange,
},
{
name: "left exclusive, right exclusive",
value: struct {
Int int `json:"int,range=(2:2)"`
}{
Int: 2,
},
err: errNumberRange,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
_, err := Marshal(test.value)
assert.Equal(t, test.err, err)
})
}
}
func TestMarshal_FromString(t *testing.T) { func TestMarshal_FromString(t *testing.T) {
v := struct { v := struct {
Age int `json:"age,string"` Age int `json:"age,string"`

@ -311,10 +311,20 @@ func parseNumberRange(str string) (*numberRange, error) {
right = math.MaxFloat64 right = math.MaxFloat64
} }
if left >= right { if left > right {
return nil, errNumberRange return nil, errNumberRange
} }
// [2:2] valid
// [2:2) invalid
// (2:2] invalid
// (2:2) invalid
if left == right {
if !leftInclude || !rightInclude {
return nil, errNumberRange
}
}
return &numberRange{ return &numberRange{
left: left, left: left,
leftInclude: leftInclude, leftInclude: leftInclude,

Loading…
Cancel
Save