verify_perform_rules_validator.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Validations::VerifyPerformRulesValidator < ActiveModel::EachValidator
  3. CHECK_PRESENT = {
  4. 'article.note' => %w[body subject internal],
  5. 'notification.email' => %w[body recipient subject],
  6. 'notification.sms' => %w[body recipient],
  7. 'notification.webhook' => %w[webhook_id],
  8. 'x-zammad-ticket-owner_id' => %w[value], # PostmasterFilter
  9. 'x-zammad-ticket-customer_id' => %w[value], # PostmasterFilter
  10. }.freeze
  11. CHECK_SPECIFIC_PRESENT = %w[
  12. ticket.customer_id
  13. ticket.organization_id
  14. ticket.owner_id
  15. ].freeze
  16. def validate_each(record, attribute, value)
  17. return if !value.is_a? Hash
  18. check_present(record, attribute, value)
  19. check_specific_present(record, attribute, value)
  20. end
  21. private
  22. def check_present(record, attribute, value)
  23. check_present_missing(value)
  24. .each do |key, inner|
  25. add_error(record, attribute, key, inner)
  26. end
  27. end
  28. def check_present_missing(value)
  29. CHECK_PRESENT.each_with_object([]) do |(key, attrs), result|
  30. next if !value[key].is_a? Hash
  31. attrs.each do |attr|
  32. result << [key, attr] if value[key][attr].blank?
  33. end
  34. end
  35. end
  36. def check_specific_present(record, attribute, value)
  37. check_specific_present_missing(value)
  38. .each do |key|
  39. add_error(record, attribute, key, 'value')
  40. end
  41. end
  42. def check_specific_present_missing(value)
  43. CHECK_SPECIFIC_PRESENT.each_with_object([]) do |key, result|
  44. next if !value[key].is_a? Hash
  45. next if value[key]['pre_condition'] != 'specific'
  46. result << key if value[key]['value'].blank?
  47. end
  48. end
  49. def add_error(record, attribute, key, inner)
  50. record.errors.add :base, __("The required '%{attribute}' value for %{key}, %{inner} is missing!"), attribute: attribute, key: key, inner: inner
  51. end
  52. end