class ActionLogic::ActionValidation::TypeValidation

Public Class Methods

validate!(validation_rules, context) click to toggle source
# File lib/action_logic/action_validation/type_validation.rb, line 8
def self.validate!(validation_rules, context)
  return unless validation_rules.values.find { |expected_validation| expected_validation[:type] }

  type_errors = validation_rules.reduce([]) do |collection, (expected_attribute, expected_validation)|
    next collection unless expected_validation[:type]

    if context.to_h[expected_attribute].class != expected_validation[:type]
      collection << "Attribute: #{expected_attribute} with value: #{context.to_h[expected_attribute]} was expected to be of type #{expected_validation[:type]} but is #{context.to_h[expected_attribute].class}"
    end
    collection
  end

  raise ActionLogic::AttributeTypeError.new(error_message_format(type_errors.join(", "))) if type_errors.any?
end