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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d53a441b9ed9f57e185e971538b1a3b3f81038527e9f071c7af8fbfb8cd10a3c
4
- data.tar.gz: d6693481b3aaa1d6b5b81b34ae91c90eea58758f094681b68ea11f9fca5ea34c
3
+ metadata.gz: b3c596c245aad8cb72bc10fbe904078ac01c7522d14c135b4a6645a7b7f5c769
4
+ data.tar.gz: 5fc2b32368fdcb1135b32fdb7d3db27c2104966ff371f020eea750df08588b4f
5
5
  SHA512:
6
- metadata.gz: 939497ac477bdc404b3ab166f3af9d2f42260617f96f145e73921d0dc721a57cea618d48b4738958b322be5c549268829fda68bd7309487db1e1695f47da3b16
7
- data.tar.gz: 4e86da1db6970398e73fb22ee6d7dd3e1505198705bdcc0cddf28d7df849afcd521703043e6a550e42ee75bd526d30ecf5077d81b048498f914251554095ff26
6
+ metadata.gz: 50ed4c3ca20040b4d88ae5ecf107c6ccfad7ae7242a724f1c3afd9f015f63a7b203966571af8699614881f7848a657d322554bbb541ff48244194662a73c40e1
7
+ data.tar.gz: 4810a8578cfecb91df93f66b4a9ad6748db42ef0ebd219c1dfce4eeb52c9fd6992027925579dea2f545b57b38f851eca600ef1eb898ba0a5f114e8d42c14537f
@@ -0,0 +1,12 @@
1
+ name: Test
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ steps:
7
+ - uses: actions/checkout@v4
8
+ - uses: ruby/setup-ruby@v1
9
+ with:
10
+ ruby-version: '3.3'
11
+ bundler-cache: true
12
+ - run: bundle exec make test
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dry-types-json-schema (0.0.2)
4
+ dry-types-json-schema (0.0.3)
5
5
  dry-types (~> 1.7.2)
6
6
 
7
7
  GEM
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "dry-types-json-schema"
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
  s.summary = "Generate JSON Schema from dry-types"
7
7
  s.authors = ["elcuervo"]
8
8
  s.licenses = %w[MIT]
@@ -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| single_type(type, opts.merge(sum: true)) }
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
- if opts[:key]
209
- target = self.class.new
210
- target.visit(schema)
216
+ return visit(schema, opts) unless opts[:key]
211
217
 
212
- @keys[opts[:key]] = target.to_hash
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
- # FIXME: cleaner way to generate individual types
257
- #
258
- def single_type(type, opts)
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.2
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