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

Support boolean extra values #40

Merged
merged 1 commit into from
Aug 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/allow_additional_props.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/defaults_expanded_toplevel.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/ignore_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/no_reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/no_reference_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/required_from_jsontags.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/test_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions fixtures/test_user_assign_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
],
"hello": "world"
},
"bool_extra": {
"type": "string",
"isFalse": false,
"isTrue": true
},
"color": {
"type": "string",
"enum": [
Expand Down
12 changes: 11 additions & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,23 @@ func (t *Schema) setExtra(key, val string) {
t.Extras[key] = append(existingVal, val)
case int:
t.Extras[key], _ = strconv.Atoi(val)
case bool:
t.Extras[key] = (val == "true" || val == "t")
}
} else {
switch key {
case "minimum":
t.Extras[key], _ = strconv.Atoi(val)
default:
t.Extras[key] = val
var x interface{}
if val == "true" {
x = true
} else if val == "false" {
x = false
} else {
x = val
}
t.Extras[key] = x
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ type TestUser struct {
UUID string `json:"uuid" jsonschema:"format=uuid"`

// Test for "extras" support
Baz string `jsonschema_extras:"foo=bar,hello=world,foo=bar1"`
Baz string `jsonschema_extras:"foo=bar,hello=world,foo=bar1"`
BoolExtra string `json:"bool_extra,omitempty" jsonschema_extras:"isTrue=true,isFalse=false"`

// Tests for simple enum tags
Color string `json:"color" jsonschema:"enum=red,enum=green,enum=blue"`
Expand Down