json_model_rb 0.1.5 → 0.1.6
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/type_spec/array.rb +6 -2
- data/lib/json_model/type_spec/composition/all_of.rb +1 -1
- data/lib/json_model/type_spec/composition/any_of.rb +1 -1
- data/lib/json_model/type_spec/composition/one_of.rb +1 -1
- data/lib/json_model/type_spec/composition.rb +1 -1
- data/lib/json_model/type_spec/const.rb +33 -0
- data/lib/json_model/type_spec/object.rb +1 -1
- data/lib/json_model/type_spec/primitive/string.rb +0 -1
- data/lib/json_model/type_spec/primitive.rb +2 -2
- data/lib/json_model/type_spec.rb +2 -1
- data/lib/json_model/types/const.rb +23 -0
- data/lib/json_model/types.rb +1 -0
- data/lib/json_model/version.rb +1 -1
- data/spec/type_spec/const_spec.rb +18 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5fd21067ed2a5b88fe10eb6238524fdeac5234bb54dbbb8325f57bd12a5201d1
|
|
4
|
+
data.tar.gz: 3e388b7b7e26367fc55a95573f5f119d6109826f81c560e12445ad0c5f3f5efe
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5c74c663193b74704f7165e312b226261b4b5ed44d469770ad6e0853c104917b21397f6ebd56c6aa18f6d17499dd431949605b6068cd853b8c7ce53bda156dcf
|
|
7
|
+
data.tar.gz: 2022ec4abc4ba514b6b106d51ead65307046283e5baf9406453f83ecbce75c393f985edee6c9469480dab320e9a3a57580da98f6ea00eda82a9d4f5216958233
|
|
@@ -46,9 +46,13 @@ module JsonModel
|
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# @param [::Object] json
|
|
49
|
-
# @return [::Object]
|
|
49
|
+
# @return [::Object, nil]
|
|
50
50
|
def cast(json)
|
|
51
|
-
json.
|
|
51
|
+
if json.is_a?(Enumerable)
|
|
52
|
+
json.map { |item| @type.cast(item) }
|
|
53
|
+
else
|
|
54
|
+
raise(Errors::TypeError, "Expected an array, got #{json.class} (#{json.inspect})")
|
|
55
|
+
end
|
|
52
56
|
end
|
|
53
57
|
|
|
54
58
|
private
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JsonModel
|
|
4
|
+
class TypeSpec
|
|
5
|
+
class Const < TypeSpec
|
|
6
|
+
# @param [String] value
|
|
7
|
+
def initialize(value)
|
|
8
|
+
super()
|
|
9
|
+
@value = value
|
|
10
|
+
|
|
11
|
+
if value.blank?
|
|
12
|
+
raise(ArgumentError, 'Const type spec requires a non-empty value')
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @param [Hash] _options
|
|
17
|
+
# @return [Hash]
|
|
18
|
+
def as_schema(**_options)
|
|
19
|
+
{
|
|
20
|
+
const: @value,
|
|
21
|
+
}.compact
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @param [Symbol] name
|
|
25
|
+
# @param [ActiveModel::Validations] klass
|
|
26
|
+
def register_validations(name, klass)
|
|
27
|
+
super
|
|
28
|
+
|
|
29
|
+
klass.validates(name, inclusion: { in: @value })
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -18,14 +18,14 @@ module JsonModel
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
# @param [::Object] json
|
|
21
|
-
# @return [::Object]
|
|
21
|
+
# @return [::Object, nil]
|
|
22
22
|
def cast(json)
|
|
23
23
|
if json.nil?
|
|
24
24
|
nil
|
|
25
25
|
elsif @types.any? { |type| json.is_a?(type) }
|
|
26
26
|
json
|
|
27
27
|
else
|
|
28
|
-
raise(Errors::TypeError, "Expected #{@type}, got #{json.class} (#{json.inspect})")
|
|
28
|
+
raise(Errors::TypeError, "Expected one of #{@type.join('/')}, got #{json.class} (#{json.inspect})")
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
end
|
data/lib/json_model/type_spec.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative('type_spec/array')
|
|
4
4
|
require_relative('type_spec/composition')
|
|
5
|
+
require_relative('type_spec/const')
|
|
5
6
|
require_relative('type_spec/enum')
|
|
6
7
|
require_relative('type_spec/object')
|
|
7
8
|
require_relative('type_spec/primitive')
|
|
@@ -24,7 +25,7 @@ module JsonModel
|
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# @param [::Object] json
|
|
27
|
-
# @return [::Object]
|
|
28
|
+
# @return [::Object, nil]
|
|
28
29
|
def cast(json)
|
|
29
30
|
json
|
|
30
31
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module T
|
|
4
|
+
class Const
|
|
5
|
+
# @param [String] value
|
|
6
|
+
def initialize(value)
|
|
7
|
+
@value = value
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# @return [JsonModel::TypeSpec::Const]
|
|
11
|
+
def to_type_spec(**options)
|
|
12
|
+
JsonModel::TypeSpec::Const.new(*@value, **options)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
# @param [Array] args
|
|
17
|
+
# @return [Const]
|
|
18
|
+
def [](*args)
|
|
19
|
+
Const.new(*args)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/json_model/types.rb
CHANGED
data/lib/json_model/version.rb
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require('spec_helper')
|
|
4
|
+
|
|
5
|
+
RSpec.describe(JsonModel::TypeSpec::Const) do
|
|
6
|
+
describe('#as_schema') do
|
|
7
|
+
it('returns a const schema') do
|
|
8
|
+
expect(described_class.new('a').as_schema)
|
|
9
|
+
.to(
|
|
10
|
+
eq(
|
|
11
|
+
{
|
|
12
|
+
const: 'a'
|
|
13
|
+
},
|
|
14
|
+
),
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Paul Gillesberger
|
|
@@ -121,6 +121,7 @@ files:
|
|
|
121
121
|
- lib/json_model/type_spec/composition/all_of.rb
|
|
122
122
|
- lib/json_model/type_spec/composition/any_of.rb
|
|
123
123
|
- lib/json_model/type_spec/composition/one_of.rb
|
|
124
|
+
- lib/json_model/type_spec/const.rb
|
|
124
125
|
- lib/json_model/type_spec/enum.rb
|
|
125
126
|
- lib/json_model/type_spec/object.rb
|
|
126
127
|
- lib/json_model/type_spec/primitive.rb
|
|
@@ -135,6 +136,7 @@ files:
|
|
|
135
136
|
- lib/json_model/types/any_of.rb
|
|
136
137
|
- lib/json_model/types/array.rb
|
|
137
138
|
- lib/json_model/types/boolean.rb
|
|
139
|
+
- lib/json_model/types/const.rb
|
|
138
140
|
- lib/json_model/types/enum.rb
|
|
139
141
|
- lib/json_model/types/one_of.rb
|
|
140
142
|
- lib/json_model/version.rb
|
|
@@ -152,6 +154,7 @@ files:
|
|
|
152
154
|
- spec/type_spec/composition/any_of_spec.rb
|
|
153
155
|
- spec/type_spec/composition/one_of_spec.rb
|
|
154
156
|
- spec/type_spec/composition_spec.rb
|
|
157
|
+
- spec/type_spec/const_spec.rb
|
|
155
158
|
- spec/type_spec/enum_spec.rb
|
|
156
159
|
- spec/type_spec/primitive/boolean_spec.rb
|
|
157
160
|
- spec/type_spec/primitive/integer_spec.rb
|