Method: AWS::Record::Validations#validates_length_of

Defined in:
lib/aws/record/validations.rb

#validates_length_of(*attributes, options = {}, &block) ⇒ Object

Validates the attribute values are of a specified length.

validates_lenth_of :username, :within => 3..25

Length vs Count

validates_length_of validates the length of individual attribute values, whereas +validates_count_of: validates the number of attribute values.

If you need to ensure there are certain number of values see #validates_count_of instead.

Options Hash (options):

  • :within (Enumerable)

    An enumerable object to ensure the length of the value falls within.

  • :exactly (Integer)

    The exact length a value must be. If this validation fails the error message specified by :wrong_length will be added.

  • :within (Range)

    An enumerable object which must include the length of the attribute, or an error will be added. If the attribute has a length outside the range then the :too_long or :too_short error message will be added.

  • :minimum (Integer)

    The minimum length an attribute value should be. If it is shorter, the :too_short error message will be added.

  • :maximum (Integer)

    The maximum length an attribute value should be. If it is longer, the :too_long error message will be added.

  • :too_long (String)

    An error message added when the attribute value is too long. Defaults to "is too long (maximum is %{maximum} characters)"

  • :too_short (String)

    An error message added when the attribute value is too short. Defaults to "is too short (minimum is %{minimum} characters)"

  • :wrong_length (String)

    An error message added when the attribute has the incorrect length (as specified by :exactly). Defaults to "is the wrong length (should be %{exactly} characters"

  • :allow_nil (Boolean) — default: false

    Skip validation if the attribute value is nil.

  • :allow_blank (Boolean) — default: false

    Skip validation if the attribute value is blank.

  • :on (Symbol) — default: :save

    When this validation is run. Valid values include:

    • :save
    • :create
    • :update
  • :if (Symbol, String, Proc)

    Specifies a method or proc to call. The validation will only be run if the return value is of the method/proc is true (e.g. :if => :name_changed? or :if => lambda{|book| book.in_stock? }).

  • :unless (Symbol, String, Proc)

    Specifies a method or proc to call. The validation will not be run if the return value is of the method/proc is false.



597
598
599
# File 'lib/aws/record/validations.rb', line 597

def validates_length_of *args
  validators << LengthValidator.new(self, *args)
end