mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-01-12 17:52:17 +08:00
RMQ::Validation.add_validator method + spec
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user