Challenge - 5 Problems
Format Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a Rails model with format validation receives invalid input?
Consider a Rails model with this validation:
What will be the state of the model after assigning
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?
Attempts:
2 left
💡 Hint
Think about what happens when the input does not match the regex pattern.
✗ Incorrect
The format validation checks if the email matches the regex. Since "invalid-email" lacks an @ symbol, it fails the regex, making the model invalid and adding an error message.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Check the correct key name inside the format hash.
✗ Incorrect
The correct syntax uses the :with key inside the format hash. Option C uses this correctly. Option C misses the hash, C uses deprecated method, and A uses wrong key :regex.
🔧 Debug
advanced2:00remaining
Why does this format validation not work as expected?
Given this model validation:
Why might a phone number like "123-456-7890" still be invalid?
validates :phone, format: { with: /\d{3}-\d{3}-\d{4}/ }Why might a phone number like "123-456-7890" still be invalid?
Attempts:
2 left
💡 Hint
Think about how Rails uses the regex to validate the entire attribute string.
✗ Incorrect
Rails format validation requires the regex to match the entire string. Without \A and \z anchors, the regex matches substrings, causing validation to fail if the whole string doesn't match.
❓ state_output
advanced2:00remaining
What is the value of errors after invalid format validation?
Given this code:
What is the value of
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?Attempts:
2 left
💡 Hint
Check the custom message in the validation.
✗ Incorrect
Since '1234a' does not match the 5-digit regex, validation fails and the custom message 'must be 5 digits' is added to errors[:zip_code].
🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Think about how Rails helpers and error messages work with built-in validations.
✗ Incorrect
Using format validation leverages Rails built-in features like automatic error message generation and integration with form helpers, making code simpler and more consistent.