Sha256: 4e7f8b079530568066512076fe6761389225b5d8122165ec335a82e435b2cd0e
Contents?: true
Size: 1.14 KB
Versions: 1
Compression:
Stored size: 1.14 KB
Contents
module Compel class Contract attr_reader :errors, :serialized_errors def initialize(hash, schema) if hash.nil? || !Coercion.valid?(hash, Hash) raise Compel::TypeError, 'must be an Hash' end @hash = Hashie::Mash.new(hash) @schema = schema @coerced_hash = Hashie::Mash.new end def validate validator = Validators::HashValidator.new(@hash, @schema).validate @errors = validator.errors @coerced_hash = validator.output self end def coerced_hash # @hash has all params that are not affected by the validation @hash.merge(@coerced_hash) end def serialize coerced_hash.tap do |hash| if !valid? hash[:errors] = serialized_errors end end end def valid? @errors.empty? end def serialized_errors @errors.to_hash end def raise? if !valid? exception = InvalidHashError.new exception.object = coerced_hash exception.errors = serialized_errors raise exception, 'hash has errors' end coerced_hash end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
compel-0.2.0 | lib/compel/contract.rb |