Class: AttrValidator::Validators::LengthValidator
- Inherits:
-
Object
- Object
- AttrValidator::Validators::LengthValidator
- Defined in:
- lib/attr_validator/validators/length_validator.rb
Class Method Summary collapse
-
.validate(object, options) ⇒ Array
Validates that given object has correct length.
- .validate_options(options) ⇒ Object
Class Method Details
.validate(object, options) ⇒ Array
Validates that given object has correct length
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/attr_validator/validators/length_validator.rb', line 7 def self.validate(object, ) return [] if object.nil? errors = [] if [:min] errors << AttrValidator::I18n.t('errors.can_not_be_less_than', length: [:min]) if object.length < [:min] end if [:max] errors << AttrValidator::I18n.t('errors.can_not_be_more_than', length: [:max]) if object.length > [:max] end if [:equal_to] errors << AttrValidator::I18n.t('errors.should_be_equal_to', length: [:equal_to]) if object.length != [:equal_to] end if [:not_equal_to] errors << AttrValidator::I18n.t('errors.should_not_be_equal_to', length: [:not_equal_to]) if object.length == [:not_equal_to] end errors end |
.validate_options(options) ⇒ Object
27 28 29 30 |
# File 'lib/attr_validator/validators/length_validator.rb', line 27 def self.() AttrValidator::ArgsValidator.is_hash!(, :validation_rule) AttrValidator::ArgsValidator.has_only_allowed_keys!(, [:min, :max, :equal_to, :not_equal_to], :validation_rule) end |