Skip to content

Commit

Permalink
move to *json.Number
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Sep 25, 2023
1 parent c7f3864 commit b0206aa
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 34 deletions.
4 changes: 2 additions & 2 deletions fixtures/allow_additional_props.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/custom_base_schema_id.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/defaults_expanded_toplevel.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/ignore_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/no_reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/no_reference_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/required_from_jsontags.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/test_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/test_user_assign_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
43 changes: 29 additions & 14 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ type Schema struct {
Type string `json:"type,omitempty"` // section 6.1.1
Enum []interface{} `json:"enum,omitempty"` // section 6.1.2
Const interface{} `json:"const,omitempty"` // section 6.1.3
MultipleOf json.Number `json:"multipleOf,omitempty"` // section 6.2.1
Maximum json.Number `json:"maximum,omitempty"` // section 6.2.2
ExclusiveMaximum json.Number `json:"exclusiveMaximum,omitempty"` // section 6.2.3
Minimum json.Number `json:"minimum,omitempty"` // section 6.2.4
ExclusiveMinimum json.Number `json:"exclusiveMinimum,omitempty"` // section 6.2.5
MultipleOf *json.Number `json:"multipleOf,omitempty"` // section 6.2.1
Maximum *json.Number `json:"maximum,omitempty"` // section 6.2.2
ExclusiveMaximum *json.Number `json:"exclusiveMaximum,omitempty"` // section 6.2.3
Minimum *json.Number `json:"minimum,omitempty"` // section 6.2.4
ExclusiveMinimum *json.Number `json:"exclusiveMinimum,omitempty"` // section 6.2.5
MaxLength int `json:"maxLength,omitempty"` // section 6.3.1
MinLength int `json:"minLength,omitempty"` // section 6.3.2
Pattern string `json:"pattern,omitempty"` // section 6.3.3
Expand Down Expand Up @@ -850,21 +850,23 @@ func (t *Schema) numericalKeywords(tags []string) {
name, val := nameValue[0], nameValue[1]
switch name {
case "multipleOf":
t.MultipleOf = json.Number(val)
t.MultipleOf = toJSONNumber(val)
case "minimum":
t.Minimum = json.Number(val)
t.Minimum = toJSONNumber(val)
case "maximum":
t.Maximum = json.Number(val)
t.Maximum = toJSONNumber(val)
case "exclusiveMaximum":
t.ExclusiveMaximum = json.Number(val)
t.ExclusiveMaximum = toJSONNumber(val)
case "exclusiveMinimum":
t.ExclusiveMinimum = json.Number(val)
t.ExclusiveMinimum = toJSONNumber(val)
case "default":
n, _ := strconv.ParseFloat(val, 64)
t.Default = n
if num := toJSONNumber(val); num != nil {
// toJSONNumber returns typed nil, so we need to account for that
t.Default = num
}
case "example":
if i, err := strconv.Atoi(val); err == nil {
t.Examples = append(t.Examples, i)
if num := toJSONNumber(val); num != nil {
t.Examples = append(t.Examples, num)
}
}
}
Expand Down Expand Up @@ -1015,6 +1017,19 @@ func ignoredByJSONSchemaTags(tags []string) bool {
return tags[0] == "-"
}

// toJSONNumber converts string to *json.Number
// it'll return nil if the number is invalid
func toJSONNumber(s string) *json.Number {
num := json.Number(s)
if _, err := num.Int64(); err == nil {
return &num
}
if _, err := num.Float64(); err == nil {
return &num
}
return nil
}

func (r *Reflector) fieldNameTag() string {
if r.FieldNameTag != "" {
return r.FieldNameTag
Expand Down
4 changes: 2 additions & 2 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type TestUser struct {
nonExported
MapType

ID int `json:"id" jsonschema:"required"`
ID int `json:"id" jsonschema:"required,minimum=bad,maximum=bad,exclusiveMinimum=bad,exclusiveMaximum=bad,default=bad"`
Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex,readOnly=true"`
Password string `json:"password" jsonschema:"writeOnly=true"`
Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"`
Expand All @@ -88,7 +88,7 @@ type TestUser struct {
// Tests for jsonpb enum support
Feeling ProtoEnum `json:"feeling,omitempty"`

Age int `json:"age" jsonschema:"minimum=18,maximum=120,exclusiveMaximum=true,exclusiveMinimum=true"`
Age int `json:"age" jsonschema:"minimum=18,maximum=120,exclusiveMaximum=121,exclusiveMinimum=17"`
Email string `json:"email" jsonschema:"format=email"`
UUID string `json:"uuid" jsonschema:"format=uuid"`

Expand Down

0 comments on commit b0206aa

Please sign in to comment.