Sha256: 90b68dbcb79505a00c43816ba16de120db460c04c3d7a98f8a72382dd6715e11
Contents?: true
Size: 1.02 KB
Versions: 2
Compression:
Stored size: 1.02 KB
Contents
require "set" require "membrane/errors" require "membrane/schema/base" module Membrane module Schema end end class Membrane::Schema::Record < Membrane::Schema::Base attr_reader :schemas attr_reader :optional_keys def initialize(schemas, optional_keys = []) @optional_keys = Set.new(optional_keys) @schemas = schemas end def validate(object) unless object.kind_of?(Hash) emsg = "Expected instance of Hash, given instance of #{object.class}" raise Membrane::SchemaValidationError.new(emsg) end key_errors = {} @schemas.each do |k, schema| if object.has_key?(k) begin schema.validate(object[k]) rescue Membrane::SchemaValidationError => e key_errors[k] = e.to_s end elsif !@optional_keys.include?(k) key_errors[k] = "Missing key" end end if key_errors.size > 0 emsg = "{ " + key_errors.map { |k, e| "#{k} => #{e}" }.join(", ") + " }" raise Membrane::SchemaValidationError.new(emsg) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
membrane-0.0.2 | lib/membrane/schema/record.rb |
membrane-0.0.1 | lib/membrane/schema/record.rb |