Data Validation is super important, don’t ignore or delay it.

I often encounter backend codebases where data isn’t validated before being processed or inserted directly into the database. This can cause serious bugs, as relying solely on your frontend buddy can lead to headaches, ruin the flow, result in unwanted…


This content originally appeared on DEV Community and was authored by Muneeb Hussain

I often encounter backend codebases where data isn't validated before being processed or inserted directly into the database. This can cause serious bugs, as relying solely on your frontend buddy can lead to headaches, ruin the flow, result in unwanted and unexpected data in the database, and make you vulnerable to SQL injection if you are using an SQL database.

What's the solution then?
I agree that data validation is a must, but it isn't always easy to get right.

Here's what you can do:

Don't trust anyone.

Validating data involves four crucial steps:

  1. Check the type.
  2. Validate the format.
  3. Refine it.
  4. Transform it (optional).

We often stop at the first step and ignore the rest. Don't do that. Take a simple example of validating a phone number. You accept a "string" and check if it is a string. If so, you allow it and let it go to the database. No errors for now. But what if you have to verify that phone number with some third-party API and the format of the phone number isn't valid? Handling it later may still be possible, but it's not a good user experience to show the user that they have input the wrong data because we didn't validate the format earlier. Now they have to provide it again in the proper format.

To create a good user experience, you should always:

  1. Check the data type (string, boolean, integer, object).
  2. Validate the format - if it's a phone number, it must look like one.
  3. Refine it - make it perfect for future use cases. For example, if you don't need the "+" at the start, remove it now.
  4. Transform it - after removing the "+", you may be left with an alphanumeric string, but you need a number instead of a string, so transform it.

That's all you need to take care of to make your backend more robust.

If you are a TypeScript lover like me, you can consider using Zod to fulfill all of the above criteria. It's a comprehensive library with great TypeScript support.

Thanks for reading.


This content originally appeared on DEV Community and was authored by Muneeb Hussain


Print Share Comment Cite Upload Translate Updates
APA

Muneeb Hussain | Sciencx (2024-06-30T03:16:51+00:00) Data Validation is super important, don’t ignore or delay it.. Retrieved from https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/

MLA
" » Data Validation is super important, don’t ignore or delay it.." Muneeb Hussain | Sciencx - Sunday June 30, 2024, https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/
HARVARD
Muneeb Hussain | Sciencx Sunday June 30, 2024 » Data Validation is super important, don’t ignore or delay it.., viewed ,<https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/>
VANCOUVER
Muneeb Hussain | Sciencx - » Data Validation is super important, don’t ignore or delay it.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/
CHICAGO
" » Data Validation is super important, don’t ignore or delay it.." Muneeb Hussain | Sciencx - Accessed . https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/
IEEE
" » Data Validation is super important, don’t ignore or delay it.." Muneeb Hussain | Sciencx [Online]. Available: https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/. [Accessed: ]
rf:citation
» Data Validation is super important, don’t ignore or delay it. | Muneeb Hussain | Sciencx | https://www.scien.cx/2024/06/30/data-validation-is-super-important-dont-ignore-or-delay-it/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.