class Thy::Types::Hash

Public Class Methods

new(schema) click to toggle source
# File lib/thy/types/hash.rb, line 6
def initialize(schema)
  @schema = schema
  @schema_length = schema.length
  @schema_keys = schema.keys
end

Public Instance Methods

check(value) click to toggle source
# File lib/thy/types/hash.rb, line 12
def check(value)
  unless value.is_a?(::Hash)
    return Result::Failure.new("Expected a Hash, but got: #{value.inspect}")
  end
  if @schema_length != value.length
    return Result::Failure.new("Expected #{value.inspect} to contain #{@schema_length} keys")
  end
  if @schema_keys & value.keys != @schema_keys
    return Result::Failure.new(
      "Expected #{value.inspect} to contain keys: #{@schema_keys.inspect}",
    )
  end

  @schema.each do |k, t|
    v = value.fetch(k)

    if t.check(v).failure?
      return Result::Failure.new("Expected #{v.inspect} to be of type #{t.inspect}")
    end
  end

  Result::Success
end