Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidfm committed Aug 4, 2022
1 parent adae5d2 commit f98e7ac
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
103 changes: 103 additions & 0 deletions fixtures/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/examples",
"$ref": "#/$defs/Examples",
"$defs": {
"Examples": {
"properties": {
"string_example": {
"type": "string",
"examples": [
"hi",
"test"
]
},
"int_example": {
"type": "integer",
"examples": [
1,
10,
42
]
},
"float_example": {
"type": "number",
"examples": [
2,
3.14,
13.37
]
},
"int_array_example": {
"items": {
"type": "integer"
},
"type": "array",
"examples": [
[
1,
2
],
[
3,
4
],
[
5,
6
]
]
},
"map_example": {
"patternProperties": {
".*": {
"type": "string"
}
},
"type": "object",
"examples": [
{
"key": "value"
}
]
},
"map_array_example": {
"items": {
"patternProperties": {
".*": {
"type": "string"
}
},
"type": "object"
},
"type": "array",
"examples": [
[
{
"a": "b"
},
{
"c": "d"
}
],
[
{
"hello": "test"
}
]
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"string_example",
"int_example",
"float_example",
"int_array_example",
"map_example",
"map_array_example"
]
}
}
}
10 changes: 10 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ type KeyNamed struct {
RenamedByComputation int `jsonschema_description:"Description was preserved"`
}

type Examples struct {
StringExample string `json:"string_example" jsonschema:"example=hi,example=test"`
IntExample int `json:"int_example" jsonschema:"example=1,example=10,example=42"`
FloatExample float64 `json:"float_example" jsonschema:"example=2.0,example=3.14,example=13.37"`
IntArrayExample []int `json:"int_array_example" jsonschema:"example=1;2,example=3;4,example=5;6"`
MapExample map[string]string `json:"map_example" jsonschema:"example={\"key\": \"value\"}"`
MapArrayExample []map[string]string `json:"map_array_example" jsonschema:"example={\"a\": \"b\"};{\"c\": \"d\"},example={\"hello\": \"test\"}"`
}

func TestReflector(t *testing.T) {
r := new(Reflector)
s := "http://example.com/schema"
Expand Down Expand Up @@ -417,6 +426,7 @@ func TestSchemaGeneration(t *testing.T) {
}, "fixtures/keynamed.json"},
{MapType{}, &Reflector{}, "fixtures/map_type.json"},
{ArrayType{}, &Reflector{}, "fixtures/array_type.json"},
{Examples{}, &Reflector{}, "fixtures/examples.json"},
}

for _, tt := range tests {
Expand Down

0 comments on commit f98e7ac

Please sign in to comment.