Closed superstas closed 2 months ago
@superstas thanks for the question! This isn't currently possible. Go doesn't allow tags on slice elements like it does struct fields, however you can wrap the int
type and provide the schema as described in https://huma.rocks/features/schema-customization/#field-schema:
type MyItem int
func (i *MyItem) Schema(r huma.Registry) *huma.Schema {
min := 1.0
return &huma.Schema{Type: "integer", Format: "int64", Minimum: &min}
}
type DemoResponse struct {
Body struct {
IDs []MyItem `json:"ids" minItems:"2"`
}
}
Maybe in the future Huma should detect which validation applies to the array vs. the array's items.
Hey @danielgtaylor
Thank you for the reply.
We ended up with a similar workaround.
Maybe in the future Huma should detect which validation applies to the array vs. the array's items.
That would be great. Thank you!
Hi there!
I want to ask about validating primitive types nested in an array.
For example, I've got the following input:
Full code of the example: https://go.dev/play/p/hp1CsyyU6dt
Huma generates the following OAS spec for this type
Is there a way to specify validation rules by using struct tags to get the same validation rules for
ids
as forid
?Thank you.