-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Ui/Model: Add field validation / format methods #210
Comments
Working on using Code example - note the pattern try:
model_instance = Model.validate(
dict(
spam="Spam",
eggs="Eggs",
)
)
except ValidationError:
... class TestContact:
def test_valid_instantiation(self):
contact = Contact.validate(
dict(
first_name="Sam",
last_name="Lowry",
email="sam.lowry@miniinf.gov",
company="Ministry of Information",
)
)
assert store_and_retrieve(contact)
def test_invalid_email(self):
with pytest.raises(ValidationError):
Contact.validate(
dict(
first_name="Sam",
last_name="Lowry",
email="27B-",
company="Ministry of Information",
)
) |
@vlad-ed-git Tell me whether I am right about this: In order to be serious about decoupling view and data model, view code should not be concerned with data validation. More specifically, there should be nothing in the view code that duplicates any of the definitions in
I believe there is currently some code in the view that tries to do data validation, e.g. if not self.contact.first_name or not self.contact.last_name:
self.on_error_callback("First and last name cannot be empty")
return Goal: Remove all code of this kind from the view. Let Check out pydantic validators. |
@clstaudt |
@clstaudt One more thing, consider a required field |
@clstaudt By the way, there was an issue in which you mentioned something about having a Ui form that is generated from the model. This is something that Django does. I do not know if there is a library for it (and even if there is, I doubt it will convert python to flet's flutter) but we could implement this ourselves in the future. |
We build a desktop app with no mobile version planned. Seems like there's no need to spend time on the keyboards.
I bet ChatGPT does well in generating form code from model code, if it is prompted with good examples. |
@vlad-ed-git How about the view inspecting the from tabulate import tabulate
def print_validation_errors(ve: ValidationError):
errors = ve.errors()
table = []
for error in errors:
field_name = error.get('loc')[0]
error_message = error.get('msg')
table.append([field_name, error_message])
print(tabulate(table, headers=["Field Name", "Error Message"]))
try:
client = Client.validate(dict(name="Ministry of Information"))
except ValidationError as ve:
print_validation_errors(ve) |
@clstaudt I am not sure about the rest of that code, but if the validation error is such that this part works, then great: errors = ve.errors()
table = []
for error in errors:
field_name = error.get('loc')[0]
error_message = error.get('msg') |
The rest of the code is just for example output. This is working: def test_missing_name(self):
"""Test that a ValidationError is raised when the name is missing."""
with pytest.raises(ValidationError):
Client.validate(dict())
try:
client = Client.validate(dict())
except ValidationError as ve:
for error in ve.errors():
field_name = error.get("loc")[0]
error_message = error.get("msg")
assert field_name == "name" |
@vlad-ed-git Can we review this together? |
Fields, such as Vat Rate under Contract, should have validation (and formatting where necessary) mechanisms to validate and format the user input according to the expected standard. E.g. keep decimals fixed, allow only numbers, etc
The text was updated successfully, but these errors were encountered: