dry-types-json-schema 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +12 -0
- data/Gemfile.lock +1 -1
- data/dry-types-json-schema.gemspec +1 -1
- data/lib/dry/types/extensions/json_schema.rb +31 -11
- data/spec/extensions/json_schema_spec.rb +14 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3c596c245aad8cb72bc10fbe904078ac01c7522d14c135b4a6645a7b7f5c769
|
4
|
+
data.tar.gz: 5fc2b32368fdcb1135b32fdb7d3db27c2104966ff371f020eea750df08588b4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50ed4c3ca20040b4d88ae5ecf107c6ccfad7ae7242a724f1c3afd9f015f63a7b203966571af8699614881f7848a657d322554bbb541ff48244194662a73c40e1
|
7
|
+
data.tar.gz: 4810a8578cfecb91df93f66b4a9ad6748db42ef0ebd219c1dfce4eeb52c9fd6992027925579dea2f545b57b38f851eca600ef1eb898ba0a5f114e8d42c14537f
|
data/Gemfile.lock
CHANGED
@@ -171,11 +171,19 @@ module Dry
|
|
171
171
|
@keys[ctx].merge!(definition)
|
172
172
|
end
|
173
173
|
|
174
|
+
def visit_intersection(node, opts = EMPTY_HASH)
|
175
|
+
*types, _ = node
|
176
|
+
|
177
|
+
result = types.map { |type| compile_type(type) }
|
178
|
+
|
179
|
+
@keys[opts[:key]] = deep_merge_items(result)
|
180
|
+
end
|
181
|
+
|
174
182
|
def visit_sum(node, opts = EMPTY_HASH)
|
175
183
|
*types, _ = node
|
176
184
|
|
177
185
|
result = types
|
178
|
-
.map { |type|
|
186
|
+
.map { |type| compile_value(type, opts.merge(sum: true)) }
|
179
187
|
.uniq
|
180
188
|
|
181
189
|
return @keys[opts[:key]] = result.first if result.count == 1
|
@@ -205,14 +213,9 @@ module Dry
|
|
205
213
|
def visit_struct(node, opts = EMPTY_HASH)
|
206
214
|
_, schema = node
|
207
215
|
|
208
|
-
|
209
|
-
target = self.class.new
|
210
|
-
target.visit(schema)
|
216
|
+
return visit(schema, opts) unless opts[:key]
|
211
217
|
|
212
|
-
|
213
|
-
else
|
214
|
-
visit(schema, opts)
|
215
|
-
end
|
218
|
+
@keys[opts[:key]] = compile_type(schema)
|
216
219
|
end
|
217
220
|
|
218
221
|
def visit_array(node, opts = EMPTY_HASH)
|
@@ -253,12 +256,29 @@ module Dry
|
|
253
256
|
|
254
257
|
private
|
255
258
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
+
def deep_merge_items(items)
|
260
|
+
items.reduce({}) do |current, target|
|
261
|
+
current.merge(target) do |_, from, to|
|
262
|
+
case [from.class, to.class]
|
263
|
+
when [::Hash, ::Hash]
|
264
|
+
deep_merge_items([from, to])
|
265
|
+
when [::Array, ::Array]
|
266
|
+
from | to
|
267
|
+
else
|
268
|
+
to
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
def compile_type(type, opts = EMPTY_HASH)
|
259
275
|
self.class.new
|
260
276
|
.tap { |target| target.visit(type, opts) }
|
261
277
|
.to_hash
|
278
|
+
end
|
279
|
+
|
280
|
+
def compile_value(type, opts = EMPTY_HASH)
|
281
|
+
compile_type(type, opts)
|
262
282
|
.values
|
263
283
|
.first
|
264
284
|
end
|
@@ -70,6 +70,9 @@ describe Dry::Types::JSONSchema do
|
|
70
70
|
.of(Types::String)
|
71
71
|
.constrained(min_size: 1)
|
72
72
|
|
73
|
+
BasicHash = Types::Hash.schema(name: Types::String)
|
74
|
+
ExtendedHash = Types::Hash.schema(age: Types::Integer) & BasicHash
|
75
|
+
|
73
76
|
attribute :data, Types::String | Types::Hash
|
74
77
|
attribute :string, Types::String.constrained(min_size: 1, max_size: 255)
|
75
78
|
attribute :list, VariableList
|
@@ -81,6 +84,8 @@ describe Dry::Types::JSONSchema do
|
|
81
84
|
attribute? :meta, Types::String.meta(format: :email)
|
82
85
|
attribute? :enum, Types::String.enum(*%w[draft published archived])
|
83
86
|
attribute? :array, ArrayOfStrings
|
87
|
+
attribute? :inter, ExtendedHash
|
88
|
+
|
84
89
|
attribute? :nested do
|
85
90
|
attribute :deep, Types::Integer
|
86
91
|
end
|
@@ -162,6 +167,15 @@ describe Dry::Types::JSONSchema do
|
|
162
167
|
items: { type: :string }
|
163
168
|
},
|
164
169
|
|
170
|
+
inter: {
|
171
|
+
type: :object,
|
172
|
+
properties: {
|
173
|
+
age: { type: :integer },
|
174
|
+
name: { type: :string }
|
175
|
+
},
|
176
|
+
required: %i[age name]
|
177
|
+
},
|
178
|
+
|
165
179
|
nested: {
|
166
180
|
type: :object,
|
167
181
|
properties: {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-types-json-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- elcuervo
|
@@ -143,6 +143,7 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
+
- ".github/workflows/test.yml"
|
146
147
|
- ".gitignore"
|
147
148
|
- ".rubocop.yml"
|
148
149
|
- Gemfile
|