diff --git a/demo/forms.py b/demo/forms.py index c94a60db..392a5ca7 100644 --- a/demo/forms.py +++ b/demo/forms.py @@ -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)]): diff --git a/demo/tests.py b/demo/tests.py index a6c02161..2aafd563 100644 --- a/demo/tests.py +++ b/demo/tests.py @@ -5,6 +5,7 @@ from fastapi.testclient import TestClient from . import app +from .forms import ToolEnum client = TestClient(app) @@ -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