Skip to content
New issue

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

Support tuples in forms #52

Merged
merged 10 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion python/demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def form_content(kind: FormKind):
case _:
raise ValueError(f'Invalid kind {kind!r}')


class LoginForm(BaseModel):
email: EmailStr = Field(title='Email Address', description="Try 'x@y' to trigger server side validation")
password: SecretStr
Expand Down Expand Up @@ -162,6 +161,11 @@ class BigModel(BaseModel):
dob: date = Field(title='Date of Birth', description='Your date of birth, this is required hence bold')
size: SizeModel

position: tuple[
Annotated[int, Field(description='X Coordinate')],
Annotated[int, Field(description='Y Coordinate')],
]

@field_validator('name')
def name_validator(cls, v: str | None) -> str:
if v and v[0].islower():
Expand Down
11 changes: 11 additions & 0 deletions python/fastui/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def unflatten(form_data: ds.FormData) -> NestedDict:
else:
d[last_key] = values

dicts = [result_dict]
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
while dicts:
d = dicts.pop()
for key, value in d.items():
if isinstance(value, dict):
if all(isinstance(k, int) for k in value.keys()):
list_value = [v for _, v in sorted(value.items())]
d[key] = list_value
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
else:
dicts.append(value)

return result_dict


Expand Down
8 changes: 8 additions & 0 deletions python/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def json_schema_array_to_fields(
items_schema[field_name] = value # type: ignore
if field := special_string_field(items_schema, loc_to_name(loc), title, required, True):
return [field]
elif 'minItems' in schema and schema.get('minItems') == schema.get('maxItems'):
if items := schema.get('prefixItems'):
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
for i, item in enumerate(items):
fields = list(json_schema_any_to_fields(item, loc + [i], title, required, defs))
if any((not f.required for f in fields)):
raise NotImplementedError('Optional `prefixItems` are not yet supported')
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
yield from fields
return
raise NotImplementedError('todo')


Expand Down