Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BaseValidator and CreateValidator classes #5

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/validators/base_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

# Base class for all validators.
# This class provides a simple interface for validating objects using ActiveModel validations.
#
# Usage:
#
# class CreateCandidateValidator < BaseValidator
# validates :name, presence: true
# validates :email, presence: true, email: true
# end
#
# candidate = Candidate.new(name: '', email: 'john@example.com')
# CreateCandidateValidator.valid?(candidate) # => false
# CreateCandidateValidator.validate!(candidate) # => raises BaseValidator::ValidationError
class BaseValidator < SimpleDelegator
class ValidationError < StandardError; end

include ActiveModel::Validations

# Checks if the object is valid based on the defined validations.
#
# @param object [Object] The object to be validated.
# @param params [Hash] Additional parameters for validation.
# @return [Boolean] Returns true if the object is valid, false otherwise.
def self.valid?(object, params = {})
new(object, params).valid?
end

# Validates the object and raises a ValidationError if it is not valid.
#
# @param object [Object] The object to be validated.
# @param params [Hash] Additional parameters for validation.
def self.validate!(object, params = {})
validator = new(object, params)
return true if validator.valid?

raise ValidationError, validator.errors.full_messages.join(', ')
end

# Initializes a new instance of BaseValidator.
#
# @param object [Object] The object to be validated.
# @param params [Hash] Additional parameters for validation.
def initialize(object, params = {})
super(object)
@object = object
@params = params
end
end
16 changes: 16 additions & 0 deletions app/validators/candidates/create_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Candidates
class CreateValidator < BaseValidator
validates :name, presence: true
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :birthdate, presence: true
validate :past_birtdate

def past_birtdate
return if birthdate.blank?

errors.add(:birthdate, :future_date) if birthdate > Date.current
end
end
end
51 changes: 51 additions & 0 deletions spec/validators/base_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

class Test
class Validator < BaseValidator
validates :name, presence: true
validate :gmail_email

def gmail_email
return if email.to_s.ends_with?('@gmail.com')

errors.add(:email, 'must be a Gmail address')
end
end
end

RSpec.describe BaseValidator do
describe '.valid?' do
subject(:valid?) { Test::Validator.valid?(candidate) }

context 'when the candidate is valid' do
let(:candidate) { build(:candidate, email: 'john@gmail.com') }

it { is_expected.to be true }
end

context 'when the candidate is invalid' do
let(:candidate) { build(:candidate, email: 'john@hotmail.com') }

it { is_expected.to be false }
end
end

describe '.validate!' do
subject(:validate!) { Test::Validator.validate!(candidate) }

context 'when the candidate is valid' do
let(:candidate) { build(:candidate, email: 'john@gmail.com') }

it { is_expected.to be true }
end

context 'when the candidate is invalid' do
let(:candidate) { build(:candidate, name: '', email: 'john@hotmail.com') }

it 'raises a ValidationError' do
expect { validate! }.to raise_error(BaseValidator::ValidationError,
"Name can't be blank, Email must be a Gmail address")
end
end
end
end
43 changes: 43 additions & 0 deletions spec/validators/candidates/create_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

RSpec.describe Candidates::CreateValidator do
describe '.valid?' do
subject { described_class.valid?(candidate) }

let(:candidate) { build(:candidate) }

context 'when the candidate is valid' do
it { is_expected.to be true }
end

context 'when the name is blank' do
before { candidate.name = '' }

it { is_expected.to be false }
end

context 'when the email is blank' do
before { candidate.email = '' }

it { is_expected.to be false }
end

context 'when the email is invalid' do
before { candidate.email = 'my.email.at.example.com' }

it { is_expected.to be false }
end

context 'when the birthdate is blank' do
before { candidate.birthdate = nil }

it { is_expected.to be false }
end

context 'when the birthdate is in the future' do
before { candidate.birthdate = 1.day.from_now }

it { is_expected.to be false }
end
end
end
Loading