min_max.rb 921 B

1234567891011121314151617181920212223242526272829303132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Validations::ObjectManager::AttributeValidator::MinMax < Validations::ObjectManager::AttributeValidator::Backend
  3. def validate
  4. return if value.blank?
  5. return if irrelevant_attribute?
  6. validate_min
  7. validate_max
  8. end
  9. private
  10. def irrelevant_attribute?
  11. attribute.data_type != 'integer'.freeze
  12. end
  13. def validate_min
  14. return if !attribute.data_option[:min]
  15. return if value >= attribute.data_option[:min]
  16. invalid_because_attribute(__('is smaller than the allowed minimum value of %{min_value}'), min_value: attribute.data_option[:min])
  17. end
  18. def validate_max
  19. return if !attribute.data_option[:max]
  20. return if value <= attribute.data_option[:max]
  21. invalid_because_attribute(__('is larger than the allowed maximum value of %{max_value}'), max_value: attribute.data_option[:max])
  22. end
  23. end