Sha256: 5109cd5f6b484f109063979498cd9f91a08e1ba6115b834f090f6890006e35de
Contents?: true
Size: 1.84 KB
Versions: 1
Compression:
Stored size: 1.84 KB
Contents
require "membrane/errors" require "membrane/schemas/base" module Membrane module Schema end end class Membrane::Schemas::Tuple < Membrane::Schemas::Base attr_reader :elem_schemas def initialize(*elem_schemas) @elem_schemas = elem_schemas end def validate(object) ArrayValidator.new(object).validate LengthValidator.new(@elem_schemas, object).validate MemberValidator.new(@elem_schemas, object).validate nil end class ArrayValidator def initialize(object) @object = object end def validate fail! if !@object.kind_of?(Array) end private def fail! emsg = "Expected instance of Array, given instance of #{@object.class}" raise Membrane::SchemaValidationError.new(emsg) end end class LengthValidator def initialize(elem_schemas, object) @elem_schemas = elem_schemas @object = object end def validate expected = @elem_schemas.length actual = @object.length fail!(expected, actual) if actual != expected end private def fail!(expected, actual) emsg = "Expected #{expected} element(s), given #{actual}" raise Membrane::SchemaValidationError.new(emsg) end end class MemberValidator def initialize(elem_schemas, object) @elem_schemas = elem_schemas @object = object end def validate errors = {} @elem_schemas.each_with_index do |schema, ii| begin schema.validate(@object[ii]) rescue Membrane::SchemaValidationError => e errors[ii] = e end end fail!(errors) if errors.size > 0 end private def fail!(errors) emsg = "There were errors at the following indices: " \ + errors.map { |ii, err| "#{ii} => #{err}" }.join(", ") raise Membrane::SchemaValidationError.new(emsg) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
membrane-0.0.4 | lib/membrane/schemas/tuple.rb |