Skip to content

Commit ce65758

Browse files
committed
Fix demo desc and add test for this
1 parent 2823c04 commit ce65758

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

demo/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class BigModel(BaseModel):
144144
None,
145145
max_length=10,
146146
min_length=2,
147-
description='This field is not required, it must start with a capital letter if provided',
147+
description='This field is not required, it must start with a capital letter if provided, and have length 2-10',
148148
)
149149
profile_pic: Annotated[UploadFile, FormFile(accept='image/*', max_size=16_000)] = Field(
150150
description='Upload a profile picture, must not be more than 16kb'
@@ -156,7 +156,7 @@ class BigModel(BaseModel):
156156
human: bool | None = Field(
157157
None, title='Is human', description='Are you human?', json_schema_extra={'mode': 'switch'}
158158
)
159-
number: int | None = Field(None, title='Number', ge=0, le=10, multiple_of=2, description='This is a number')
159+
number: int | None = Field(None, title='Number', ge=0, le=10, multiple_of=2, description='This is a number should be 0-10 and step with 2')
160160
size: SizeModel
161161

162162
@field_validator('name')

src/python-fastui/fastui/json_schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class JsonSchemaString(JsonSchemaBase):
4646
type: _ta.Required[_t.Literal['string']]
4747
default: str
4848
format: _t.Literal['date', 'date-time', 'time', 'email', 'uri', 'uuid', 'password']
49+
maxLength: int
50+
minLength: int
4951

5052

5153
class JsonSchemaStringEnum(JsonSchemaBase, total=False):
@@ -61,6 +63,8 @@ class JsonSchemaStringSearch(JsonSchemaBase, total=False):
6163
search_url: _ta.Required[str]
6264
placeholder: str
6365
initial: SelectOption
66+
maxLength: int
67+
minLength: int
6468

6569

6670
class JsonSchemaFile(JsonSchemaBase, total=False):

src/python-fastui/tests/test_forms.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
from fastapi import HTTPException
77
from fastui import components
88
from fastui.forms import FormFile, fastui_form
9-
from pydantic import BaseModel
9+
from pydantic import BaseModel, Field
1010
from starlette.datastructures import FormData, Headers, UploadFile
1111
from typing_extensions import Annotated
1212

1313

1414
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')
1717

1818

1919
class FakeRequest:
@@ -31,7 +31,7 @@ async def form(self):
3131

3232
def 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():
6168
async 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

7178
async def test_simple_form_submit_repeat():

0 commit comments

Comments
 (0)