Validations in Flask db
Published: 2023-10-08 22:28:28

My journey in my full stack has been a difficult one. I have learned a lot these past few months that have only helped me develop my skills to becoming an amazing full stack developer. When i first started i only had an understanding of the basics such as loops and variables, now have been able to my own back-end and front-end. 
 **Back-end**
The back-end of full stack is not a very visual part of the project but is  very important one from what i have come to learn. I learned how to develop the back-end with python and using alembic library, more specifically flask db. These are tools provided when using SQLAlchemy. 
When creating the classes, that become the tables for the database, we need to make sure that everything we allow to be a column must be the correct information.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ay06fszwz5m5bq686ve.png)

Some applications can get away with the minimal amount validations, but sometimes it is important that the information is the correct one. 
**Validations**
When information is passed from the front-end, in the form of json data(one of the few options for data). it is received by the back-end and used to populate the column values of a specific table. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qc8m25if9y3btqga8ze.png)

The information passed must be correct and fit the requirements of the table column. If for some reason the information does not fit then the column gets the value of Null, or is maybe filled with data that the developer did not mean to acquire. This is why we use validations and constraints to prevent incorrect information, errors, or even the application to simply crash and not work. 
In the image above you can see one of the constraints. _nullable=False_ prevents an input of nothing. A null value is an empty variable with nothing stored in it. This can cause issues on the clients side. It can produce errors, empty information, and overall a bad experience. Besides getting a null value, an empty string, or any variable, can be just as bad. This may not give out as many errors but the information received by the client will be flawed and most likely not valuable at all. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8d19fzpc6zo8ub8tg8e.png)


Using @validates allows me restrict what the client is able to pass to the sql. This prevents any hiccups from empty data and the issues it might cause. 
Validation is not just empty strings and null values. Validations can also be used to make sure the user inputs a correct form of information and even preventing to short of a string. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62zz5arvbo85mdxori8l.png)
In the picture above I am validating that the user inputs data in the form of an email. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4twyze9cgn92rmbxtg76.png)
In the example above I make sure that the user cannot input unreal information, an age cannot be a negative number, and most people will not be over the age of 120. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5tuut2wetmgfbp3vbz7.png)
Sometimes you want to have a minimum amount of characters to make sure that there is valuable information that may require more than one character or one word. 

**Front-end**
To help prevent invalid information even reaching the back end we use some sort of validations and restrictions on my front end. We can use formik to help with this process. Formik combined with yup library help us validate that the user input the correct, or at least the required type and length required for the back-end to be able to process the request and create the new class object. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sobqzumlvr53wxah8rut.png)
The image above shows that all fields (type_of_report, content, and client_name) need to be a string. This means that if any other type is attempted to pass an error message will display and not allow the user to pass the information. This is one way to help prevent any incorrect data from reaching the database. 

Passwords is an example of how validations and constraints work. If the information does not math the requirements then an error will pop up or simply will not allow to log in. 
**Conclusion**
Validations and constraints are very important in any and all applications. They prevent errors and incorrect information from passing and being saved in the database for the information to be incorrect when used by anyone.