What is a "validation"?
In terms of strict English, a validation is a method that serves as a way of confirming something is what it is before anything else happens.
"Does this dress look good on me? I want to validate that it's not tacky and okay to wear to the party."
Definitely not the most normal human interaction but you get it.
Validations in code are essentially the same thing! They consist of code that protects the database from receiving invalid data that may harm the database or other aspects of the application.
We implement validations to assure ourselves that we are receiving the correct information every time.
One method of validations can be handled through ActiveRecord, which we will discuss in a later blog. For now, this is how it will look.
Basic Usage
class Person < ActiveRecord::Base
validates :name, presence: true
end
#validates
is doing exactly what it looks like it's doing. It is accepting the name of the attribute that we want to validate (:name) and accepting a hash of options that includes the details of the validation. { presence: true } is a very basic validation that checks that there is at least something present for the :name attribute.
Well, writing validations is nice, but, how do we even know if something is validated or not?
We can check manually with #valid?
class Person < Active Record::Base
validates :name, presence: true
end
person = Person.new
person.valid? #=> false
Above, we created a new person and set it equal to person. We then called #valid? on our new person and returned a value of false. Why is it false? Let's check with errors.messages
!
person = Person.new
person.errors.messages #=> name: can't be blank
We did not create a new person with a name, therefore, failing the validation of { presence: true }
in reference to the name attribute.
To give our users more information on the validation error, we can also render our validation errors as JSON in our controllers, which will be discussed in another blog post.
def create
person = Person.create(person_params)
if person.valid?
render json: person, status: :created
else
render json: { error: person.errors }, status: :unprocessable_entity
end
end
render json: { error: person.errors }
: This line of code is doing exactly what we did before when we called person.errors.messages but displaying it as JSON for the user to see on their end, the frontend.
Other Built-In Validators
Length
length
is one of the most versatile because most inputs, if not all, will be a certain amount of characters long.
class Person < Active Record::Base
validates :name, length: { minimum: 2 }
validates :bio, length: { maximum: 500 }
validates :password, length: { in: 6..20 }
validates :registration_number, length: { is: 6 }
end
Uniqueness
uniqueness
is also very common as it is used to ensure that the same username or email cannot be used for more than user account.
class Account < Active Record::Base
validates :email, uniqueness: true
end
Custom Messages
class Alien < Active Record::Base
validates :not_a_human, acceptance: true, message: "Extraterrestrials only!"
end
You can say whatever you want for the errors! Make it yours!
Conclusion
We covered the importance of validations and why they are necessary for any application worth using. If you want a secure database, validations must be implemented. If you want to feel safe as a consumer, make sure the applications that use have validations implemented. We went over common validation methods and use-cases.
Resources
https://guides.rubyonrails.org/active_record_validations.html
https://web-crunch.com/posts/understanding-ruby-on-rails-activerecord-validations
Top comments (0)