Skip to content

Commit

Permalink
Update AttributeValidator to use Validators.get
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Nov 19, 2023
1 parent 6ffc4a6 commit f8cd9b4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 9 deletions.
10 changes: 1 addition & 9 deletions lib/kangaru/validation/attribute_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@ def validate(validator, **params)

private

def validator_name(validator)
validator.to_s.to_class_name(suffix: :validator)
end

def load_validator(validator:, **params)
name = validator_name(validator)

raise "#{name} is not defined" unless Validators.const_defined?(name)

Validators.const_get(name).new(model:, attribute:, **params)
Validators.get(validator).new(model:, attribute:, **params)
end
end
end
Expand Down
111 changes: 111 additions & 0 deletions spec/features/custom_validators_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
RSpec.describe "a validatable model using custom validator rules" do
subject(:model) { SomeGem::Model.new(**attributes) }

include_context :kangaru_initialised

let(:attributes) { { attribute: value } }

let(:value) { :valid }

let(:model_class) do
<<~RUBY
module SomeGem
class Model
include Kangaru::Validatable
#{validations}
attr_reader :attribute
def initialize(attribute:)
@attribute = attribute
end
end
end
RUBY
end

let(:validator_class) do
<<~RUBY
module SomeGem
module Validators
class CustomValidator < Kangaru::Validator
def validate
return if value == :valid
add_error!("is invalid")
end
end
end
end
RUBY
end

before do
gem.path("validators", ext: nil).mkdir
gem.path("validators", "custom_validator").write(validator_class)

gem.path("model").write(model_class)

gem.load!
end

describe "#validate" do
subject(:validate) { model.validate }

let(:validations) do
<<~RUBY
validates :attribute, #{validator_name}: true
RUBY
end

context "when validator with given name is not defined" do
let(:validator_name) { :undefined }

it "raises an error" do
expect { validate }.to raise_error("UndefinedValidator is not defined")
end
end

context "when validator with given name is defined" do
let(:validator_name) { :custom }

context "and model is not valid" do
let(:value) { :invalid }

it "does not raise any errors" do
expect { validate }.not_to raise_error
end

it "is not valid" do
expect(model).not_to be_valid
end

it "adds an error" do
expect { validate }.to change { model.errors.count }.by(1)
end

it "sets the expected message" do
validate
expect(model.errors.last.full_message).to eq("Attribute is invalid")
end
end

context "and model is valid" do
let(:value) { :valid }

it "does not raise any errors" do
expect { validate }.not_to raise_error
end

it "is valid" do
expect(model).to be_valid
end

it "does not add any errors" do
expect { validate }.not_to change { model.errors }
end
end
end
end
end

0 comments on commit f8cd9b4

Please sign in to comment.