Skip to content

Commit

Permalink
Merge branch 'main' into fix/missed-description-with-ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor authored Jul 25, 2024
2 parents 8065a91 + a3311d5 commit 5d90e4f
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 4 deletions.
19 changes: 15 additions & 4 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,24 @@ func SchemaFromField(registry Registry, f reflect.StructField, hint string) *Sch
}
}

fs.Minimum = floatTag(f, "minimum")
if _, ok := f.Tag.Lookup("minimum"); ok {
fs.Minimum = floatTag(f, "minimum")
}

fs.ExclusiveMinimum = floatTag(f, "exclusiveMinimum")
fs.Maximum = floatTag(f, "maximum")

if _, ok := f.Tag.Lookup("maximum"); ok {
fs.Maximum = floatTag(f, "maximum")
}
fs.ExclusiveMaximum = floatTag(f, "exclusiveMaximum")
fs.MultipleOf = floatTag(f, "multipleOf")
fs.MinLength = intTag(f, "minLength")
fs.MaxLength = intTag(f, "maxLength")
if _, ok := f.Tag.Lookup("minLength"); ok {
fs.MinLength = intTag(f, "minLength")
}

if _, ok := f.Tag.Lookup("maxLength"); ok {
fs.MaxLength = intTag(f, "maxLength")
}
fs.Pattern = f.Tag.Get("pattern")
fs.PatternDescription = f.Tag.Get("patternDescription")
if _, ok := f.Tag.Lookup("minItems"); ok {
Expand Down
166 changes: 166 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ type CustomSchemaPtr struct {

func (c *CustomSchemaPtr) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema {
s.Description = "custom description"
return s
}

type TypedStringWithCustomLength string

func (c TypedStringWithCustomLength) Schema(r huma.Registry) *huma.Schema {
return &huma.Schema{
Type: "string",
MinLength: Ptr(1),
MaxLength: Ptr(10),
}
}

type TypedIntegerWithCustomLimits int

func (c *TypedIntegerWithCustomLimits) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema {
s.Minimum = Ptr(float64(1))
s.Maximum = Ptr(float64(10))
return s
}

Expand Down Expand Up @@ -857,6 +875,154 @@ func TestSchema(t *testing.T) {
}{},
panics: `nullable is not supported for field 'Value' which is type '#/components/schemas/ValueStruct'`,
},
{
name: "field-custom-length-string-in-slice",
input: struct {
Values []TypedStringWithCustomLength `json:"values"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"values":{
"type":"array",
"items":{
"type":"string",
"minLength":1,
"maxLength":10
}
}
},
"required":["values"],
"type":"object"
}`,
},
{
name: "field-custom-length-string",
input: struct {
Value TypedStringWithCustomLength `json:"value"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"type":"string",
"minLength":1,
"maxLength":10
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-custom-length-string-with-tag",
input: struct {
Value TypedStringWithCustomLength `json:"value" maxLength:"20"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"type":"string",
"minLength":1,
"maxLength":20
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-ptr-to-custom-length-string",
input: struct {
Value *TypedStringWithCustomLength `json:"value"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"type":"string",
"minLength":1,
"maxLength":10
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-ptr-to-custom-length-string-with-tag",
input: struct {
Value *TypedStringWithCustomLength `json:"value" minLength:"0"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"type":"string",
"minLength":0,
"maxLength":10
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-custom-limits-int",
input: struct {
Value TypedIntegerWithCustomLimits `json:"value"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"type":"integer",
"format":"int64",
"minimum":1,
"maximum":10
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-custom-limits-int-with-tag",
input: struct {
Value TypedIntegerWithCustomLimits `json:"value" minimum:"2"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"type":"integer",
"format":"int64",
"minimum":2,
"maximum":10
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-ptr-to-custom-limits-int",
input: struct {
Value *TypedIntegerWithCustomLimits `json:"value"`
}{},
expected: ` {
"additionalProperties":false,
"properties":{
"value":{
"format":"int64",
"type": ["integer", "null"]
}
},
"required":["value"],
"type":"object"
}`,
},
{
name: "field-custom-array",
input: struct {
Expand Down

0 comments on commit 5d90e4f

Please sign in to comment.