Validation of step-through forms #15
jacob-a-brown
started this conversation in
Ideas
Replies: 1 comment
-
On second thought the database flush/rollback solution may not be feasible because the endpoint calls use different sessions. So the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Potential problem
Step through forms are elegant ways to have users submit data in an easy-to-understand, organized, and orderly fashion. At the end of the process, though, is when the data is actually submitted to the backend from the frontend and validated. This may cause issues because
It's worth nothing that the frontend does do some data validation, but it does not encompass all of the custom validators from the backend (in particular because those validations are not part of the OpenAPI JSON that the frontend uses for TypeScript).
Proposed solution
When data is submitted to POST endpoints it is immediately validated via Pydantic. If there are any issues with the data, Pydantic raises 422 errors. For general issues, such as data types, Pydantic has pre-built messages. Fields and models can be further validated with custom validators.
To ensure that only valid data is submitted at the end of the step through form, and to ensure that errors are found and displayed to the user in the appropriate section, we can have validate POST endpoints; they would do nothing except for validate the data. They could be named
/<endpoint>/validate
and would expect the same incoming data. If the data is valid the endpoint would return a 202 status code.Potential hazards
Some data is not validated until it gets submitted to the database, such as uniqueness and foreign key constraints. Since the proposed solutions do not necessarily validate against the database some errors may not be caught until the very end (which is the issue this proposed solution is trying to prevent).
Other
Database-level validations may be achieved by flushing data to the database via the
/<endpoint>/validate
and then rolling back the flushed operations before submitted the data to the actual POST endpoints. To achieve this would we have a POST/rollback
endpoint? Can this cause other issues, such as security problems?If we want to do this the data should remain flushed until the end so that foreign/primary keys are recognized and validated (and other database necessities/validations).
Alternative solution
Every POST endpoint could just flush the data, and then we can have a POST
/commit
endpoint that sends everything to the database. One advantage of this is that we can usetry/except
statements to rollback if something goes awry.Potential hazards
POST endpoints are meant to write data to the database, so this alternative solution would be breaking protocol.
Beta Was this translation helpful? Give feedback.
All reactions