Class: Slackify::Handlers::Validator
- Inherits:
-
Object
- Object
- Slackify::Handlers::Validator
- Defined in:
- lib/slackify/handlers/validator.rb
Overview
Simple validator for handlers. It will blow your app up on the configuration step instead of crashing when handling production requests.
Constant Summary collapse
- VALID_PARAMETER_TYPES =
[:string, :int, :boolean, :float].freeze
Class Method Summary collapse
- .validate_parameters(parameters) ⇒ Object
-
.verify_handler_integrity(handler) ⇒ Object
Checks if your handler hash is valid.
Class Method Details
.validate_parameters(parameters) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/slackify/handlers/validator.rb', line 67 def validate_parameters(parameters) errors = [] parameters.each do |parameter| key = parameter.keys[0] type = parameter.values[0] next if VALID_PARAMETER_TYPES.include?(type.to_sym) type.constantize next if Slackify::Parameter.supported_parameters.include?(type) errors << "Invalid parameter type for: #{key}, '#{type}'.\n"\ "If this is a custom parameter, make sure it inherits from Slackify::Parameter" rescue NameError errors << "Failed to find the custom class for: #{key}, '#{type}'." end errors end |
.verify_handler_integrity(handler) ⇒ Object
Checks if your handler hash is valid. It’s pass or raise 🧨💥
12 13 14 15 16 17 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 |
# File 'lib/slackify/handlers/validator.rb', line 12 def verify_handler_integrity(handler) handler_name = handler.keys.first handler_class = handler_name.camelize.constantize unless handler[handler_name].key?('commands') && handler.dig(handler_name, 'commands')&.any? raise Exceptions::InvalidHandler, "#{handler_name} doesn't have any command specified" end handler_errors = [] handler.dig(handler_name, 'commands').each do |command| command_errors = [] unless command['regex'] || command['base_command'] command_errors.append('No regex or base command was provided.') end if command['regex'].present? if command['base_command'].present? command_errors.append('Regex and base_command cannot be used in the same handler.') end if command['parameters'].present? command_errors.append('Regex and parameters cannot be used in the same handler.') end unless command['regex'].is_a?(Regexp) command_errors.append('No regex was provided.') end end if command['base_command'] unless command['base_command'].is_a?(String) command_errors.append('Invalid base command provided, it must be a string.') end if command['parameters'].present? command_errors << validate_parameters(command['parameters']) end end unless !command['action'].to_s.strip.empty? && handler_class.respond_to?(command['action']) command_errors.append('No valid action was provided.') end command_errors = command_errors.flatten.compact handler_errors.append("[#{command['name']}]: #{command_errors.join(' ')}") unless command_errors.empty? end unless handler_errors.empty? raise Exceptions::InvalidHandler, "#{handler_name} is not valid: #{handler_errors.join(' ')}" end rescue NameError raise Exceptions::InvalidHandler, "#{handler_name} is not defined" end |