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-1.0.0 lib/avromatic/model/validation.rb
avromatic-0.33.0 lib/avromatic/model/validation.rb
avromatic-0.32.0 lib/avromatic/model/validation.rb
avromatic-0.32.0.rc0 lib/avromatic/model/validation.rb
avromatic-0.31.0 lib/avromatic/model/validation.rb
avromatic-0.30.0 lib/avromatic/model/validation.rb
avromatic-0.29.1 lib/avromatic/model/validation.rb
avromatic-0.29.0 lib/avromatic/model/validation.rb
avromatic-0.28.1 lib/avromatic/model/validation.rb
avromatic-0.27.0 lib/avromatic/model/validation.rb
avromatic-0.26.0 lib/avromatic/model/validation.rb
avromatic-0.25.0 lib/avromatic/model/validation.rb
avromatic-0.24.0 lib/avromatic/model/validation.rb
avromatic-0.23.0 lib/avromatic/model/validation.rb
avromatic-0.22.0 lib/avromatic/model/validation.rb
avromatic-0.21.1 lib/avromatic/model/validation.rb
avromatic-0.21.0 lib/avromatic/model/validation.rb
avromatic-0.21.0.rc1 lib/avromatic/model/validation.rb
avromatic-0.21.0.rc0 lib/avromatic/model/validation.rb
avromatic-0.20.0 lib/avromatic/model/validation.rb