Sha256: 4fd25ad928f5c5d674f20318c0f734095d0fc50c7ee1a6d6d19f7300d15746fc

Contents?: true

Size: 1.45 KB

Versions: 44

Compression:

Stored size: 1.45 KB

Contents

module Avromatic
  module Model
    module Validation
      extend ActiveSupport::Concern

      module ClassMethods

        # Returns an array of messages
        def validate_nested_value(value)
          case value
          when Avromatic::Model::Attributes
            validate_record_value(value)
          when Array
            value.flat_map.with_index do |element, index|
              validate_nested_value(element).map do |message|
                "[#{index}]#{message}"
              end
            end
          when Hash
            value.flat_map do |key, map_value|
              # keys for the Avro map type are always strings and do not require
              # validation
              validate_nested_value(map_value).map do |message|
                "['#{key}']#{message}"
              end
            end
          else
            []
          end
        end

        private

        def validate_complex(field_name)
          validate do |instance|
            value = instance.send(field_name)
            messages = self.class.validate_nested_value(value)
            messages.each { |message| instance.errors.add(field_name.to_sym, message) }
          end
        end

        def validate_record_value(record)
          if record && record.invalid?
            record.errors.map do |key, message|
              ".#{key} #{message}".gsub(' .', '.')
            end
          else
            []
          end
        end
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
avromatic-0.19.0 lib/avromatic/model/validation.rb
avromatic-0.18.1 lib/avromatic/model/validation.rb
avromatic-0.18.0 lib/avromatic/model/validation.rb
avromatic-0.18.0.rc0 lib/avromatic/model/validation.rb
avromatic-0.17.1 lib/avromatic/model/validation.rb
avromatic-0.17.0 lib/avromatic/model/validation.rb
avromatic-0.15.1 lib/avromatic/model/validation.rb
avromatic-0.15.0 lib/avromatic/model/validation.rb
avromatic-0.15.0.rc0 lib/avromatic/model/validation.rb
avromatic-0.14.0 lib/avromatic/model/validation.rb
avromatic-0.14.0.rc1 lib/avromatic/model/validation.rb
avromatic-0.14.0.rc0 lib/avromatic/model/validation.rb
avromatic-0.13.0 lib/avromatic/model/validation.rb
avromatic-0.13.0.rc1 lib/avromatic/model/validation.rb
avromatic-0.13.0.rc0 lib/avromatic/model/validation.rb
avromatic-0.12.0 lib/avromatic/model/validation.rb
avromatic-0.11.2 lib/avromatic/model/validation.rb
avromatic-0.11.1 lib/avromatic/model/validation.rb
avromatic-0.11.0 lib/avromatic/model/validation.rb
avromatic-0.10.0 lib/avromatic/model/validation.rb