Let's talk validations.
Published: 2023-09-18 19:35:27

**A**s 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._ 

**I**n 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.

**I**n 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.

**J**ust 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.

**V**alidations 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.


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

**I**n 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.

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

**A**bove 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:



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16sc7fqou0ldxd8kqdg9.jpeg)

**A**n 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:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/495lvnb4fvlaevhd6rxt.jpeg)

**I**f 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:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e8ss4o704fc92e803hx.jpeg)


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

**V**alidations 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](https://guides.rubyonrails.org/active_record_validations.html)!


## **Resources**
**1:** [Rails Guides: Active Record Validations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)