json_model_rb 0.1.3 → 0.1.4
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/lib/json_model/properties.rb +10 -3
- data/lib/json_model/schema_meta.rb +8 -4
- data/lib/json_model/type_spec.rb +2 -2
- data/lib/json_model/version.rb +1 -1
- data/spec/schema_spec.rb +33 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d8629addf96efee3c996feeeff0f3b1806ceb78bbc3ecffd6470ea1f96db1da5
|
|
4
|
+
data.tar.gz: 9821968d8a12620d7b4f53c5884cbe6214998e3af9b6785719f8390a3148725f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ae72851a9d485cc0cbd7df518d48a40ff03a115025a4e3a04177fb6207783c2df3377c7778b1131cee053fc13711e60b9dafe220b831303f06d19eec56ef3eb6
|
|
7
|
+
data.tar.gz: e51539c6a3beb3319949bb0e052e5817304772c012d742f96b744289c2cb33afd8d9864b4593e3a0ea9013ee9039a5183e4ce1351fa670534dae1e1b45b10ab4
|
|
@@ -7,14 +7,21 @@ module JsonModel
|
|
|
7
7
|
included do
|
|
8
8
|
extend(ActiveSupport::DescendantsTracker)
|
|
9
9
|
include(ActiveModel::Validations)
|
|
10
|
+
end
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
# @return [Hash]
|
|
13
|
+
def attributes
|
|
14
|
+
@attributes ||= {}
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
class_methods do
|
|
18
|
+
# @return [Hash]
|
|
19
|
+
def properties
|
|
20
|
+
@properties ||= {}
|
|
21
|
+
end
|
|
22
|
+
|
|
16
23
|
# @param [Symbol] name
|
|
17
|
-
# @param [Object] type
|
|
24
|
+
# @param [Object, Class] type
|
|
18
25
|
# @param [Hash] options
|
|
19
26
|
def property(name, type:, **options)
|
|
20
27
|
property_options = options.slice(:default, :optional, :ref_mode, :as)
|
|
@@ -13,20 +13,24 @@ module JsonModel
|
|
|
13
13
|
}.freeze
|
|
14
14
|
|
|
15
15
|
included do
|
|
16
|
-
class_attribute(:meta_attributes, instance_writer: false, default: {})
|
|
17
|
-
class_attribute(:ref_schema, instance_writer: false)
|
|
18
|
-
|
|
19
16
|
# @param [Class] subclass
|
|
20
17
|
def self.inherited(subclass)
|
|
21
18
|
super
|
|
19
|
+
subclass.schema_id(JsonModel.config.schema_id_naming_strategy.call(self))
|
|
20
|
+
subclass.additional_properties(false)
|
|
22
21
|
subclass.meta_attributes[:$ref] = schema_id
|
|
23
22
|
end
|
|
24
23
|
|
|
25
|
-
schema_id(
|
|
24
|
+
schema_id(JsonModel.config.schema_id_naming_strategy.call(self))
|
|
26
25
|
additional_properties(false)
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
class_methods do
|
|
29
|
+
# @return [Hash]
|
|
30
|
+
def meta_attributes
|
|
31
|
+
@meta_attributes ||= {}
|
|
32
|
+
end
|
|
33
|
+
|
|
30
34
|
# @param [String, nil] description
|
|
31
35
|
# @return [String, nil]
|
|
32
36
|
def description(description = nil)
|
data/lib/json_model/type_spec.rb
CHANGED
|
@@ -30,7 +30,7 @@ module JsonModel
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
class << self
|
|
33
|
-
# @param [Object] type
|
|
33
|
+
# @param [Object, Class] type
|
|
34
34
|
# @param [Hash] options
|
|
35
35
|
# @return [TypeSpec]
|
|
36
36
|
def resolve(type, **options)
|
|
@@ -48,7 +48,7 @@ module JsonModel
|
|
|
48
48
|
|
|
49
49
|
private
|
|
50
50
|
|
|
51
|
-
# @param [Object] type
|
|
51
|
+
# @param [Object, Class] type
|
|
52
52
|
# @param [Hash] options
|
|
53
53
|
# @return [TypeSpec]
|
|
54
54
|
def resolve_type_from_class(type, **options)
|
data/lib/json_model/version.rb
CHANGED
data/spec/schema_spec.rb
CHANGED
|
@@ -200,6 +200,14 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
200
200
|
let(:child) do
|
|
201
201
|
Class.new(klass) do
|
|
202
202
|
schema_id('https://example.com/schemas/child.json')
|
|
203
|
+
property(:baz, type: String)
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
let(:second_child) do
|
|
207
|
+
Class.new(klass) do
|
|
208
|
+
schema_id('https://example.com/schemas/second-child.json')
|
|
209
|
+
title('SecondChild')
|
|
210
|
+
property(:bar, type: String)
|
|
203
211
|
end
|
|
204
212
|
end
|
|
205
213
|
|
|
@@ -215,9 +223,34 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
215
223
|
'$ref': 'https://example.com/schemas/example.json',
|
|
216
224
|
type: 'object',
|
|
217
225
|
additionalProperties: false,
|
|
226
|
+
properties: { baz: { type: 'string' } },
|
|
227
|
+
required: %i(baz),
|
|
218
228
|
},
|
|
219
229
|
),
|
|
220
230
|
)
|
|
231
|
+
|
|
232
|
+
expect(second_child.as_schema)
|
|
233
|
+
.to(
|
|
234
|
+
eq(
|
|
235
|
+
{
|
|
236
|
+
'$id': 'https://example.com/schemas/second-child.json',
|
|
237
|
+
'$ref': 'https://example.com/schemas/example.json',
|
|
238
|
+
title: 'SecondChild',
|
|
239
|
+
type: 'object',
|
|
240
|
+
additionalProperties: false,
|
|
241
|
+
properties: { bar: { type: 'string' } },
|
|
242
|
+
required: %i(bar),
|
|
243
|
+
},
|
|
244
|
+
),
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
instance = child.new(foo: 'foo', baz: 'baz')
|
|
248
|
+
expect(instance.foo).to(eq('foo'))
|
|
249
|
+
expect(instance.baz).to(eq('baz'))
|
|
250
|
+
|
|
251
|
+
second_instance = second_child.new(foo: 'foo', bar: 'bar')
|
|
252
|
+
expect(second_instance.foo).to(eq('foo'))
|
|
253
|
+
expect(second_instance.bar).to(eq('bar'))
|
|
221
254
|
end
|
|
222
255
|
end
|
|
223
256
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json_model_rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Paul Gillesberger
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2025-12-
|
|
10
|
+
date: 2025-12-29 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: rake
|