Method: Modelish::Validations::ClassMethods#validate_length

Defined in:
lib/modelish/validations.rb

#validate_length(name, value, max_length) ⇒ Object

Validates the length of a value, returning an error when validation fails.

Parameters:

  • name (Symbol, String)

    the property/argument to validate

  • value (#length)

    the value to be validated

  • max_length (#to_i)

    the maximum allowable length

Raises:

  • (ArgumentError)

    when the value is longer than max_length



148
149
150
151
152
153
# File 'lib/modelish/validations.rb', line 148

def validate_length(name, value, max_length)
  if max_length.to_i > 0 && value.to_s.length > max_length.to_i
    message = "#{name} must be less than #{max_length} characters"
    ArgumentError.new(message)
  end
end