Sha256: 26e26cf650b954f99a2ab311dc1a58722232d5821615f85ee20093ed386c9b5e

Contents?: true

Size: 883 Bytes

Versions: 1

Compression:

Stored size: 883 Bytes

Contents

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

module Membrane
  module Schema
  end
end

class Membrane::Schema::Dictionary
  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.1 lib/membrane/schema/dictionary.rb