Validation is Golden Rule

In this article I’ll explain why you should always validate inputs and never assume anything.
Please consider that we need to create a database table to store contacts. The requirements say that we only need to record Name, Email Address and Phone Number, but only the Name is required.
Based on the requirements we ended by creating the following database entity:

Now, considering what we’ve learned in the last article, we might write the following action to Create or Update a Contact:

As you can see, we’re not validating anything and we’re assuming that the record can be correctly saved in the database.
Now consider that while using last action, we forgot to set the Name field (which is mandatory in database)

I wrote “we forgot to set Name” but other possible reasons are:
– A missing input validation that validates if the user sets that field;
– Some other action that ended by using ‘Contact_CreateOrUpdate’ in another context and missed that field;
Not doing any kind of validation, we’re assuming that Name field will be always filled in.
You might think that by setting Is Mandatory = true in the entity definition, this would enforce that validation when saving the record to the database?
If so, you’re wrong:

Entity attribute decoration Is Mandatory is only used to do form validation when using built in validations and to decorate field labels with mandatory (*).
So, what should you do then?
You need to validate inputs before doing anything. In this specific case you need validate that field Name contains any value before continuing the action flow:

Please note that this is a single simple example where we use a single text field that we identified that it is a mandatory field (by requirement). You could have more complex scenarios where multiple validation must be enforced.
Take special attention about Is Mandatory attribute decoration, as this information is also valid for numeric, date and boolean fields.
However, by setting Is Mandatory = true in fields that are Foreing Keys to other entities, database validation will occur as data integrity must be maintained.
Better validate twice than don’t validate whatsoever
Tiago