dry-types 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -3
- data/Gemfile +4 -3
- data/Rakefile +2 -2
- data/benchmarks/hash_schemas.rb +8 -6
- data/benchmarks/lax_schema.rb +0 -1
- data/benchmarks/profile_invalid_input.rb +1 -1
- data/benchmarks/profile_lax_schema_valid.rb +1 -1
- data/benchmarks/profile_valid_input.rb +1 -1
- data/dry-types.gemspec +19 -19
- data/lib/dry/types.rb +2 -2
- data/lib/dry/types/array.rb +6 -0
- data/lib/dry/types/array/constructor.rb +32 -0
- data/lib/dry/types/array/member.rb +7 -0
- data/lib/dry/types/builder.rb +1 -1
- data/lib/dry/types/builder_methods.rb +15 -15
- data/lib/dry/types/coercions.rb +19 -6
- data/lib/dry/types/coercions/params.rb +4 -4
- data/lib/dry/types/constrained.rb +5 -0
- data/lib/dry/types/constructor.rb +3 -8
- data/lib/dry/types/constructor/function.rb +4 -5
- data/lib/dry/types/core.rb +27 -8
- data/lib/dry/types/enum.rb +1 -0
- data/lib/dry/types/extensions/maybe.rb +9 -1
- data/lib/dry/types/hash.rb +8 -10
- data/lib/dry/types/hash/constructor.rb +5 -5
- data/lib/dry/types/json.rb +4 -0
- data/lib/dry/types/lax.rb +3 -3
- data/lib/dry/types/map.rb +8 -4
- data/lib/dry/types/module.rb +3 -3
- data/lib/dry/types/nominal.rb +3 -4
- data/lib/dry/types/params.rb +4 -0
- data/lib/dry/types/printer.rb +11 -10
- data/lib/dry/types/schema.rb +14 -20
- data/lib/dry/types/schema/key.rb +19 -1
- data/lib/dry/types/spec/types.rb +3 -6
- data/lib/dry/types/version.rb +1 -1
- metadata +40 -33
data/lib/dry/types/schema.rb
CHANGED
@@ -156,8 +156,8 @@ module Dry
|
|
156
156
|
[
|
157
157
|
:schema,
|
158
158
|
[keys.map { |key| key.to_ast(meta: meta) },
|
159
|
-
|
160
|
-
|
159
|
+
opts,
|
160
|
+
meta ? self.meta : EMPTY_HASH]
|
161
161
|
]
|
162
162
|
end
|
163
163
|
|
@@ -190,9 +190,7 @@ module Dry
|
|
190
190
|
def with_key_transform(proc = nil, &block)
|
191
191
|
fn = proc || block
|
192
192
|
|
193
|
-
if fn.nil?
|
194
|
-
raise ArgumentError, "a block or callable argument is required"
|
195
|
-
end
|
193
|
+
raise ArgumentError, 'a block or callable argument is required' if fn.nil?
|
196
194
|
|
197
195
|
handle = Dry::Types::FnContainer.register(fn)
|
198
196
|
with(key_transform_fn: handle)
|
@@ -203,7 +201,7 @@ module Dry
|
|
203
201
|
# @return [Boolean]
|
204
202
|
#
|
205
203
|
# @api public
|
206
|
-
def
|
204
|
+
def transform_keys?
|
207
205
|
!options[:key_transform_fn].nil?
|
208
206
|
end
|
209
207
|
|
@@ -294,10 +292,10 @@ module Dry
|
|
294
292
|
#
|
295
293
|
# @api private
|
296
294
|
def merge_keys(*keys)
|
297
|
-
keys
|
298
|
-
flatten(1)
|
299
|
-
each_with_object({}) { |key, merged| merged[key.name] = key }
|
300
|
-
values
|
295
|
+
keys
|
296
|
+
.flatten(1)
|
297
|
+
.each_with_object({}) { |key, merged| merged[key.name] = key }
|
298
|
+
.values
|
301
299
|
end
|
302
300
|
|
303
301
|
# Validate and coerce a hash. Raise an exception on any error
|
@@ -315,19 +313,17 @@ module Dry
|
|
315
313
|
if type
|
316
314
|
begin
|
317
315
|
result[k] = type.call_unsafe(value)
|
318
|
-
rescue ConstraintError =>
|
319
|
-
raise SchemaError.new(type.name, value,
|
320
|
-
rescue CoercionError =>
|
321
|
-
raise SchemaError.new(type.name, value,
|
316
|
+
rescue ConstraintError => e
|
317
|
+
raise SchemaError.new(type.name, value, e.result)
|
318
|
+
rescue CoercionError => e
|
319
|
+
raise SchemaError.new(type.name, value, e.message)
|
322
320
|
end
|
323
321
|
elsif strict?
|
324
322
|
raise unexpected_keys(hash.keys)
|
325
323
|
end
|
326
324
|
end
|
327
325
|
|
328
|
-
if result.size < keys.size
|
329
|
-
resolve_missing_keys(result, options)
|
330
|
-
end
|
326
|
+
resolve_missing_keys(result, options) if result.size < keys.size
|
331
327
|
|
332
328
|
result
|
333
329
|
end
|
@@ -351,9 +347,7 @@ module Dry
|
|
351
347
|
end
|
352
348
|
end
|
353
349
|
|
354
|
-
if result.size < keys.size
|
355
|
-
resolve_missing_keys(result, options, &block)
|
356
|
-
end
|
350
|
+
resolve_missing_keys(result, options, &block) if result.size < keys.size
|
357
351
|
|
358
352
|
result
|
359
353
|
end
|
data/lib/dry/types/schema/key.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'dry/equalizer'
|
4
|
+
require 'dry/core/deprecations'
|
4
5
|
|
5
6
|
module Dry
|
6
7
|
module Types
|
@@ -14,6 +15,7 @@ module Dry
|
|
14
15
|
#
|
15
16
|
# @see Dry::Types::Schema
|
16
17
|
class Key
|
18
|
+
extend ::Dry::Core::Deprecations[:'dry-types']
|
17
19
|
include Type
|
18
20
|
include Dry::Equalizer(:name, :type, :options, inspect: false)
|
19
21
|
include Decorator
|
@@ -94,7 +96,7 @@ module Dry
|
|
94
96
|
#
|
95
97
|
# @api public
|
96
98
|
def lax
|
97
|
-
|
99
|
+
__new__(type.lax).required(false)
|
98
100
|
end
|
99
101
|
|
100
102
|
# Dump to internal AST representation
|
@@ -113,6 +115,22 @@ module Dry
|
|
113
115
|
]
|
114
116
|
end
|
115
117
|
|
118
|
+
# @see Dry::Types::Meta#meta
|
119
|
+
#
|
120
|
+
# @api public
|
121
|
+
def meta(data = nil)
|
122
|
+
if data.nil? || !data.key?(:omittable)
|
123
|
+
super
|
124
|
+
else
|
125
|
+
self.class.warn(
|
126
|
+
'Using meta for making schema keys is deprecated, ' \
|
127
|
+
'please use .omittable or .required(false) instead' \
|
128
|
+
"\n" + Core::Deprecations::STACK.()
|
129
|
+
)
|
130
|
+
super.required(!data[:omittable])
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
116
134
|
private
|
117
135
|
|
118
136
|
# @api private
|
data/lib/dry/types/spec/types.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
RSpec.shared_examples_for 'Dry::Types::Nominal without primitive' do
|
4
4
|
def be_boolean
|
5
|
-
satisfy { |x| x == true || x == false
|
5
|
+
satisfy { |x| x == true || x == false }
|
6
6
|
end
|
7
7
|
|
8
8
|
describe '#constrained?' do
|
@@ -43,9 +43,7 @@ RSpec.shared_examples_for 'Dry::Types::Nominal without primitive' do
|
|
43
43
|
|
44
44
|
describe '#to_s' do
|
45
45
|
it 'returns a custom string representation' do
|
46
|
-
if type.class.name.start_with?('Dry::Types')
|
47
|
-
expect(type.to_s).to start_with('#<Dry::Types')
|
48
|
-
end
|
46
|
+
expect(type.to_s).to start_with('#<Dry::Types') if type.class.name.start_with?('Dry::Types')
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
@@ -81,7 +79,7 @@ RSpec.shared_examples_for 'Dry::Types::Nominal#meta' do
|
|
81
79
|
expect(type.meta).not_to have_key :immutable_test
|
82
80
|
derived = type.meta(immutable_test: 1)
|
83
81
|
expect(derived.meta).to be_frozen
|
84
|
-
expect(derived.meta).to eql(
|
82
|
+
expect(derived.meta).to eql(immutable_test: 1)
|
85
83
|
expect(type.meta).not_to have_key :immutable_test
|
86
84
|
end
|
87
85
|
end
|
@@ -139,4 +137,3 @@ RSpec.shared_examples_for 'a nominal type' do |inputs: Object.new|
|
|
139
137
|
end
|
140
138
|
end
|
141
139
|
end
|
142
|
-
|
data/lib/dry/types/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -25,79 +25,79 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: dry-
|
28
|
+
name: dry-container
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
34
|
-
- - ">="
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: 0.4.4
|
33
|
+
version: '0.3'
|
37
34
|
type: :runtime
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
38
|
- - "~>"
|
42
39
|
- !ruby/object:Gem::Version
|
43
|
-
version: '0.
|
44
|
-
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 0.4.4
|
40
|
+
version: '0.3'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
|
-
name: dry-
|
42
|
+
name: dry-core
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
50
44
|
requirements:
|
51
45
|
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0.
|
47
|
+
version: '0.4'
|
54
48
|
- - ">="
|
55
49
|
- !ruby/object:Gem::Version
|
56
|
-
version: 0.
|
50
|
+
version: 0.4.4
|
57
51
|
type: :runtime
|
58
52
|
prerelease: false
|
59
53
|
version_requirements: !ruby/object:Gem::Requirement
|
60
54
|
requirements:
|
61
55
|
- - "~>"
|
62
56
|
- !ruby/object:Gem::Version
|
63
|
-
version: '0.
|
57
|
+
version: '0.4'
|
64
58
|
- - ">="
|
65
59
|
- !ruby/object:Gem::Version
|
66
|
-
version: 0.
|
60
|
+
version: 0.4.4
|
67
61
|
- !ruby/object:Gem::Dependency
|
68
|
-
name: dry-
|
62
|
+
name: dry-equalizer
|
69
63
|
requirement: !ruby/object:Gem::Requirement
|
70
64
|
requirements:
|
71
65
|
- - "~>"
|
72
66
|
- !ruby/object:Gem::Version
|
73
|
-
version: '0.
|
67
|
+
version: '0.2'
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.2.2
|
74
71
|
type: :runtime
|
75
72
|
prerelease: false
|
76
73
|
version_requirements: !ruby/object:Gem::Requirement
|
77
74
|
requirements:
|
78
75
|
- - "~>"
|
79
76
|
- !ruby/object:Gem::Version
|
80
|
-
version: '0.
|
77
|
+
version: '0.2'
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.2.2
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
|
-
name: dry-
|
82
|
+
name: dry-inflector
|
83
83
|
requirement: !ruby/object:Gem::Requirement
|
84
84
|
requirements:
|
85
85
|
- - "~>"
|
86
86
|
- !ruby/object:Gem::Version
|
87
|
-
version: '0.
|
87
|
+
version: '0.1'
|
88
88
|
- - ">="
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version: 0.
|
90
|
+
version: 0.1.2
|
91
91
|
type: :runtime
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
95
|
- - "~>"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: '0.
|
97
|
+
version: '0.1'
|
98
98
|
- - ">="
|
99
99
|
- !ruby/object:Gem::Version
|
100
|
-
version: 0.
|
100
|
+
version: 0.1.2
|
101
101
|
- !ruby/object:Gem::Dependency
|
102
102
|
name: dry-logic
|
103
103
|
requirement: !ruby/object:Gem::Requirement
|
@@ -105,6 +105,9 @@ dependencies:
|
|
105
105
|
- - "~>"
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '1.0'
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.0.2
|
108
111
|
type: :runtime
|
109
112
|
prerelease: false
|
110
113
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -112,6 +115,9 @@ dependencies:
|
|
112
115
|
- - "~>"
|
113
116
|
- !ruby/object:Gem::Version
|
114
117
|
version: '1.0'
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 1.0.2
|
115
121
|
- !ruby/object:Gem::Dependency
|
116
122
|
name: bundler
|
117
123
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,47 +133,47 @@ dependencies:
|
|
127
133
|
- !ruby/object:Gem::Version
|
128
134
|
version: '0'
|
129
135
|
- !ruby/object:Gem::Dependency
|
130
|
-
name:
|
136
|
+
name: dry-monads
|
131
137
|
requirement: !ruby/object:Gem::Requirement
|
132
138
|
requirements:
|
133
139
|
- - "~>"
|
134
140
|
- !ruby/object:Gem::Version
|
135
|
-
version: '
|
141
|
+
version: '0.2'
|
136
142
|
type: :development
|
137
143
|
prerelease: false
|
138
144
|
version_requirements: !ruby/object:Gem::Requirement
|
139
145
|
requirements:
|
140
146
|
- - "~>"
|
141
147
|
- !ruby/object:Gem::Version
|
142
|
-
version: '
|
148
|
+
version: '0.2'
|
143
149
|
- !ruby/object:Gem::Dependency
|
144
|
-
name:
|
150
|
+
name: rake
|
145
151
|
requirement: !ruby/object:Gem::Requirement
|
146
152
|
requirements:
|
147
153
|
- - "~>"
|
148
154
|
- !ruby/object:Gem::Version
|
149
|
-
version: '
|
155
|
+
version: '11.0'
|
150
156
|
type: :development
|
151
157
|
prerelease: false
|
152
158
|
version_requirements: !ruby/object:Gem::Requirement
|
153
159
|
requirements:
|
154
160
|
- - "~>"
|
155
161
|
- !ruby/object:Gem::Version
|
156
|
-
version: '
|
162
|
+
version: '11.0'
|
157
163
|
- !ruby/object:Gem::Dependency
|
158
|
-
name:
|
164
|
+
name: rspec
|
159
165
|
requirement: !ruby/object:Gem::Requirement
|
160
166
|
requirements:
|
161
167
|
- - "~>"
|
162
168
|
- !ruby/object:Gem::Version
|
163
|
-
version: '
|
169
|
+
version: '3.3'
|
164
170
|
type: :development
|
165
171
|
prerelease: false
|
166
172
|
version_requirements: !ruby/object:Gem::Requirement
|
167
173
|
requirements:
|
168
174
|
- - "~>"
|
169
175
|
- !ruby/object:Gem::Version
|
170
|
-
version: '
|
176
|
+
version: '3.3'
|
171
177
|
- !ruby/object:Gem::Dependency
|
172
178
|
name: yard
|
173
179
|
requirement: !ruby/object:Gem::Requirement
|
@@ -214,6 +220,7 @@ files:
|
|
214
220
|
- lib/dry/types.rb
|
215
221
|
- lib/dry/types/any.rb
|
216
222
|
- lib/dry/types/array.rb
|
223
|
+
- lib/dry/types/array/constructor.rb
|
217
224
|
- lib/dry/types/array/member.rb
|
218
225
|
- lib/dry/types/builder.rb
|
219
226
|
- lib/dry/types/builder_methods.rb
|