Class: Dry::Types::Schema
- Includes:
- Enumerable
- Defined in:
- lib/dry/types/schema.rb,
lib/dry/types/schema/key.rb
Overview
Schema is a hash with explicit member types defined
Defined Under Namespace
Classes: Key
Constant Summary collapse
- NO_TRANSFORM =
Dry::Types::FnContainer.register { |x| x }
- SYMBOLIZE_KEY =
Dry::Types::FnContainer.register(:to_sym.to_proc)
Instance Attribute Summary collapse
- #keys ⇒ Array[Dry::Types::Schema::Key] readonly
- #name_key_map ⇒ Hash[Symbol, Dry::Types::Schema::Key] readonly
- #options ⇒ Hash included from Options readonly private
- #transform_key ⇒ #call readonly
Instance Method Summary collapse
- #apply(hash, options = EMPTY_HASH) ⇒ Hash{Symbol => Object}
- #call_safe(hash, options = EMPTY_HASH) ⇒ Hash{Symbol => Object} private
- #call_unsafe(hash, options = EMPTY_HASH) ⇒ Hash{Symbol => Object} private
-
#clear ⇒ Schema
Empty schema with the same options.
- #constrained? ⇒ Boolean
-
#each(&block) ⇒ Array<Dry::Types::Schema::Key>, Enumerator
Iterate over each key type.
-
#initialize(_primitive, **options) ⇒ Schema
constructor
private
A new instance of Schema.
-
#key(name, fallback = Undefined, &block) ⇒ Object
Fetch key type by a key name.
-
#key?(name) ⇒ Boolean
Whether the schema has the given key.
- #lax ⇒ Lax
-
#merge(other) ⇒ Schema
Merge given schema keys into current schema.
- #schema(keys_or_map) ⇒ Object
-
#strict(strict = true) ⇒ Schema
Make the schema intolerant to unknown keys.
-
#strict? ⇒ Boolean
Whether the schema rejects unknown keys.
-
#to_ast(meta: true) ⇒ Array
An AST representation.
-
#transform_keys? ⇒ Boolean
Whether the schema transforms input keys.
-
#try(input) {|failure| ... } ⇒ Logic::Result, Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity.
-
#with_key_transform(proc = nil, &block) ⇒ Schema
Inject a key transformation function.
Constructor Details
#initialize(_primitive, **options) ⇒ Schema
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Schema.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dry/types/schema.rb', line 41 def initialize(_primitive, **) @keys = .fetch(:keys) @name_key_map = keys.each_with_object({}) do |key, idx| idx[key.name] = key end key_fn = .fetch(:key_transform_fn, NO_TRANSFORM) @transform_key = Dry::Types::FnContainer[key_fn] super end |
Instance Attribute Details
#keys ⇒ Array[Dry::Types::Schema::Key] (readonly)
26 27 28 |
# File 'lib/dry/types/schema.rb', line 26 def keys @keys end |
#name_key_map ⇒ Hash[Symbol, Dry::Types::Schema::Key] (readonly)
29 30 31 |
# File 'lib/dry/types/schema.rb', line 29 def name_key_map @name_key_map end |
#options ⇒ Hash (readonly) Originally defined in module Options
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
#transform_key ⇒ #call (readonly)
32 33 34 |
# File 'lib/dry/types/schema.rb', line 32 def transform_key @transform_key end |
Instance Method Details
#apply(hash, options = EMPTY_HASH) ⇒ Hash{Symbol => Object}
80 81 82 |
# File 'lib/dry/types/schema.rb', line 80 def apply(hash, = EMPTY_HASH) call_unsafe(hash, ) end |
#call_safe(hash, options = EMPTY_HASH) ⇒ Hash{Symbol => Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
68 69 70 |
# File 'lib/dry/types/schema.rb', line 68 def call_safe(hash, = EMPTY_HASH) resolve_safe(coerce(hash) { return yield }, ) { return yield } end |
#call_unsafe(hash, options = EMPTY_HASH) ⇒ Hash{Symbol => Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 |
# File 'lib/dry/types/schema.rb', line 59 def call_unsafe(hash, = EMPTY_HASH) resolve_unsafe(coerce(hash), ) end |
#clear ⇒ Schema
Empty schema with the same options
298 299 300 |
# File 'lib/dry/types/schema.rb', line 298 def clear with(keys: EMPTY_ARRAY) end |
#constrained? ⇒ Boolean
270 271 272 |
# File 'lib/dry/types/schema.rb', line 270 def constrained? true end |
#each(&block) ⇒ Array<Dry::Types::Schema::Key>, Enumerator
Iterate over each key type
229 230 231 |
# File 'lib/dry/types/schema.rb', line 229 def each(&block) keys.each(&block) end |
#key(name, fallback = Undefined) ⇒ Dry::Types::Schema::Key, Object #key(name, &block) ⇒ Dry::Types::Schema::Key, Object
Fetch key type by a key name
Behaves as ::Hash#fetch
259 260 261 262 263 264 265 |
# File 'lib/dry/types/schema.rb', line 259 def key(name, fallback = Undefined, &block) if Undefined.equal?(fallback) name_key_map.fetch(name, &block) else name_key_map.fetch(name, fallback) end end |
#key?(name) ⇒ Boolean
Whether the schema has the given key
240 241 242 |
# File 'lib/dry/types/schema.rb', line 240 def key?(name) name_key_map.key?(name) end |
#lax ⇒ Lax
277 278 279 |
# File 'lib/dry/types/schema.rb', line 277 def lax Lax.new(schema(keys.map(&:lax))) end |
#merge(other) ⇒ Schema
Merge given schema keys into current schema
A new instance is returned.
289 290 291 |
# File 'lib/dry/types/schema.rb', line 289 def merge(other) schema(other.keys) end |
#schema(type_map, meta = EMPTY_HASH) ⇒ Dry::Types::Schema #schema(keys) ⇒ Dry::Types::Schema
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/dry/types/schema.rb', line 213 def schema(keys_or_map) if keys_or_map.is_a?(::Array) new_keys = keys_or_map else new_keys = build_keys(keys_or_map) end keys = merge_keys(self.keys, new_keys) Schema.new(primitive, **, keys: keys, meta: ) end |
#strict(strict = true) ⇒ Schema
Make the schema intolerant to unknown keys
172 173 174 |
# File 'lib/dry/types/schema.rb', line 172 def strict(strict = true) # rubocop:disable Style/OptionalBooleanParameter with(strict: strict) end |
#strict? ⇒ Boolean
Whether the schema rejects unknown keys
163 164 165 |
# File 'lib/dry/types/schema.rb', line 163 def strict? .fetch(:strict, false) end |
#to_ast(meta: true) ⇒ Array
Returns An AST representation.
149 150 151 152 153 154 155 156 |
# File 'lib/dry/types/schema.rb', line 149 def to_ast(meta: true) [ :schema, [keys.map { |key| key.to_ast(meta: ) }, .slice(:key_transform_fn, :type_transform_fn, :strict), ? self. : EMPTY_HASH] ] end |
#transform_keys? ⇒ Boolean
Whether the schema transforms input keys
198 199 200 |
# File 'lib/dry/types/schema.rb', line 198 def transform_keys? ![:key_transform_fn].nil? end |
#try(input) {|failure| ... } ⇒ Logic::Result, Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dry/types/schema.rb', line 95 def try(input) if primitive?(input) success = true output = {} result = {} input.each do |key, value| k = @transform_key.(key) type = @name_key_map[k] if type key_result = type.try(value) result[k] = key_result output[k] = key_result.input success &&= key_result.success? elsif strict? success = false end end if output.size < keys.size resolve_missing_keys(output, ) do success = false end end success &&= primitive?(output) if success failure = nil else error = CoercionError.new("#{input} doesn't conform schema", meta: result) failure = failure(output, error) end else failure = failure(input, CoercionError.new("#{input} must be a hash")) end if failure.nil? success(output) elsif block_given? yield(failure) else failure end end |
#with_key_transform(proc = nil, &block) ⇒ Schema
Inject a key transformation function
184 185 186 187 188 189 190 191 |
# File 'lib/dry/types/schema.rb', line 184 def with_key_transform(proc = nil, &block) fn = proc || block raise ArgumentError, "a block or callable argument is required" if fn.nil? handle = Dry::Types::FnContainer.register(fn) with(key_transform_fn: handle) end |