Skip to content

Commit

Permalink
fix scalar as list with multi selects (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
schatimo committed May 30, 2024
1 parent c61f2f7 commit fa88ab2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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


@pytest.fixture
Expand Down Expand Up @@ -74,4 +75,20 @@ def test_menu_links(client: TestClient, 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

0 comments on commit fa88ab2

Please sign in to comment.