As the title states, it's time to talk about validations. What does it mean to validate or to be validated? To keep it short and simple, it is the action of checking or proving the validity or accuracy of something.
In the world of cyber technology, I believe as users of platforms such as Facebook, Instagram, and X (formerly known as Twitter) we don't realize or come to the idea that we are always passing or failing validation checks. For example, if we attempt to sign in to our Facebook account with a valid email but forget to enter our password, we as the user might receive a message stating 'Username or password is invalid'. For the average user, it may not spark enough interest to dive deeper and understand what is causing this particular error. But as passionate coders, our minds start running beyond miles to truly understand and hone in on how the magic of such an error can occur.
In general, a lie would be exposed if a statement was made that we as humans don't like being 'validated' for how we believe things should be executed to prevent mishaps. We have all had roommates in our lifetime at some point. Let's paint a picture here. We have two roommates living together. Roommate one likes using top-notch paper towels because they believe it cleans the best when wiping down counters. Roommate two likes using top-notch dish soap because they believe it clears a high volume of oil off of greasy pots. If roommate two buys cheap paper towels, a conflict might arise with roommate one blocking roommate two from using the paper towels they just bought. Why? Because roommate one does not approve of paper towels that are not of good quality. They believe it won't do the job of thoroughly cleaning the countertops. Visa versa, if roommate one buys cheap dish soap, a conflict might arise with roommate two giving the same outcome as the first scenario.
Just how it would work with our roommate scenario and the conflicts that come along with it, validations in the cyber world work in similar ways. So how do the examples above relate to Ruby on Rails validations? Relating back to the example of logging in to a platform with a valid email but an empty input field for a password, we as the user are blocked from gaining access to our account. Facebook (roommate one) does not approve of the user (roommate two) not having a password or a valid password. In return, Facebook responds and says 'No way am I giving you access to an actual account, the values given do not match the credentials of the user you say or think you are'. A conflict is then raised between the user and the platform. When the user (roommate two) provides a password that passes a credential check, Facebook (roommate one) will validate and approve the user moving forward with the actions they want to fulfill.
Validations with Ruby on Rails are used to ensure data sent through an HTTP request from the client (such as logging in or signing into an account with a username and password) are valid to save to the backends database. If the username and password are not valid this would be considered an unprocessable entity. Validations are helper methods, they help the backend prevent bad data from being saved to the database. These helper methods live within our classes so we can specify exactly what attributes within the instances of these classes we want to set validations on.
In the example above we have a User class that utilizes validation helper methods for attributes on a given instance of the class. The first two validations are set up for the attribute username within the instance. Presence ensures that a username does exist in the login or signup input field (the field can not be left empty). Uniqueness ensures that a username can't be used more than once, meaning if I sign up for Facebook under JustenBass5678, when another user signs up and tries using the exact same username they will get an error on the front end. Let's take a look at how this would look on the client side.
Above we have a signup input field for a user creating a new account on a platform. If we submit this form with the username being blank we should get an error due to the presence validation helper method set up in our User class:
An error should also occur on the client side if we signup with a username that already exists for another user due to the uniqueness validation helper method set up in our User class:
If we as the user change our username from JustenBass5678 to JustenBass22, we should meet all the credentials for the validations set in our User class and gain full access to our new account:
Validations are key for preventing bad data from getting saved into a database. However, it also plays a huge role in security. If Facebook allowed all its users to have the same username it would open a wide door for potential hacking. All it would take is another user having the same username to guess the correct password of another user's account and BAM, they are in. What if the platform wasn't Facebook and it was a Chase Bank account? Then a user can possibly gain access to another user's important financial documentation. Sure it could take some time for a hacker to guess the right password but is possible. You can only imagine the many different ideas and theories on the importance of validations. Just know they are IMPORTANT! For more information on validations and other validations you can take advantage of, check out Rails Guides: Active Record Validations!
Top comments (0)