Sha256: 6a50d44566a956e74b503d2123bfbe913c1cabeba7424b844decf301c6036835
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
require "membrane/errors" require "membrane/schemas/base" module Membrane module Schema end end class Membrane::Schemas::List < Membrane::Schemas::Base attr_reader :elem_schema def initialize(elem_schema) @elem_schema = elem_schema end def validate(object) ArrayValidator.new(object).validate MemberValidator.new(@elem_schema, object).validate 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 MemberValidator def initialize(elem_schema, object) @elem_schema = elem_schema @object = object end def validate errors = {} @object.each_with_index do |elem, ii| begin @elem_schema.validate(elem) rescue Membrane::SchemaValidationError => e errors[ii] = e.to_s end end fail!(errors) if errors.size > 0 end def fail!(errors) emsg = errors.map { |ii, e| "At index #{ii}: #{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.4 | lib/membrane/schemas/list.rb |