RMQ::Validation.add_validator method + spec

This commit is contained in:
Vaughan Kelly Guy
2014-09-10 17:21:11 -04:00
parent a0a8b077de
commit f18460cd40
2 changed files with 33 additions and 0 deletions

View File

@@ -206,6 +206,10 @@ module RubyMotionQuery
(value.to_s =~ regex) != nil
end
def add_validator(rule, &block)
@@validation_methods[rule] = block
end
end
end
end

View File

@@ -276,4 +276,33 @@ describe 'validation' do
end
end
describe 'adding a custom validator' do
before do
@rmq.validation.add_validator(:start_with) do |value, opts|
value.start_with?(opts[:prefix])
end
end
it "should not raise a RuntimeError for a missing validation method" do
should.not.raise(RuntimeError) do
validation = @rmq.validation.new(:start_with)
end
end
it "should validate at utility and selection level" do
# Utility Level
@rmq.validation.valid?('test', :start_with, {prefix: 'te'}).should == true
@rmq.validation.valid?('test', :start_with, {prefix: 'est'}).should == false
# Validation Selection level
vc = UIViewController.new
vc.rmq.append(UITextField).validates(:start_with, prefix: 'x').data('test').tag(:one)
vc.rmq(:one).valid?.should == false
vc.rmq.append(UITextField).validates(:start_with, prefix: 't').data('test').tag(:two)
vc.rmq(:two).valid?.should == true
end
end
end