Replies: 1 comment
-
|
I see you are using BaseModel, so my approach would be something like this: models: Annotated[str | list, AfterValidator(lambda x: x if isinstance(x, list) else [x])] A custom attribute like "data-strict-list" on an HTML element and a small extension would also work. I’d use the AfterValidator. :) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
A common use-case I have with htmx is to use the json-enc extension to translate forms to JSON, so I can hit a backend that expects JSON payloads (and then emits HTML). Backends are typically FastAPI with Pydantic payload parsing.
An issue I run into is where an endpoint expects a list of values in JSON, but validating that with
list[str]in Pydantic fails when only a single form field is passed. A trendy example:This works fine if multiple checkboxes are checked, the parameters in
htmx:requestConfigwill be{models: ['gpt-4.5', 'claude-3-opus-20240229']}. If one checkbox is checked, you'll get{models: 'gpt-4.5'}.I understand this is entirely a HTML forms thing. My workarounds for this are one of:
fieldname[], and tweak json-enc to convert those to lists:listtype in Pydantic that coerces scalars to a list of that scalar.All this to avoid workarounds for x-www-urlencoded and multipart. Does anyone have any ideas or better ways of doing this?
I was thinking of converting my middleware to a htmx extension if that would be useful to anyone - if anyone knows of a JS package that does similar hydration that would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions