data_option_validator.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ObjectManager::Attribute::DataOptionValidator < ActiveModel::Validator
  3. VALIDATE_INTEGER_MIN = -2_147_483_647
  4. VALIDATE_INTEGER_MAX = 2_147_483_647
  5. VALIDATE_UNSIGNED_INTEGER_REGEXP = %r{^\d+$}
  6. INPUT_DATA_TYPES = %w[text password tel fax email url].freeze
  7. def validate(record)
  8. case record.data_type
  9. when 'input'
  10. type_check(record)
  11. maxlength_check(record)
  12. when %r{^(textarea|richtext)$}
  13. maxlength_check(record)
  14. when 'integer'
  15. min_max_check(record)
  16. when %r{^((multi_)?tree_select|(multi)?select|checkbox)$}
  17. default_check(record)
  18. relation_check(record)
  19. when 'boolean'
  20. default_check(record)
  21. presence_check(record)
  22. when 'datetime'
  23. future_check(record)
  24. past_check(record)
  25. end
  26. end
  27. private
  28. def maxlength_check(record)
  29. return if VALIDATE_UNSIGNED_INTEGER_REGEXP.match?(record.local_data_option[:maxlength].to_s)
  30. record.errors.add :base, __('Max length must be an integer.')
  31. end
  32. def type_check(record)
  33. return if INPUT_DATA_TYPES.include? record.local_data_option[:type]
  34. record.errors.add :base, __('Input field must be text, password, tel, fax, email or url type.')
  35. end
  36. def default_check(record)
  37. return if record.local_data_option.key?(:default)
  38. record.errors.add :base, __('Default value is required.')
  39. end
  40. def relation_check(record)
  41. return if !record.local_data_option[:options].nil? || !record.local_data_option[:relation].nil?
  42. record.errors.add :base, __('Options or relation is required.')
  43. end
  44. def presence_check(record)
  45. return if !record.local_data_option[:options].nil?
  46. record.errors.add :base, __('Options are required.')
  47. end
  48. def future_check(record)
  49. return if !record.local_data_option[:future].nil?
  50. record.errors.add :base, __('Allow future dates toggle value is required.')
  51. end
  52. def past_check(record)
  53. return if !record.local_data_option[:past].nil?
  54. record.errors.add :base, __('Allow past dates toggle value is required.')
  55. end
  56. def min_max_check(record)
  57. min_ok = min_max_validate_min(record)
  58. max_ok = min_max_validate_max(record)
  59. return if !min_ok || !max_ok
  60. min_max_validate_range(record)
  61. end
  62. def min_max_validate_min(record)
  63. min = record.local_data_option[:min]
  64. if !min.is_a?(Integer)
  65. record.errors.add :base, __('Minimal value must be an integer')
  66. return
  67. end
  68. if min < VALIDATE_INTEGER_MIN
  69. record.errors.add :base, __('Minimal value must be higher than -2147483648')
  70. return
  71. end
  72. if min > VALIDATE_INTEGER_MAX
  73. record.errors.add :base, __('Minimal value must be lower than 2147483648')
  74. return
  75. end
  76. true
  77. end
  78. def min_max_validate_max(record)
  79. max = record.local_data_option[:max]
  80. if !max.is_a?(Integer)
  81. record.errors.add :base, __('Maximal value must be an integer')
  82. return
  83. end
  84. if max < VALIDATE_INTEGER_MIN
  85. record.errors.add :base, __('Maximal value must be higher than -2147483648')
  86. return
  87. end
  88. if max > VALIDATE_INTEGER_MAX
  89. record.errors.add :base, __('Maximal value must be lower than 2147483648')
  90. return
  91. end
  92. true
  93. end
  94. def min_max_validate_range(record)
  95. min = record.local_data_option[:min]
  96. max = record.local_data_option[:max]
  97. return if min.is_a?(Integer) && max.is_a?(Integer) && min <= max
  98. record.errors.add :base, __('Maximal value must be higher than or equal to minimal value')
  99. end
  100. end