Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Recursive tag application for array fields #101

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions fixtures/array_handling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/array-handler",
"$ref": "#/$defs/ArrayHandler",
"$defs": {
"ArrayHandler": {
"properties": {
"min_len": {
"items": {
"type": "string",
"minLength": 2
},
"type": "array"
},
"min_val": {
"items": {
"type": "number",
"minimum": 2.5
},
"type": "array"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"min_len",
"min_val"
]
}
}
}
41 changes: 19 additions & 22 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,17 +773,6 @@ func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName str
Type: ty,
})
}
case "enum":
switch t.Type {
case "string":
t.Enum = append(t.Enum, val)
case "integer":
i, _ := strconv.Atoi(val)
t.Enum = append(t.Enum, i)
case "number":
f, _ := strconv.ParseFloat(val, 64)
t.Enum = append(t.Enum, f)
}
}
}
}
Expand Down Expand Up @@ -837,6 +826,8 @@ func (t *Schema) stringKeywords(tags []string) {
t.Default = val
case "example":
t.Examples = append(t.Examples, val)
case "enum":
t.Enum = append(t.Enum, val)
}
}
}
Expand Down Expand Up @@ -867,6 +858,10 @@ func (t *Schema) numericalKeywords(tags []string) {
if num, ok := toJSONNumber(val); ok {
t.Examples = append(t.Examples, num)
}
case "enum":
if num, ok := toJSONNumber(val); ok {
t.Enum = append(t.Enum, num)
}
}
}
}
Expand Down Expand Up @@ -906,17 +901,6 @@ func (t *Schema) arrayKeywords(tags []string) {
t.UniqueItems = true
case "default":
defaultValues = append(defaultValues, val)
case "enum":
switch t.Items.Type {
case "string":
t.Items.Enum = append(t.Items.Enum, val)
case "integer":
i, _ := strconv.Atoi(val)
t.Items.Enum = append(t.Items.Enum, i)
case "number":
f, _ := strconv.ParseFloat(val, 64)
t.Items.Enum = append(t.Items.Enum, f)
}
case "format":
t.Items.Format = val
case "pattern":
Expand All @@ -927,6 +911,19 @@ func (t *Schema) arrayKeywords(tags []string) {
if len(defaultValues) > 0 {
t.Default = defaultValues
}

switch t.Items.Type {
case "string":
t.Items.stringKeywords(tags)
case "number":
t.Items.numericalKeywords(tags)
case "integer":
t.Items.numericalKeywords(tags)
case "array":
// explicitly don't support traversal for the [][]..., as it's unclear where the array tags belong
case "boolean":
t.Items.booleanKeywords(tags)
}
}

func (t *Schema) extraKeywords(tags []string) {
Expand Down
12 changes: 12 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,15 @@ func TestNumberHandling(t *testing.T) {
fixtureContains(t, "fixtures/number_handling.json", `"default": 12`)
fixtureContains(t, "fixtures/number_handling.json", `"default": 12.5`)
}

func TestArrayHandling(t *testing.T) {
type ArrayHandler struct {
MinLen []string `json:"min_len" jsonschema:"minLength=2"`
MinVal []float64 `json:"min_val" jsonschema:"minimum=2.5"`
}

r := &Reflector{}
compareSchemaOutput(t, "fixtures/array_handling.json", r, &ArrayHandler{})
fixtureContains(t, "fixtures/array_handling.json", `"minLength": 2`)
fixtureContains(t, "fixtures/array_handling.json", `"minimum": 2.5`)
}
Loading