0
0
Ruby on Railsframework~20 mins

Format validation in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Format Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Rails model with format validation receives invalid input?
Consider a Rails model with this validation:
validates :email, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "must be a valid email" }

What will be the state of the model after assigning user.email = "invalid-email" and calling user.valid??
Ruby on Rails
class User < ApplicationRecord
  validates :email, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "must be a valid email" }
end

user = User.new
user.email = "invalid-email"
valid = user.valid?
AThe model is valid; errors will be empty because format validation only checks presence.
BThe model is valid; format validation ignores invalid emails without @ symbol.
CThe model raises a runtime error due to invalid regex in validation.
DThe model is invalid; user.errors will include "email must be a valid email".
Attempts:
2 left
💡 Hint
Think about what happens when the input does not match the regex pattern.
📝 Syntax
intermediate
2:00remaining
Which format validation syntax is correct in Rails?
You want to validate that a username contains only letters and numbers. Which of these validations is syntactically correct?
Avalidates :username, format: { regex: /\A[a-zA-Z0-9]+\z/ }
Bvalidates_format_of :username, with: /\A[a-zA-Z0-9]+\z/, message: 'only letters and numbers allowed'
Cvalidates :username, format: { with: /\A[a-zA-Z0-9]+\z/ }
Dvalidates :username, format: /\A[a-zA-Z0-9]+\z/
Attempts:
2 left
💡 Hint
Check the correct key name inside the format hash.
🔧 Debug
advanced
2:00remaining
Why does this format validation not work as expected?
Given this model validation:
validates :phone, format: { with: /\d{3}-\d{3}-\d{4}/ }

Why might a phone number like "123-456-7890" still be invalid?
AThe regex is missing start (\A) and end (\z) anchors, so it matches substrings but validation expects full match.
BThe regex lacks anchors, so partial matches pass, but full string match is required.
CThe regex is correct; the problem is the phone attribute is nil.
DThe format validation requires the regex to be a string, not a Regexp object.
Attempts:
2 left
💡 Hint
Think about how Rails uses the regex to validate the entire attribute string.
state_output
advanced
2:00remaining
What is the value of errors after invalid format validation?
Given this code:
class User < ApplicationRecord
  validates :zip_code, format: { with: /\A\d{5}\z/, message: 'must be 5 digits' }
end

user = User.new(zip_code: '1234a')
user.valid?
errors = user.errors[:zip_code]

What is the value of errors?
A["must be 5 digits"]
B["is invalid"]
C[]
Dnil
Attempts:
2 left
💡 Hint
Check the custom message in the validation.
🧠 Conceptual
expert
2:00remaining
Why use format validation instead of custom validation method in Rails?
Which is the main advantage of using validates :attribute, format: { with: /regex/ } over writing a custom validate :method_name method for format checking?
AFormat validation allows multiple regex patterns at once, custom methods do not.
BFormat validation integrates automatically with Rails error messages and helpers, reducing boilerplate code.
CCustom validation methods cannot add errors to the model.
DCustom validation methods run faster than format validations.
Attempts:
2 left
💡 Hint
Think about how Rails helpers and error messages work with built-in validations.