Yet another validator and formatter

This works in simple cases, but sometimes the validation of the value of each interface component is different form the validation of the whole user input, so on the server side I never validate the user input, I only validate the object generated with it. There are simple cases where the input validation is identical to the business logic, but for consistency, I like to always manage the business logic as “smart” business logic and the user input as “stupid” user input.

For example, if a form asks for width and height of a box, I usually set something like:

self.validator.greater_than(component=self.width, min_value=10)
self.validator.greater_than(component=self.height, min_value=10)

At this point the user can type any value greater than 10 for each input. This validation gives a quick feedback in case the user does something obviously wrong.

But then, every time the user changes a value, the value is passed to the object containing the business logic and the object will determine if that combination is valid or not.

For example a 20x20 box may be valid for candies, but not for cookies, or it may be valid for a type of shipment, but not for another, or the validation may be in the box calculated weight. If that is the case the app will decide what to do and maybe treat it as a generic invalid input or give some more constructive feedback.

This type of validation can’t really be applied to one input component, because it is not the width that is wrong, or the height or the type of shipment. It’s the combination of them all. Validating all the inputs at once could generate a Christmas tree of validation notifications that I really don’t like.

When it is possible to blame one specific component, I can do this:

self.validator.with_function(component=self.width, validating_function=object.validate_width)

But even in this case I don’t need a Validator class to tell me that I need to use the validation methods for the validation. It’s the other way: I’m giving the Validator the validation code written only once. And the validation code is where it belongs: inside the business logic manager, not inside the user interface.

When the form is ready to send data to the server, the object is serialized, sent to the server, deserialized, validated, etc.

The server side validation doesn’t care about the width being >= 10, it only cares that the box parameters are valid according to the business logic.

Summary: the client validates the user input, while the server validates a deserialized object.

1 Like