Sha256: cff15cdee22f6aa051f5ac8a598a02ec88a51791ac079f4f38a8bd441ec3fe97
Contents?: true
Size: 1.54 KB
Versions: 1
Compression:
Stored size: 1.54 KB
Contents
require 'action_logic/errors' require 'action_logic/action_validation/base_validation' module ActionLogic module ActionValidation class PresenceValidation < BaseValidation def self.validate!(validation_rules, context) return unless validation_rules.values.find { |expected_validation| expected_validation[:presence] } errors = presence_errors(validation_rules, context) raise ActionLogic::PresenceError.new(errors) if errors.any? end def self.presence_errors(validation_rules, context) validation_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| next error_collection unless expected_validation[:presence] error_collection << error_message(expected_attribute, expected_validation, context) error_collection end || [] end def self.error_message(expected_attribute, expected_validation, context) case expected_validation[:presence] when TrueClass then "Attribute: #{expected_attribute} is missing value in context but presence validation was specified" unless context[expected_attribute] when Proc then "Attribute: #{expected_attribute} is missing value in context but custom presence validation was specified" unless expected_validation[:presence].call(context[expected_attribute]) else raise ActionLogic::UnrecognizablePresenceValidatorError.new(error_message_format("Presence validator: #{expected_validation[:presence]} is not a supported format")) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
action_logic-0.3.3 | lib/action_logic/action_validation/presence_validation.rb |