66from fastapi import HTTPException
77from fastui import components
88from fastui .forms import FormFile , fastui_form
9- from pydantic import BaseModel
9+ from pydantic import BaseModel , Field
1010from starlette .datastructures import FormData , Headers , UploadFile
1111from typing_extensions import Annotated
1212
1313
1414class SimpleForm (BaseModel ):
15- name : str
16- size : int = 4
15+ name : str = Field (..., max_length = 10 , min_length = 2 , description = 'This field is required, it must have length 2-10' )
16+ size : int = Field ( 4 , ge = 0 , le = 10 , multiple_of = 2 , description = 'size with range 0-10 and step with 2' )
1717
1818
1919class FakeRequest :
@@ -31,7 +31,7 @@ async def form(self):
3131
3232def test_simple_form_fields ():
3333 m = components .ModelForm [SimpleForm ](submit_url = '/foobar/' )
34-
34+ print ( m . model_dump ( by_alias = True , exclude_none = True ))
3535 assert m .model_dump (by_alias = True , exclude_none = True ) == {
3636 'submitUrl' : '/foobar/' ,
3737 'method' : 'POST' ,
@@ -44,6 +44,9 @@ def test_simple_form_fields():
4444 'locked' : False ,
4545 'htmlType' : 'text' ,
4646 'type' : 'FormFieldInput' ,
47+ 'maxLength' : 10 ,
48+ 'minLength' : 2 ,
49+ 'description' : 'This field is required, it must have length 2-10' ,
4750 },
4851 {
4952 'name' : 'size' ,
@@ -53,6 +56,10 @@ def test_simple_form_fields():
5356 'locked' : False ,
5457 'htmlType' : 'number' ,
5558 'type' : 'FormFieldInput' ,
59+ 'ge' : 0 ,
60+ 'le' : 10 ,
61+ 'multipleOf' : 2 ,
62+ 'description' : 'size with range 0-10 and step with 2' ,
5663 },
5764 ],
5865 }
@@ -61,11 +68,11 @@ def test_simple_form_fields():
6168async def test_simple_form_submit ():
6269 form_dep = fastui_form (SimpleForm )
6370
64- request = FakeRequest ([('name' , 'bar' ), ('size' , '123 ' )])
71+ request = FakeRequest ([('name' , 'bar' ), ('size' , '2 ' )])
6572
6673 m = await form_dep .dependency (request )
6774 assert isinstance (m , SimpleForm )
68- assert m .model_dump () == {'name' : 'bar' , 'size' : 123 }
75+ assert m .model_dump () == {'name' : 'bar' , 'size' : 2 }
6976
7077
7178async def test_simple_form_submit_repeat ():
0 commit comments