ruby
33 lines · 6 steps
Country-aware phone validation in Rails
A Contact model normalizes phone input, then validates it against per-country regex rules with tailored error messages.
Explained by
highlit
1class Contact < ApplicationRecord
2 PHONE_FORMATS = {
3 "US" => /\A\+1\d{10}\z/,
4 "GB" => /\A\+44\d{10}\z/,
5 "DE" => /\A\+49\d{10,11}\z/,
6 "IN" => /\A\+91\d{10}\z/,
7 "BR" => /\A\+55\d{10,11}\z/
8 }.freeze
9
10 before_validation :normalize_phone
11
12 validates_each :phone do |record, attr, value|
13 country = record.country_code.to_s.upcase
14 format = PHONE_FORMATS[country]
15
16 if value.blank?
17 record.errors.add(attr, :blank)
18 elsif format.nil?
19 record.errors.add(attr, :unsupported_country, country: country)
20 elsif !value.match?(format)
21 record.errors.add(attr, :invalid_phone, country: country)
22 end
23 end
24
25 private
26
27 def normalize_phone
28 return if phone.blank?
29
30 digits = phone.gsub(/[\s().-]/, "")
31 self.phone = digits.start_with?("+") ? digits : "+#{digits}"
32 end
33end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Normalizing input in a before_validation callback lets validation logic assume clean, canonical data.
- 2A lookup table of regexes keyed by identifier keeps per-case rules declarative and easy to extend.
- 3Distinct error keys let each failure mode produce its own translatable, context-rich message.
Related explainers
ruby
class ToastBroadcaster include ActionView::RecordIdentifier def self.broadcast_to(user, message:, type: :notice)
How Turbo Stream toasts broadcast in Rails
turbo-streams
service-object
real-time
Intermediate
6 steps
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
ruby
class KeyTransformer def self.camelize(data) new.camelize(data) end
Recursively camelizing nested Ruby data
recursion
data-transformation
pattern-matching
Intermediate
7 steps
ruby
module Paginatable extend ActiveSupport::Concern private
A reusable pagination concern in Rails
pagination
concerns
http-headers
Intermediate
8 steps
ruby
class Api::MessagesController < ApiController before_action :authenticate_api_key! rate_limit to: 100,
Layered API rate limiting in Rails
rate-limiting
api-authentication
throttling
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/country-aware-phone-validation-in-rails-explained-ruby-534a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.