Skip to content

String length validation for []string #678

Answered by eest
eest asked this question in Q&A
Discussion options

You must be logged in to vote

To try to answer myself: I happened to find #513 and based on that came up with this:

func Ptr[T any](v T) *T {
       return &v
}

type userString string

func (us userString) Schema(_ huma.Registry) *huma.Schema {
       return &huma.Schema{
               Type:      "string",
               MinLength: Ptr(10),
               MaxLength: Ptr(63),
       }
}

input *struct {
  Body struct {
    Users []userString `json:"users" minItems:"1" maxItems:"5"`
  }
}

This seems to work as expected, so while I am still curious if this is the "right" way, at least I can make something work :).

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@danielgtaylor
Comment options

Answer selected by danielgtaylor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants