Skip to content

Commit

Permalink
Merge pull request #28 from ontoportal-lirmm/feature/implement-new-va…
Browse files Browse the repository at this point in the history
…lidators

Feature: Implement instance proc validators
  • Loading branch information
syphax-bouazzouni committed Jul 8, 2023
1 parent fc27d3c commit 56bd84c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/goo/validators/enforce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ def enforce(inst,attr,value)
type = opt.to_s.index("max_") ? :max : :min
check Goo::Validators::ValueRange, inst, attr, value, type, opt.to_s
else
model_range = object_type(opt)
check_object_type inst, attr, value, model_range
if object_type?(opt)
check_object_type inst, attr, value, opt
elsif instance_proc?(inst, opt)
call_proc(inst.method(opt), inst, attr)
end
end
end

Expand All @@ -67,8 +70,16 @@ def object_type(opt)
opt.respond_to?(:shape_attribute) ? opt : Goo.model_by_name(opt)
end

def check_object_type(inst, attr, value, model_range)
def object_type?(opt)
opt.respond_to?(:shape_attribute) ? opt : Goo.model_by_name(opt)
end

def instance_proc?(inst, opt)
opt && (opt.is_a?(Symbol) || opt.is_a?(String)) && inst.respond_to?(opt)
end

def check_object_type(inst, attr, value, opt)
model_range = object_type(opt)
if model_range && !value.nil?
check Goo::Validators::ObjectType, inst, attr, value, model_range.model_name, model_range
end
Expand All @@ -82,9 +93,12 @@ def enforce_by_attribute(model, attr)
model.model_settings[:attributes][attr][:enforce]
end

def call_proc(opt,inst, attr)
def call_proc(proc,inst, attr)
# This should return an array like [:name_of_error1, "Error message 1", :name_of_error2, "Error message 2"]
errors = opt.call(inst, attr)
errors = proc.call(inst, attr)

return unless !errors.nil? && errors.is_a?(Array)

errors.each_slice(2) do |e|
next if e.nil? || e.compact.empty?
add_error(e[0].to_sym, e[1])
Expand Down
39 changes: 39 additions & 0 deletions test/test_validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ class InverseOfTestModel < Goo::Base::Resource
end


class ProcValidatorsTestModel < Goo::Base::Resource
model :proc_validator_test_model, name_with: :name
attribute :name, enforce: [:unique, :equal_to_test]
attribute :last_name, enforce: [:unique, ->(inst, attr) { equal_to_test_2(inst, attr)}]


def self.equal_to_test_2(inst, attr)
value = inst.send(attr)

return nil if value && value.eql?('test 2')

[:equal_to_test_2, "#{attr} need to be equal to `test 2`"]
end

def equal_to_test(inst, attr)
value = inst.send(attr)

return nil if value && value.eql?('test')

[:equal_to_test, "#{attr} need to be equal to `test`"]
end
end

class TestValidators < MiniTest::Unit::TestCase

def self.before_suite
Expand Down Expand Up @@ -385,4 +408,20 @@ def test_inverse_of_validator_list
assert p1.valid?

end


def test_proc_validators
p = ProcValidatorsTestModel.new
p.name = "hi"
p.last_name = "hi"

refute p.valid?
assert p.errors[:name][:equal_to_test]
assert p.errors[:last_name][:equal_to_test_2]

p.name = "test"
p.last_name = "test 2"

assert p.valid?
end
end

0 comments on commit 56bd84c

Please sign in to comment.