Class: ActiveModel::Validations::MustBeValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::MustBeValidator
- Defined in:
- lib/validators/must_be_validator.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- MATCHERS =
{ equal_to: :==, greater_than: :>, greater_or_equal_to: :>=, less_than: :<, less_or_equal_to: :<=, not_equal_to: :!=, matching: :=~, }.freeze
- REQD_OPTS =
(MATCHERS.keys + [:blank, :present, :one_of, :not_any_of, :only_from, :before, :after])
- MESSAGE_PREFIX =
"must be"
- ERRORS_METHOD =
:errors
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/validators/must_be_validator.rb', line 21 def validate_each(record, attribute, value) raise "Requires at least one comparison operator for attribute." unless (.keys & REQD_OPTS).length > 0 return if [:rule_name] && BeValid.config.rules&.fetch([:rule_name], {})[:disabled] original_value = value original_value = record.read_attribute_before_type_cast( attribute ) rescue nil # only in rails = self.class::MESSAGE_PREFIX.dup return if [:blank] && value.blank? << " blank" if [:blank] return if [:present] && value.present? << " present" if [:present] return if [:one_of] && Array([:one_of]).flatten.include?(value) = ": '#{value}' is not a valid value" if [:one_of] return if [:not_any_of] && !([:not_any_of].include?(value)) = ": '#{value}' is not a valid value" if [:not_any_of] return if [:only_from] && ([:only_from] & Array(value) == Array(value)) = ": #{Array(value).join(",").gsub('"', '\'')} is not a valid value" if [:only_from] # handle before and after date comparisons using date validator if [:before] before_resp = DateValidator.new(.merge(attributes: attributes)).validate_before_option(record, attribute, value, original_value) return if before_resp == true = before_resp end if [:after] after_resp = DateValidator.new(.merge(attributes: attributes)).validate_after_option(record, attribute, value, original_value) return if after_resp == true = after_resp end .slice(*MATCHERS.keys).each do |key, operand| operand_msg = operand.is_a?(Regexp) ? operand.inspect : operand.to_s operand = record.send(operand) if operand.is_a? Symbol return if operand.nil? return if value&.send(MATCHERS[key], operand) return if key == :not_equal_to && value != operand << " #{key.to_s.humanize(capitalize: false)} #{operand_msg}" end if [:when].present? # check if conditions to validate satisfied conditions = [:when] raise "Invalid :when option provided to must_be, must be a Hash or method" unless (conditions.is_a? Hash || conditions.is_a?(Method)) # add error message with predicate info << " when " condition_errors = [] conditions.each do |field, values| field_value = record.send(field) case values when Regexp return if !values.match?(field_value) condition_errors << "#{field} = #{field_value}" when Array return if !values.flatten.include?(field_value) condition_errors << "#{field} = #{field_value}" when :blank return unless record.send(field).blank? condition_errors << "#{field} is #{values.to_s.gsub('?', '')}" when :present return unless record.send(field).present? condition_errors << "#{field} is #{values.to_s.gsub('?', '')}" when Symbol return if !record.send(field)&.send(values) condition_errors << "#{field} is #{values.to_s.gsub('?', '')}" else return if values != field_value condition_errors << "#{field} = #{field_value}" end end << condition_errors.join(' and ') end if [:one_of] && .fetch(:show_values, true) << ". Valid values: #{[:one_of].join(', ')}" end if [:only_from] && .fetch(:show_values, true) << ". Valid values: #{[:only_from].join(', ')}" end if [:not_any_of] && .fetch(:show_values, true) << ". Invalid values: #{[:not_any_of].join(', ')}" end record.send(self.class::ERRORS_METHOD).add(attribute, ([:message] || "#{}."), rule_name: [:rule_name]) end |