We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi there!
I want to ask about validating primitive types nested in an array.
For example, I've got the following input:
type Input struct { Body struct { ID int `json:"id" required:"true" minimum:"1" maximum:"10"` IDs []int `json:"ids" required:"true" minItems:"1" maxItems:"4"` } }
Full code of the example: https://go.dev/play/p/hp1CsyyU6dt
Huma generates the following OAS spec for this type
InputBody: additionalProperties: false properties: $schema: description: A URL to the JSON Schema for this object. examples: - https://example.com/schemas/InputBody.json format: uri readOnly: true type: string id: format: int64 maximum: 10 minimum: 1 type: integer ids: items: format: int64 type: integer maxItems: 4 minItems: 1 type: array required: - id - ids type: object
Is there a way to specify validation rules by using struct tags to get the same validation rules for ids as for id?
ids
id
... id: format: int64 maximum: 10 minimum: 1 type: integer ids: items: format: int64 type: integer maximum: 10 <-- Added minimum: 1 <-- Added maxItems: 4 minItems: 1 type: array ...
Thank you.
The text was updated successfully, but these errors were encountered:
@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:
int
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.
Sorry, something went wrong.
Hey @danielgtaylor
Thank you for the reply.
We ended up with a similar workaround.
That would be great. Thank you!
No branches or pull requests
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.
The text was updated successfully, but these errors were encountered: