Skip to content

Commit

Permalink
Merge pull request #105 from cloudquery/fix/zero-max-min-length
Browse files Browse the repository at this point in the history
fix: Unsigned int handling (0 is a valid value)
  • Loading branch information
samlown authored Oct 2, 2023
2 parents 2bf120d + d109806 commit ab48bdb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 deletions.
47 changes: 47 additions & 0 deletions fixtures/unsigned_int_handling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/unsigned-int-handler",
"$ref": "#/$defs/UnsignedIntHandler",
"$defs": {
"UnsignedIntHandler": {
"properties": {
"min_len": {
"items": {
"type": "string",
"minLength": 0
},
"type": "array"
},
"max_len": {
"items": {
"type": "string",
"maxLength": 0
},
"type": "array"
},
"min_items": {
"items": {
"type": "string"
},
"type": "array",
"minItems": 0
},
"max_items": {
"items": {
"type": "string"
},
"type": "array",
"maxItems": 0
}
},
"additionalProperties": false,
"type": "object",
"required": [
"min_len",
"max_len",
"min_items",
"max_items"
]
}
}
}
41 changes: 23 additions & 18 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ type Schema struct {
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
MaxLength *uint64 `json:"maxLength,omitempty"` // section 6.3.1
MinLength *uint64 `json:"minLength,omitempty"` // section 6.3.2
Pattern string `json:"pattern,omitempty"` // section 6.3.3
MaxItems int `json:"maxItems,omitempty"` // section 6.4.1
MinItems int `json:"minItems,omitempty"` // section 6.4.2
MaxItems *uint64 `json:"maxItems,omitempty"` // section 6.4.1
MinItems *uint64 `json:"minItems,omitempty"` // section 6.4.2
UniqueItems bool `json:"uniqueItems,omitempty"` // section 6.4.3
MaxContains uint `json:"maxContains,omitempty"` // section 6.4.4
MinContains uint `json:"minContains,omitempty"` // section 6.4.5
MaxProperties int `json:"maxProperties,omitempty"` // section 6.5.1
MinProperties int `json:"minProperties,omitempty"` // section 6.5.2
MaxContains *uint64 `json:"maxContains,omitempty"` // section 6.4.4
MinContains *uint64 `json:"minContains,omitempty"` // section 6.4.5
MaxProperties *uint64 `json:"maxProperties,omitempty"` // section 6.5.1
MinProperties *uint64 `json:"minProperties,omitempty"` // section 6.5.2
Required []string `json:"required,omitempty"` // section 6.5.3
DependentRequired map[string][]string `json:"dependentRequired,omitempty"` // section 6.5.4
// RFC draft-bhutton-json-schema-validation-00, section 7
Expand Down Expand Up @@ -459,8 +459,9 @@ func (r *Reflector) reflectSliceOrArray(definitions Definitions, t reflect.Type,
}

if t.Kind() == reflect.Array {
st.MinItems = t.Len()
st.MaxItems = st.MinItems
l := uint64(t.Len())
st.MinItems = &l
st.MaxItems = &l
}
if t.Kind() == reflect.Slice && t.Elem() == byteSliceType.Elem() {
st.Type = "string"
Expand Down Expand Up @@ -808,11 +809,9 @@ func (t *Schema) stringKeywords(tags []string) {
name, val := nameValue[0], nameValue[1]
switch name {
case "minLength":
i, _ := strconv.Atoi(val)
t.MinLength = i
t.MinLength = parseUint(val)
case "maxLength":
i, _ := strconv.Atoi(val)
t.MaxLength = i
t.MaxLength = parseUint(val)
case "pattern":
t.Pattern = val
case "format":
Expand Down Expand Up @@ -898,11 +897,9 @@ func (t *Schema) arrayKeywords(tags []string) {
name, val := nameValue[0], nameValue[1]
switch name {
case "minItems":
i, _ := strconv.Atoi(val)
t.MinItems = i
t.MinItems = parseUint(val)
case "maxItems":
i, _ := strconv.Atoi(val)
t.MaxItems = i
t.MaxItems = parseUint(val)
case "uniqueItems":
t.UniqueItems = true
case "default":
Expand Down Expand Up @@ -1039,6 +1036,14 @@ func toJSONNumber(s string) (json.Number, bool) {
return json.Number(""), false
}

func parseUint(num string) *uint64 {
val, err := strconv.ParseUint(num, 10, 64)
if err != nil {
return nil
}
return &val
}

func (r *Reflector) fieldNameTag() string {
if r.FieldNameTag != "" {
return r.FieldNameTag
Expand Down
16 changes: 16 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,19 @@ func TestArrayHandling(t *testing.T) {
fixtureContains(t, "fixtures/array_handling.json", `"minLength": 2`)
fixtureContains(t, "fixtures/array_handling.json", `"minimum": 2.5`)
}

func TestUnsignedIntHandling(t *testing.T) {
type UnsignedIntHandler struct {
MinLen []string `json:"min_len" jsonschema:"minLength=0"`
MaxLen []string `json:"max_len" jsonschema:"maxLength=0"`
MinItems []string `json:"min_items" jsonschema:"minItems=0"`
MaxItems []string `json:"max_items" jsonschema:"maxItems=0"`
}

r := &Reflector{}
compareSchemaOutput(t, "fixtures/unsigned_int_handling.json", r, &UnsignedIntHandler{})
fixtureContains(t, "fixtures/unsigned_int_handling.json", `"minLength": 0`)
fixtureContains(t, "fixtures/unsigned_int_handling.json", `"maxLength": 0`)
fixtureContains(t, "fixtures/unsigned_int_handling.json", `"minItems": 0`)
fixtureContains(t, "fixtures/unsigned_int_handling.json", `"maxItems": 0`)
}

0 comments on commit ab48bdb

Please sign in to comment.