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

fix scalar as list with multi selects #141

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ class SelectForm(BaseModel):
search_select_single: str = Field(json_schema_extra={'search_url': '/api/forms/search'})
search_select_multiple: list[str] = Field(json_schema_extra={'search_url': '/api/forms/search'})

@field_validator('select_multiple', 'search_select_multiple', mode='before')
@classmethod
def correct_select_multiple(cls, v: list[str]) -> list[str]:
if isinstance(v, list):
return v
else:
return [v]


@router.post('/select', response_model=FastUI, response_model_exclude_none=True)
async def select_form_post(form: Annotated[SelectForm, fastui_form(SelectForm)]):
Expand Down
17 changes: 17 additions & 0 deletions demo/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fastapi.testclient import TestClient

from . import app
from .forms import ToolEnum

client = TestClient(app)

Expand Down Expand Up @@ -68,4 +69,20 @@ def test_menu_links(url: str):
assert isinstance(data, list)


def test_forms_validate_correct_select_multiple():
with client as _client:
countries = _client.get('api/forms/search', params={'q': None})
countries_options = countries.json()['options']
r = client.post(
'api/forms/select',
data={
'select_single': ToolEnum._member_names_[0],
'select_multiple': ToolEnum._member_names_[0],
'search_select_single': countries_options[0]['options'][0]['value'],
'search_select_multiple': countries_options[0]['options'][0]['value'],
},
)
assert r.status_code == 200


# TODO tests for forms, including submission