Skip to content

Commit

Permalink
Merge pull request #40 from invopop/bool-extras
Browse files Browse the repository at this point in the history
Support boolean extra values
  • Loading branch information
samlown committed Aug 4, 2022
2 parents e8e691e + 50cc059 commit 4a807f9
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions fixtures/allow_additional_props.json
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
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
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
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
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
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
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
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
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
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

0 comments on commit 4a807f9

Please sign in to comment.