Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bool form fields optional. #323

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ class BigModel(BaseModel):
None, description='Upload multiple images'
)
dob: date = Field(title='Date of Birth', description='Your date of birth, this is required hence bold')
human: bool | None = Field(
None, title='Is human', description='Are you human?', json_schema_extra={'mode': 'switch'}
)
human: bool = Field(title='Is human', description='Are you human?', json_schema_extra={'mode': 'switch'})
size: SizeModel

position: tuple[
Expand Down
3 changes: 2 additions & 1 deletion src/python-fastui/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def json_schema_obj_to_fields(
required = set(schema.get('required', []))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead make bool fields not required by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand, because this is what PR already implements: bool fields are not required. Maybe you mean with "not required by default" that there should be a possibility to make bool field required if necessary? Or set bool field to non required somewhere else, e.g. here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sergue1,

Yeah my apologies, my feedback was a bit confusing. Let me dig around this weekend and see if I can better understand how we might be able to fix this in a slightly different way!

Great work so far!

if properties := schema.get('properties'):
for key, value in properties.items():
yield from json_schema_any_to_fields(value, loc + [key], title, key in required, defs)
is_bool = value.get('type') == 'boolean'
yield from json_schema_any_to_fields(value, loc + [key], title, key in required and not is_bool, defs)


def json_schema_any_to_fields(
Expand Down
34 changes: 34 additions & 0 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,37 @@ def test_form_description_leakage():
'submitUrl': '/foobar/',
'type': 'ModelForm',
}


class FormFieldsBool(BaseModel):
human: bool = Field(title='Is human', description='Are you human?', json_schema_extra={'mode': 'switch'})
is_foo: bool


def test_form_description_leakage():
m = components.ModelForm(model=FormFieldsBool, submit_url='/foobar/')

assert m.model_dump(by_alias=True, exclude_none=True) == {
'formFields': [
{
'description': 'Are you human?',
'locked': False,
'mode': 'switch',
'name': 'human',
'required': False,
'title': ['Is human'],
'type': 'FormFieldBoolean',
},
{
'locked': False,
'mode': 'checkbox',
'name': 'is_foo',
'required': False,
'title': ['Is Foo'],
'type': 'FormFieldBoolean',
},
],
'method': 'POST',
'submitUrl': '/foobar/',
'type': 'ModelForm',
}
Loading