Class: CommandTower::Messaging::NotificationTypes::DeclarationValidator
- Inherits:
-
Object
- Object
- CommandTower::Messaging::NotificationTypes::DeclarationValidator
- Defined in:
- app/services/command_tower/messaging/notification_types/declaration_validator.rb
Constant Summary collapse
- IDENTIFIER_FORMAT =
Lowercase alphanumeric segments separated by "." or "_". Compatible with existing keys such as "hello_world" and "booking.success".
/\A[a-z][a-z0-9]*(?:[._][a-z][a-z0-9]*)*\z/
Class Method Summary collapse
- .category_consistency_errors(declaration, peers:) ⇒ Object
- .validate(declaration) ⇒ Object
- .validate!(declaration) ⇒ Object
- .validate_category_consistency!(declaration, peers:) ⇒ Object
Class Method Details
.category_consistency_errors(declaration, peers:) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/services/command_tower/messaging/notification_types/declaration_validator.rb', line 85 def self.category_consistency_errors(declaration, peers:) return [] if declaration.nil? || declaration.category_key.nil? || declaration.category_key.strip.empty? peers = Array(peers) conflicting = peers.select { |peer| peer.category_key == declaration.category_key } return [] if conflicting.empty? errors = [] peer = conflicting.first if peer.category_label != declaration.category_label errors << "category_key #{declaration.category_key.inspect} has conflicting category_label " \ "(#{declaration.category_label.inspect} vs #{peer.category_label.inspect})" end if peer.category_order != declaration.category_order errors << "category_key #{declaration.category_key.inspect} has conflicting category_order " \ "(#{declaration.category_order.inspect} vs #{peer.category_order.inspect})" end errors end |
.validate(declaration) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/services/command_tower/messaging/notification_types/declaration_validator.rb', line 18 def self.validate(declaration) errors = [] if declaration.nil? errors << "declaration is required" return errors end validate_identifier_field(errors, declaration.key, field: "key", allow_blank_message: "key must be present") validate_non_blank_string(errors, declaration.label, field: "label") validate_identifier_field( errors, declaration.category_key, field: "category_key", allow_blank_message: "category_key must be present", ) validate_non_blank_string(errors, declaration.category_label, field: "category_label") validate_non_negative_integer(errors, declaration.category_order, field: "category_order") validate_non_negative_integer(errors, declaration.type_order, field: "type_order") if declaration.allowed_channels.nil? errors << "allowed_channels is required" elsif !declaration.allowed_channels.is_a?(Array) errors << "allowed_channels must be an array" end if declaration.default_channels.nil? errors << "default_channels is required" elsif !declaration.default_channels.is_a?(Array) errors << "default_channels must be an array" end if declaration.allowed_channels.is_a?(Array) validate_channel_list(errors, declaration.allowed_channels, field: "allowed_channels") end if declaration.default_channels.is_a?(Array) validate_channel_list(errors, declaration.default_channels, field: "default_channels") end if declaration.allowed_channels.is_a?(Array) && declaration.default_channels.is_a?(Array) extras = declaration.default_channels - declaration.allowed_channels if extras.any? errors << "default_channels must be a subset of allowed_channels (extras: #{extras.join(', ')})" end end if declaration.default_preference_state.nil? errors << "default_preference_state is required" elsif !declaration.default_preference_state.is_a?(Hash) errors << "default_preference_state must be a hash" end unless [true, false].include?(declaration.settings_visible) errors << "settings_visible must be a boolean" end errors end |
.validate!(declaration) ⇒ Object
11 12 13 14 15 16 |
# File 'app/services/command_tower/messaging/notification_types/declaration_validator.rb', line 11 def self.validate!(declaration) errors = validate(declaration) return if errors.empty? raise InvalidDeclarationError, errors.join("; ") end |
.validate_category_consistency!(declaration, peers:) ⇒ Object
78 79 80 81 82 83 |
# File 'app/services/command_tower/messaging/notification_types/declaration_validator.rb', line 78 def self.validate_category_consistency!(declaration, peers:) errors = category_consistency_errors(declaration, peers:) return if errors.empty? raise InvalidDeclarationError, errors.join("; ") end |