Skip to content

Commit

Permalink
test: add test for min/max properties validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Feb 4, 2022
1 parent 777ddb6 commit 308fe69
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
40 changes: 39 additions & 1 deletion _testdata/sample.json
Expand Up @@ -652,6 +652,15 @@
"minLength": 1
}
},
"ValidationStringMap": {
"type": "object",
"additionalProperties": {
"type": "string",
"minLength": 1
},
"minProperties": 1,
"maxProperties": 4
},
"RecursiveMap": {
"type": "object",
"additionalProperties": {
Expand Down Expand Up @@ -727,11 +736,37 @@
"additionalProperties": {
"type": "string"
}
},
"map_validation": {
"$ref": "#/components/schemas/ValidationStringMap"
}
},
"additionalProperties": {
"type": "string"
}
},
"maxProperties": 7
},
"MaxPropertiesTest" : {
"type": "object",
"required": [
"required"
],
"properties": {
"required": {
"type": "integer"
},
"optional_a": {
"type": "integer"
},
"optional_b": {
"type": "integer"
},
"optional_c": {
"type": "integer"
}
},
"minProperties": 2,
"maxProperties": 2
},
"Data": {
"type": "object",
Expand Down Expand Up @@ -930,6 +965,9 @@
"testAnyOf": {
"$ref": "#/components/schemas/AnyOfTest"
},
"testMaxProperties": {
"$ref": "#/components/schemas/MaxPropertiesTest"
},
"testDate": {
"type": "string",
"format": "date"
Expand Down
107 changes: 107 additions & 0 deletions json_test.go
Expand Up @@ -320,12 +320,77 @@ func TestJSONAdditionalProperties(t *testing.T) {
},
false,
},
{
// ValidationStringMap expects maximum 4 property.
`{"required": 1, "map_validation": {"1": "1", "2": "2"}}`,
api.MapWithProperties{
Required: 1,
AdditionalProps: map[string]string{},
MapValidation: api.NewOptValidationStringMap(api.ValidationStringMap{
"1": "1",
"2": "2",
}),
},
false,
},
{
// ValidationStringMap expects maximum 4 property.
`{"required": 1, "map_validation": {"1": "1", "2": "2", "3": "3", "4": "4"}}`,
api.MapWithProperties{
Required: 1,
AdditionalProps: map[string]string{},
MapValidation: api.NewOptValidationStringMap(api.ValidationStringMap{
"1": "1",
"2": "2",
"3": "3",
"4": "4",
}),
},
false,
},
{
// MapWithProperties expects string for `runtime_field`.
`{"required": 1, "runtime_field": 10}`,
api.MapWithProperties{},
true,
},
{
// MapWithProperties expects maximum 7 properties.
`{"required": 1, "2": "2", "3": "3", "4": "4", "5": "5", "6": "6", "7": "7", "8":"8"}`,
api.MapWithProperties{},
true,
},
{
// MapWithProperties expects maximum 7 properties.
`{
"required":1,
"inlined_sub_map":{
"runtime_field":"field"
},
"sub_map":{
"runtime_field":"field"
},
"4":"4",
"5":"5",
"6":"6",
"7":"7",
"8":"8"
}`,
api.MapWithProperties{},
true,
},
{
// ValidationStringMap expects minimum 1 property.
`{"required": 1, "map_validation": {}}`,
api.MapWithProperties{},
true,
},
{
// ValidationStringMap expects maximum 4 property.
`{"required": 1, "map_validation": {"1": "1", "2": "2", "3": "3", "4": "4", "5": "5"}}`,
api.MapWithProperties{},
true,
},
} {
// Make range value copy to prevent data races.
tc := tc
Expand Down Expand Up @@ -430,6 +495,48 @@ func TestJSONNullableEnum(t *testing.T) {
}
}

func TestJSONPropertiesCount(t *testing.T) {
for i, tc := range []struct {
Input string
Error bool
}{
{
`{"required": 1, "optional_a": 1}`,
false,
},
{
`{"required": 1, "optional_b": 1}`,
false,
},
{
`{}`,
true,
},
{
`{"required": 1}`,
true,
},
{
`{"optional_a": 1, "optional_b": 1}`,
true,
},
{
`{"required": 1, "optional_a": 1, "optional_b": 1}`,
true,
},
} {
tc := tc
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
m := api.MaxPropertiesTest{}
checker := require.NoError
if tc.Error {
checker = require.Error
}
checker(t, m.Decode(jx.DecodeStr(tc.Input)))
})
}
}

func TestJSONSum(t *testing.T) {
t.Run("Issue143", func(t *testing.T) {
for i, tc := range []struct {
Expand Down

0 comments on commit 308fe69

Please sign in to comment.