Module: SlackValidBlockKit::Validator::Base
- Included in:
- Runner
- Defined in:
- lib/slack_valid_block_kit/validator/base.rb
Constant Summary collapse
- MULTI_SELECT_TYPES =
%w(multi_static_select multi_external_select multi_users_select multi_conversations_select multi_channels_select)
- SELECT_TYPES =
%w(static_select external_select users_select conversations_select channels_select)
- ACTION_ELEMENT_TYPES =
%w(button overflow datepicker radio_buttons checkboxes timepicker) + SELECT_TYPES + MULTI_SELECT_TYPES
- INPUT_ELEMENT_TYPES =
%w(checkboxes datepicker plain_text_input radio_buttons timepicker) + SELECT_TYPES + MULTI_SELECT_TYPES
- ALL_ELEMENT_TYPES =
%w(button overflow datepicker radio_buttons checkboxes timepicker plain_text_input image) + SELECT_TYPES + MULTI_SELECT_TYPES
- LAYOUT_BLOCKS_FOR_MODAL =
%w(actions context divider header image input section)
- LAYOUT_BLOCKS_FOR_HOME =
LAYOUT_BLOCKS_FOR_MODAL- LAYOUT_BLOCKS_FOR_MESSAGE =
LAYOUT_BLOCKS_FOR_MODAL + %w(file)
- BLOCK_KIT_GROUPS =
{ action_elements: ACTION_ELEMENT_TYPES, input_elements: INPUT_ELEMENT_TYPES, all_element_types: ALL_ELEMENT_TYPES, plain_text: ["plain_text"], text_objects: %w(plain_text mrkdwn), image_or_text: %(plain_text mrkdwn image), layout_blocks_for_modal: LAYOUT_BLOCKS_FOR_MODAL, layout_blocks_for_home: LAYOUT_BLOCKS_FOR_HOME, layout_blocks_for_message: LAYOUT_BLOCKS_FOR_MESSAGE, modal: ["modal"], top: %w(modal home blocks) }
Instance Method Summary collapse
- #blank?(v) ⇒ Boolean
- #countable?(v) ⇒ Boolean
- #present?(v) ⇒ Boolean
- #validate(obj, path, types, options = nil) ⇒ Object
- #validate_for(obj, path, clazz_type, required, only: nil, max: nil, min: nil) ⇒ Object
- #validate_for_action_id(action_id, path) ⇒ Object
- #validate_for_block_id(block_id, path) ⇒ Object
- #validate_for_focus_on_load(obj, path) ⇒ Object
- #validate_for_plain_text(obj, path, required, size) ⇒ Object
- #validate_for_properties(obj, path, properties) ⇒ Object
- #validate_for_text_objects(obj, path, required, size) ⇒ Object
- #validate_for_types(type, path, types) ⇒ Object
Instance Method Details
#blank?(v) ⇒ Boolean
91 92 93 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 91 def blank?(v) v.nil? || (countable?(v) ? v.size.zero? : false) end |
#countable?(v) ⇒ Boolean
99 100 101 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 99 def countable?(v) v.is_a?(Array) || v.is_a?(Hash) || v.is_a?(String) end |
#present?(v) ⇒ Boolean
95 96 97 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 95 def present?(v) v.respond_to?(:size) ? !v.size.zero? : !v.nil? end |
#validate(obj, path, types, options = nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 29 def validate(obj, path, types, = nil) return if obj.nil? type = obj[:type] return unless validate_for_types(type, "#{path}.type", types) self.send("validate_#{type}", obj, "#{path}(#{type})", ) errors_by_path end |
#validate_for(obj, path, clazz_type, required, only: nil, max: nil, min: nil) ⇒ Object
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 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 37 def validate_for(obj, path, clazz_type, required, only: nil, max: nil, min: nil) if obj.nil? add_error(path, :required) if required return end if blank?(obj) add_error(path, :no_item) return end if clazz_type == Array if !obj.is_a?(Array) add_error(path, :clazz_type, Array) end elsif clazz_type == :bool if !obj.is_a?(TrueClass) && !obj.is_a?(FalseClass) add_error(path, :clazz_type, :bool) end elsif clazz_type == :array_of_string if !obj.is_a?(Array) || obj.reject { |v| v.class == String }.size > 0 add_error(path, :clazz_type, :array_of_string) end else if !obj.is_a?(clazz_type) add_error(path, :clazz_type, clazz_type) end end if !only.nil? if obj.is_a?(Array) add_error(path, :only, only) if (obj - only).size > 0 else add_error(path, :only, only) if !only.include?(obj) end end if !max.nil? if obj.is_a?(Numeric) add_error(path, :max, max) if obj > max else add_error(path, :max_size, max) if obj.size > max end end if !min.nil? if obj.is_a?(Numeric) add_error(path, :min, min) if obj < min else add_error(path, :min_size, min) if obj.size < min end end end |
#validate_for_action_id(action_id, path) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 136 def validate_for_action_id(action_id, path) validate_for(action_id, path, String, true, max: 255) unless action_id.nil? path_by_action_id[action_id] ||= [] path_by_action_id[action_id] << path end end |
#validate_for_block_id(block_id, path) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 128 def validate_for_block_id(block_id, path) validate_for(block_id, path, String, false, max: 255) unless block_id.nil? path_by_block_id[block_id] ||= [] path_by_block_id[block_id] << path end end |
#validate_for_focus_on_load(obj, path) ⇒ Object
154 155 156 157 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 154 def validate_for_focus_on_load(obj, path) validate_for(obj, path, :bool, false) focus_on_load_by_path[path] = true if obj end |
#validate_for_plain_text(obj, path, required, size) ⇒ Object
144 145 146 147 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 144 def validate_for_plain_text(obj, path, required, size) validate_for(obj, path, Hash, required) validate(obj, path, :plain_text, { text_size: size }) end |
#validate_for_properties(obj, path, properties) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 118 def validate_for_properties(obj, path, properties) other_properties = obj.keys - properties if present?(other_properties) add_error(path, :invalid_properties, other_properties) false else true end end |
#validate_for_text_objects(obj, path, required, size) ⇒ Object
149 150 151 152 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 149 def validate_for_text_objects(obj, path, required, size) validate_for(obj, path, Hash, required) validate(obj, path, :text_objects, { text_size: size }) if present?(obj) end |
#validate_for_types(type, path, types) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/slack_valid_block_kit/validator/base.rb', line 103 def validate_for_types(type, path, types) if type.nil? add_error(path, :required) return false end return true if types.nil? if !BLOCK_KIT_GROUPS[types].include?(type.to_s) add_error(path, :invalid_type) return false end true end |