6
6
from fastapi import HTTPException
7
7
from fastui import components
8
8
from fastui .forms import FormFile , fastui_form
9
- from pydantic import BaseModel
9
+ from pydantic import BaseModel , Field
10
10
from starlette .datastructures import FormData , Headers , UploadFile
11
11
from typing_extensions import Annotated
12
12
13
13
14
14
class 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' )
17
17
18
18
19
19
class FakeRequest :
@@ -31,7 +31,7 @@ async def form(self):
31
31
32
32
def test_simple_form_fields ():
33
33
m = components .ModelForm [SimpleForm ](submit_url = '/foobar/' )
34
-
34
+ print ( m . model_dump ( by_alias = True , exclude_none = True ))
35
35
assert m .model_dump (by_alias = True , exclude_none = True ) == {
36
36
'submitUrl' : '/foobar/' ,
37
37
'method' : 'POST' ,
@@ -44,6 +44,9 @@ def test_simple_form_fields():
44
44
'locked' : False ,
45
45
'htmlType' : 'text' ,
46
46
'type' : 'FormFieldInput' ,
47
+ 'maxLength' : 10 ,
48
+ 'minLength' : 2 ,
49
+ 'description' : 'This field is required, it must have length 2-10' ,
47
50
},
48
51
{
49
52
'name' : 'size' ,
@@ -53,6 +56,10 @@ def test_simple_form_fields():
53
56
'locked' : False ,
54
57
'htmlType' : 'number' ,
55
58
'type' : 'FormFieldInput' ,
59
+ 'ge' : 0 ,
60
+ 'le' : 10 ,
61
+ 'multipleOf' : 2 ,
62
+ 'description' : 'size with range 0-10 and step with 2' ,
56
63
},
57
64
],
58
65
}
@@ -61,11 +68,11 @@ def test_simple_form_fields():
61
68
async def test_simple_form_submit ():
62
69
form_dep = fastui_form (SimpleForm )
63
70
64
- request = FakeRequest ([('name' , 'bar' ), ('size' , '123 ' )])
71
+ request = FakeRequest ([('name' , 'bar' ), ('size' , '2 ' )])
65
72
66
73
m = await form_dep .dependency (request )
67
74
assert isinstance (m , SimpleForm )
68
- assert m .model_dump () == {'name' : 'bar' , 'size' : 123 }
75
+ assert m .model_dump () == {'name' : 'bar' , 'size' : 2 }
69
76
70
77
71
78
async def test_simple_form_submit_repeat ():
0 commit comments