Class: Dry::Types::Hash::Strict

Inherits:
Permissive show all
Defined in:
lib/dry/types/hash/schema.rb

Overview

Strict hash will raise errors when keys are missing or value types are incorrect. Strict schema raises a UnknownKeysError if there are any unexpected keys in given hash, and raises a MissingKeyError if any key is missing in it.

Examples:

hash = Types::Hash.strict(name: Types::String, age: Types::Coercible::Int)
hash[email: '[email protected]', name: 'Jane', age: 21]
  # => Dry::Types::SchemaKeyError: :email is missing in Hash input

Direct Known Subclasses

StrictWithDefaults

Instance Attribute Summary

Attributes inherited from Schema

#member_types

Attributes inherited from Definition

#options, #primitive

Attributes included from Options

#options

Instance Method Summary collapse

Methods inherited from Permissive

#resolve_missing_value

Methods inherited from Schema

#call, #coerce, #initialize, #resolve_missing_value, #try, #try_coerce

Methods included from MaybeTypes

#resolve_missing_value

Methods inherited from Dry::Types::Hash

#permissive, #resolve_missing_value, #schema, #strict, #strict_with_defaults, #symbolized, #weak

Methods inherited from Definition

[], #call, #constrained?, #default?, #failure, #initialize, #name, #primitive?, #result, #success, #try

Methods included from Builder

#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Options

#initialize, #meta, #with

Constructor Details

This class inherits a constructor from Dry::Types::Hash::Schema

Instance Method Details

#resolve(hash) ⇒ Hash{Symbol => Object} (private)

Parameters:

Returns:

  • (Hash{Symbol => Object})

Raises:



138
139
140
141
142
143
144
145
146
147
# File 'lib/dry/types/hash/schema.rb', line 138

def resolve(hash)
  unexpected = hash.keys - member_types.keys
  raise UnknownKeysError.new(*unexpected) unless unexpected.empty?

  super do |member_type, key, value|
    type = member_type.default? ? member_type.type : member_type

    yield(type, key, value)
  end
end