Sha256: 78d4aa7fa46daccb31a7b46c8471f2b052069004c81ef7c04b3ae462f6d23743

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require "membrane/errors"
require "membrane/schema/base"

module Membrane
  module Schema
  end
end

class Membrane::Schema::Tuple < Membrane::Schema::Base
  attr_reader :elem_schemas

  def initialize(*elem_schemas)
    @elem_schemas = elem_schemas
  end

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

    expected = @elem_schemas.length
    actual = object.length

    if actual != expected
      emsg = "Expected #{expected} element(s), given #{actual}"
      raise Membrane::SchemaValidationError.new(emsg)
    end

    errors = {}

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

    if errors.size > 0
      emsg = "There were errors at the following indices: " \
             + errors.map { |ii, err| "#{ii} => #{err}" }.join(", ")
      raise Membrane::SchemaValidationError.new(emsg)
    end

    nil
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
membrane-0.0.2 lib/membrane/schema/tuple.rb
membrane-0.0.1 lib/membrane/schema/tuple.rb