I'm actually wondering what are the pros and cons of these two ways to handle client side validations / verifications.
Let's imagine an application where there is a contact form that needs these information:
- An email address (with a specific format)
- A postal address (with a specific postal code format)
I have in mind two ways to handle these format verifications:
At the form level
The "usual" way to handle this: we put some verifications based on user inputs and validate / invalidate the information.
Concretely, on the change
(or submit
, anyway) event of the input
we verify the field value with some regexp and modify the current component state to display an error accordingly.
The "backend DDD" way
The idea is to rely on the following statement:
When creating an object, it should always be in a good and consistent shape so that you don't have to tweak for specific computations everywhere in the app
The idea is to enforce the verifications using schemas and to always rely on consistent objects that we don't have to tweak in multiple places like modifying one of its internal attributes by hand (implying multiple sanity verifications etc...).
In a concrete world, I would simply rely on a Email.create(rawInputValue)
that may throw an error concerning a bad format exception or something like this. Email
would be a class definition that owns information on how to build a valid email address.
After this bit of context, I'm wondering what are the pros / cons of these system? Which one do you use and why?