Validate a min and max value of an Integer

39 views
Skip to first unread message

samuil

unread,
Mar 13, 2018, 7:31:14 AM3/13/18
to Roda
In the documentation it's not mentioned how to validate that an integer has a certain min or max value. It only mentions "validates_min_length" and "validates_max_length" which is for a String, right?

How to validate an Integer?

Jeremy Evans

unread,
Mar 13, 2018, 10:30:33 AM3/13/18
to Roda
On Tuesday, March 13, 2018 at 4:31:14 AM UTC-7, samuil wrote:
In the documentation it's not mentioned how to validate that an integer has a certain min or max value. It only mentions "validates_min_length" and "validates_max_length" which is for a String, right?

How to validate an Integer?

I assume this is a question for Sequel and not Roda, but "validates_includes min..max, column" should work for both a min and max value.  If you only have a min or a max, "validates_operator :>=, min, col" or "validates_operator :<=, max, col".

Thanks,
Jeremy

samuil

unread,
Mar 14, 2018, 10:05:06 AM3/14/18
to Roda
yes, for Sequel:


    class MyModel < Sequel::Model
     
def validate
       
super
        validates_operator
(:>, 1, :var1)
        validates_operator
(:>, 100, :var2)
     
end
   
end



And an error at runtime:


      NoMethodError: undefined method `validates_operator' for #<MyModel:0x00007ff25c12e818>

Adam Daniels

unread,
Mar 14, 2018, 10:11:41 AM3/14/18
to Roda
On Wednesday, March 14, 2018 at 10:05:06 AM UTC-4, samuil wrote:
yes, for Sequel:


    class MyModel < Sequel::Model
     
def validate
       
super
        validates_operator
(:>, 1, :var1)
        validates_operator
(:>, 100, :var2)
     
end
   
end



And an error at runtime:


      NoMethodError: undefined method `validates_operator' for #<MyModel:0x00007ff25c12e818>



You're missing the validation_helpers plugin.

class MyModel < Sequel::Model
  plugin :validation_helpers

  def validate
    super
    validates_operator :>, 1, :var1
    validates_operator :>, 100, :var2
  end
end 
Reply all
Reply to author
Forward
0 new messages