Sha256: e5c0af2addb0763418dbe6027ea3d136b22e3a26fa8d5ee86f83c8349085e7fd

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require "membrane/errors"
require "membrane/schemas/base"

module Membrane
  module Schema
  end
end

class Membrane::Schemas::List < Membrane::Schemas::Base
  attr_reader :elem_schema

  def initialize(elem_schema)
    @elem_schema = elem_schema
  end

  def validate(object)
    ArrayValidator.new(object).validate
    MemberValidator.new(@elem_schema, object).validate
  end

  class ArrayValidator
    def initialize(object)
      @object = object
    end

    def validate
      fail!(@object) if !@object.kind_of?(Array)
    end

    private

    def fail!(object)
      emsg = "Expected instance of Array, given instance of #{object.class}"
      raise Membrane::SchemaValidationError.new(emsg)
    end
  end

  class MemberValidator
    def initialize(elem_schema, object)
      @elem_schema = elem_schema
      @object = object
    end

    def validate
      errors = {}

      @object.each_with_index do |elem, ii|
        begin
          @elem_schema.validate(elem)
        rescue Membrane::SchemaValidationError => e
          errors[ii] = e.to_s
        end
      end

      fail!(errors) if errors.size > 0
    end

    def fail!(errors)
      emsg = errors.map { |ii, e| "At index #{ii}: #{e}" }.join(", ")
      raise Membrane::SchemaValidationError.new(emsg)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
membrane-1.1.0 lib/membrane/schemas/list.rb
membrane-1.0.0 lib/membrane/schemas/list.rb
membrane-0.0.5 lib/membrane/schemas/list.rb