Kinnu

Data validation

Data Validation and Error Handling

What is data validation?

Data validation makes sure that the data entered into a system meets predefined formats. For example, if a database lists weights in kilograms, it could stop a user from entering a weight in grams.

In 1999, NASA’s Mars Climate Orbiter accidentally disintegrated in Mars’ atmosphere thanks to a data validation issue. One team used metric units (Newtons) for measuring thrust, while another team used English units (pounds). Because of this confusion, the spacecraft approached Mars with too much speed – something that would have been avoided if the data was properly validated.

Data validation isn't just about avoiding errors. It also prevents the entry of malicious data, such as inputs that could exploit vulnerabilities or manipulate the system.

Validation processes

Data validation processes are a set of rules or criteria that data must meet before it is accepted into a system. These rules can be as simple as checking if a field is filled out, or as complex as verifying that a credit card number is valid.

There are various types of data validation. A range check ensures that a data value isn't too big or too small. A format check makes sure that if you only want numbers, a user can’t input letters. An existence check verifies whether a given data entry corresponds to an existing entity, like when a user is trying to choose a new username, and it can’t be the same as someone else's.

These processes could be used at the point of data entry, to prevent bad data from entering the system in the first place. They could also be used during data transmission, catching errors that occur when data is moved from one backend system to another.

Error handling

When data is entered incorrectly, error handling is the safety net: response procedures that protect the system when unacceptable data makes it through the checks.

Log in page. Image: energepic.com via Pexels (https://www.pexels.com/photo/person-holding-phone-while-logging-in-on-instagram-application-174938/)

For example, a Backend Developer might build an app that lets users enter usernames made up of letters. The validation procedures stop users from entering numbers. But the developer forgot about emojis – and when a user enters a poop emoji, the backend doesn't know what to do with it. It tries to save it to a database, where emojis aren't compatible, and the entire system crashes.

This wouldn't have happened if the developer had put a measure in place that tells the backend how it's meant to respond to unexpected conditions.

Try-catch blocks

When handling errors, a developer might use a try-catch block. This is a piece of code that works like a piece of protective casing, surrounding another piece of code that the developer thinks might fail. If the code does fail, the try-catch code will detect it, and perform an action to prevent the code from crashing.

For example, if a poop emoji caused a database to fail its attempts at writing data, a try-catch block would detect this. In response, it would close the database connection to stop the database from crashing, then print an error statement to the console so that the developer sees what happened.

The code for this try-catch might look like this:

Try {

database.write(“INSERT INTO users (username) VALUES (💩);”

} catch (error) {

database.close()

print(“error inserting data into database”)

}

Error handling. Image: Subhashish Panigrahi, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons

Testing and Best Practices

Testing procedures

Proactive testing allows developers to identify and fix potential issues before they become problems.

The testing process involves creating scenarios to trigger errors and validate the system's response to these errors. This can involve anything from entering incorrect data to simulating network failures. By testing how the system responds to these scenarios, developers can ensure that their error handling mechanisms are effective, and that their data validation processes are robust.

Automated testing tools can be used to simulate a variety of error conditions and validate the effectiveness of error handling mechanisms. These tools can save developers time and effort by automating the process of creating and executing test scenarios. They can also provide a more comprehensive and rigorous testing process, ensuring that all potential error conditions are accounted for.

Useful tools

Backend Developers are not on their own when it comes to data validation and error handling. There are various tools available, ranging from built-in programming language features to standalone software applications.

These tools provide a range of features that improve efficiency and decrease the likelihood of human error. For example, a developer might use an automated testing tool like Katalon to create and execute meticulous tests that validate aspects of their application, including input fields, form submissions, and error scenarios.

Katalon Studio. Image: Katalon LLC, CC BY-SA 4.0 <https://creativecommons.org/licenses/by-sa/4.0>, via Wikimedia Commons

Most of these tools come with a user-friendly interface for defining test cases, running them across various platforms, and generating detailed reports.

Best practices

When it comes to data validation and error handling, there are a few best practices which are good for developers to follow.

Early data validation – at the point of entry – makes sure that invalid data is caught and addressed before it enters the backend system. When this happens, a clear error message is important, so that users understand what went wrong. For example, if a user's password is rejected because it has too few letters, the user needs to be told to make their password longer.

As for error handling, a clear strategy makes sure that errors are all handled in a similar manner, making it easier to diagnose and fix issues. Logging errors is also critical, as it provides a record of what went wrong and when. Overall, developers need to design their programs to handle errors gracefully, reducing the likelihood of crashes.