Data Validation: Garbage In, Garbage Out
Data validation is the bouncer for your app, checking data at the door to ensure it's correct and useful. It's used on user forms, API requests, and file imports. The footgun is skipping it, which risks corrupted data, security holes, and future crashes.
THE MENTAL MODEL: Think of data validation as the bouncer for your application. It stands at the door and checks every piece of data to ensure it's correct, useful, and safe before it's allowed inside. This enforces the principle of "garbage in, garbage out"—if you let bad data in, you will get bad results out.
HOW IT WORKS: Validation uses a set of rules, sometimes called "constraints" or "check routines," to inspect incoming data. These rules can be simple or complex. Common checks include: data type checks (is this a number or a string?), format checks (does this look like a valid email address?), range checks (is this age between 0 and 120?), and presence checks (is this required field empty?). These rules can be defined within the application's code or sometimes in a central data dictionary that automatically enforces them.
WHEN TO USE IT: You should validate data any time it crosses a boundary into your system. This applies to user input from web forms, data received at an API endpoint, files being uploaded, or records being imported into a database. It is a fundamental practice for building robust and secure software.
WHEN NOT TO USE IT: It is almost never correct to skip validation entirely. Even data from supposedly "trusted" internal systems can be malformed due to bugs or changes. While the strictness of validation may vary based on the source, some level of checking is always prudent. The cost of debugging data corruption issues far outweighs the initial cost of implementing validation.
ONE CANONICAL EXAMPLE: A user registration form is a classic case. The system validates that the username field is not empty and contains only allowed characters. It checks that the email address field contains a string that matches the structure of a real email address. It ensures the password meets complexity requirements (e.g., minimum length, contains a number) and that the birthdate is a valid date, proving the user is old enough to register. If any rule fails, the system rejects the submission and prompts the user to fix the specific error.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.