Sha256: 3514e3c309a6c3e8c309482794c741317c66fc19a59e87f87db8289e3582bb83

Contents?: true

Size: 908 Bytes

Versions: 1

Compression:

Stored size: 908 Bytes

Contents

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

module Membrane
  module Schema
  end
end

class Membrane::Schema::Dictionary < Membrane::Schema::Base
  attr_reader :key_schema
  attr_reader :value_schema

  def initialize(key_schema, value_schema)
    @key_schema = key_schema
    @value_schema = value_schema
  end

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

    errors = {}

    object.each do |k, v|
      begin
        @key_schema.validate(k)
        @value_schema.validate(v)
      rescue Membrane::SchemaValidationError => e
        errors[k] = e.to_s
      end
    end

    if errors.size > 0
      emsg = "{ " + errors.map { |k, e| "#{k} => #{e}" }.join(", ") + " }"
      raise Membrane::SchemaValidationError.new(emsg)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
membrane-0.0.2 lib/membrane/schema/dictionary.rb