Sha256: 0f6ceff3d6cf37c7607ff4031c9e37c7e22114324f5647964f2fd128b70609d7

Contents?: true

Size: 751 Bytes

Versions: 5

Compression:

Stored size: 751 Bytes

Contents

# frozen_string_literal: true

require "active_record"
require "store_model/combine_errors_strategies"

module ActiveModel
  module Validations
    class StoreModelValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        if value.nil?
          record.errors.add(attribute, :blank)
          return
        end

        case record.type_for_attribute(attribute).type
        when :json
          strategy.call(attribute, record.errors, value.errors) if value.invalid?
        when :array
          record.errors.add(attribute, :invalid) if value.any?(&:invalid?)
        end
      end

      private

      def strategy
        StoreModel::CombineErrorsStrategies.configure(options)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
store_model-0.5.1 lib/active_model/validations/store_model_validator.rb
store_model-0.5.0 lib/active_model/validations/store_model_validator.rb
store_model-0.4.1 lib/active_model/validations/store_model_validator.rb
store_model-0.4.0 lib/active_model/validations/store_model_validator.rb
store_model-0.3.2 lib/active_model/validations/store_model_validator.rb