This week I have been going over my past projects. One of the main things I've noticed are my messed databases. Why? Because I did not pay much attention to validations when I created them. Some only had one validation or even one. I found that it is easy to get distracted with the front end and not spend much time on your back-end. So this past week, I have been going through my past projects and cleaning up the back-end. That means adding in some validations.
What are validations?
Validations in a sense are check points before data enters your database. The data being entered or processed to the back-end are being checked before they are committed into the database.
Why are validations important?
As someone creating examples for your own product or code, you may know exactly what should be entered in your database. However, most people don't and plenty of times, the user will enter something completely unpredictable in the input. This can often times break you code completely. Having this in the database is considered "bad" data and will probably affect your project. Imagine a user typing in a username that's already been used. What if they don't type in anything at all? What if they enter in a number and someone else enters the same number for their username? Users can enter many "odd" things into your database. So using validations will keep this from happening and keep your database clean.
Rails Validation Format
class User < ActiveRecord::Base
validates :username, :presence => true
end
Here's a simple example of a rails validation? Pretty simple and easy to follow right? You declare the attribute that you want checked or validated and what you are checking for.
List of Common Validations
Uniqueness
class User < ActiveRecord::Base
validates :username, :uniqueness => true
end
This is one of the most common validations I have ever used because my projects all have some kind of user or account. Uniqueness means that whatever attribute is chosen in the validation, that attribute has to be unique. In this case, if a username of JohnDoe was entered by a new user and it already exists, then this new entry will not be committed into the database.
Presence
class User < ActiveRecord::Base
validates :username, :first_name, :last_name :presence => true
end
This one's self explanatory and very commonly used. Basically, it's saying, does this attribute exist? If not, then do not commit. Notice that there are several attributes in this one. If you are checking for all your fields for presence then you can enter them on the same line like this.
Length
class Post < ActiveRecord::Base
validates :title, length: 6..20
validates :post_body, length: { maximum: 300 }
validates :post_body, length: { minimum: 30 }
end
There are several ways to define what you want in length. In this case, a post body is restricted to only 300 words. So if a post is more than 300 words, it will not save to the database. However, there is also a minimum set. If it does not reach at least 30 words, then it will not save either. This is a good example of setting a minimum because if you are building a blog platform, do you really want people to have blogs of only 1 word? We also see that you can dictate max and min through a range like the first validation.
Format
class User < ApplicationRecord::Base
validates :first_name, :last_name,
format: { with: /\A[a-zA-Z]+\z/, message: "only allows letters" }
end
This validates the format of what you want in your database? Official names should only be letters right? What about pin numbers? Or input fields that need dates. This can all be validated with this method.
Confirmation
class User < ApplicationRecord
validates :password, confirmation: true
end
l mainly use this one for password confirmation but it is very useful and common as well.
There are many validations out there. More can be found on the rails validation document. I hope this helped you. Thank you for reading.
Top comments (0)