json_model_rb 0.1.19 → 0.1.22
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/README.md +156 -572
- data/lib/json_model/builder/alias_builder.rb +10 -0
- data/lib/json_model/builder/array_builder.rb +24 -0
- data/lib/json_model/builder/base_builder.rb +24 -0
- data/lib/json_model/builder/composition/any_of_builder.rb +16 -0
- data/lib/json_model/builder/composition/builder.rb +47 -0
- data/lib/json_model/builder/composition/intersection_builder.rb +16 -0
- data/lib/json_model/builder/composition/one_of_builder.rb +16 -0
- data/lib/json_model/builder/composition/sum_builder.rb +16 -0
- data/lib/json_model/builder/composition.rb +6 -0
- data/lib/json_model/builder/constrained_builder.rb +83 -0
- data/lib/json_model/builder/default_builder.rb +14 -0
- data/lib/json_model/builder/enum_builder.rb +16 -0
- data/lib/json_model/builder/format_builder.rb +47 -0
- data/lib/json_model/builder/key_builder.rb +30 -0
- data/lib/json_model/builder/nested_builder.rb +31 -0
- data/lib/json_model/builder/primitive/boolean_builder.rb +16 -0
- data/lib/json_model/builder/primitive/builder.rb +25 -0
- data/lib/json_model/builder/primitive/integer_builder.rb +16 -0
- data/lib/json_model/builder/primitive/null_builder.rb +16 -0
- data/lib/json_model/builder/primitive/number_builder.rb +16 -0
- data/lib/json_model/builder/primitive/string_builder.rb +16 -0
- data/lib/json_model/builder/primitive.rb +7 -0
- data/lib/json_model/builder/ref_builder.rb +35 -0
- data/lib/json_model/builder/schema_builder.rb +14 -0
- data/lib/json_model/builder.rb +65 -0
- data/lib/json_model/config/options.rb +5 -2
- data/lib/json_model/config.rb +0 -2
- data/lib/json_model/errors.rb +0 -3
- data/lib/json_model/logic/predicates/methods.rb +17 -0
- data/lib/json_model/predicates.rb +3 -0
- data/lib/json_model/schema.rb +41 -77
- data/lib/json_model/schema_meta.rb +7 -28
- data/lib/json_model/types/alias.rb +33 -0
- data/lib/json_model/types/any_of.rb +51 -20
- data/lib/json_model/types/one_of.rb +51 -20
- data/lib/json_model/types/ref.rb +32 -0
- data/lib/json_model/types.rb +47 -9
- data/lib/json_model/version.rb +1 -1
- data/lib/json_model.rb +3 -6
- data/spec/config_spec.rb +0 -14
- data/spec/examples/file_system_spec.rb +71 -31
- data/spec/examples/user_spec.rb +67 -26
- data/spec/schema_meta_spec.rb +0 -40
- data/spec/schema_spec.rb +43 -59
- metadata +84 -57
- data/lib/json_model/errors/invalid_ref_mode_error.rb +0 -12
- data/lib/json_model/errors/type_error.rb +0 -8
- data/lib/json_model/errors/unknown_attribute_error.rb +0 -13
- data/lib/json_model/properties.rb +0 -86
- data/lib/json_model/property.rb +0 -54
- data/lib/json_model/ref_mode.rb +0 -9
- data/lib/json_model/type_spec/array.rb +0 -72
- data/lib/json_model/type_spec/castable.rb +0 -34
- data/lib/json_model/type_spec/composition/all_of.rb +0 -29
- data/lib/json_model/type_spec/composition/any_of.rb +0 -34
- data/lib/json_model/type_spec/composition/one_of.rb +0 -38
- data/lib/json_model/type_spec/composition.rb +0 -79
- data/lib/json_model/type_spec/const.rb +0 -35
- data/lib/json_model/type_spec/enum.rb +0 -35
- data/lib/json_model/type_spec/object.rb +0 -32
- data/lib/json_model/type_spec/primitive/boolean.rb +0 -13
- data/lib/json_model/type_spec/primitive/integer.rb +0 -21
- data/lib/json_model/type_spec/primitive/null.rb +0 -13
- data/lib/json_model/type_spec/primitive/number.rb +0 -14
- data/lib/json_model/type_spec/primitive/numeric.rb +0 -85
- data/lib/json_model/type_spec/primitive/string.rb +0 -150
- data/lib/json_model/type_spec/primitive.rb +0 -40
- data/lib/json_model/type_spec.rb +0 -82
- data/lib/json_model/types/all_of.rb +0 -29
- data/lib/json_model/types/array.rb +0 -29
- data/lib/json_model/types/boolean.rb +0 -7
- data/lib/json_model/types/const.rb +0 -23
- data/lib/json_model/types/enum.rb +0 -23
- data/lib/json_model/types/integer.rb +0 -23
- data/lib/json_model/types/null.rb +0 -7
- data/lib/json_model/types/number.rb +0 -23
- data/lib/json_model/types/string.rb +0 -23
- data/spec/properties_spec.rb +0 -76
- data/spec/property_spec.rb +0 -86
- data/spec/type_spec/array_spec.rb +0 -206
- data/spec/type_spec/castable_spec.rb +0 -19
- data/spec/type_spec/composition/all_of_spec.rb +0 -57
- data/spec/type_spec/composition/any_of_spec.rb +0 -54
- data/spec/type_spec/composition/one_of_spec.rb +0 -59
- data/spec/type_spec/composition_spec.rb +0 -90
- data/spec/type_spec/const_spec.rb +0 -18
- data/spec/type_spec/enum_spec.rb +0 -84
- data/spec/type_spec/primitive/boolean_spec.rb +0 -12
- data/spec/type_spec/primitive/integer_spec.rb +0 -57
- data/spec/type_spec/primitive/null_spec.rb +0 -12
- data/spec/type_spec/primitive/number_spec.rb +0 -12
- data/spec/type_spec/primitive/numeric_spec.rb +0 -176
- data/spec/type_spec/primitive/string_spec.rb +0 -119
- data/spec/type_spec_spec.rb +0 -32
|
@@ -6,62 +6,87 @@ RSpec.describe('File system schema') do
|
|
|
6
6
|
before do
|
|
7
7
|
stub_const(
|
|
8
8
|
'DiskDevice',
|
|
9
|
-
Class.new do
|
|
9
|
+
Class.new(Dry::Struct) do
|
|
10
10
|
include(JsonModel::Schema)
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
def self.name
|
|
13
|
+
'DiskDevice'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attribute(:type, JsonModel::Types::String.constrained(eql: 'disk'))
|
|
17
|
+
attribute(:device, JsonModel::Types::String.constrained(format: %r{\A/dev/[^/]+(/[^/]+)*\z}))
|
|
14
18
|
end,
|
|
15
19
|
)
|
|
16
20
|
|
|
17
21
|
stub_const(
|
|
18
22
|
'DiskUuid',
|
|
19
|
-
Class.new do
|
|
23
|
+
Class.new(Dry::Struct) do
|
|
20
24
|
include(JsonModel::Schema)
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
def self.name
|
|
27
|
+
'DiskUuid'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
attribute(:type, JsonModel::Types::String.enum('diskUUID', 'diskuuid'))
|
|
31
|
+
attribute(
|
|
24
32
|
:label,
|
|
25
|
-
|
|
33
|
+
JsonModel::Types::String.constrained(
|
|
34
|
+
format: /\A[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\z/,
|
|
35
|
+
),
|
|
26
36
|
)
|
|
27
37
|
end,
|
|
28
38
|
)
|
|
29
39
|
|
|
30
40
|
stub_const(
|
|
31
41
|
'Nfs',
|
|
32
|
-
Class.new do
|
|
42
|
+
Class.new(Dry::Struct) do
|
|
33
43
|
include(JsonModel::Schema)
|
|
34
44
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
def self.name
|
|
46
|
+
'Nfs'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
attribute(:type, JsonModel::Types::String.constrained(eql: 'nfs'))
|
|
50
|
+
attribute(:remote_path, JsonModel::Types::String.constrained(format: %r{\A(/[^/]+)+\z}).as(:remotePath))
|
|
51
|
+
attribute(:server, JsonModel::Types::IPv4)
|
|
38
52
|
end,
|
|
39
53
|
)
|
|
40
54
|
|
|
41
55
|
stub_const(
|
|
42
56
|
'Tmpfs',
|
|
43
|
-
Class.new do
|
|
57
|
+
Class.new(Dry::Struct) do
|
|
44
58
|
include(JsonModel::Schema)
|
|
45
59
|
|
|
46
|
-
|
|
47
|
-
|
|
60
|
+
def self.name
|
|
61
|
+
'Tmpfs'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
attribute(:type, JsonModel::Types::String.constrained(eql: 'tmpfs'))
|
|
65
|
+
attribute(:size_in_mb, JsonModel::Types::Integer.constrained(gteq: 16, lteq: 512).as(:sizeInMB))
|
|
48
66
|
end,
|
|
49
67
|
)
|
|
50
68
|
|
|
51
69
|
stub_const(
|
|
52
70
|
'Fstab',
|
|
53
|
-
Class.new do
|
|
71
|
+
Class.new(Dry::Struct) do
|
|
54
72
|
include(JsonModel::Schema)
|
|
55
73
|
|
|
56
74
|
description('JSON Schema for an fstab entry')
|
|
57
|
-
|
|
75
|
+
attribute(
|
|
58
76
|
:storage,
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
JsonModel::Types.one_of(:type) do
|
|
78
|
+
on('disk', DiskDevice.local)
|
|
79
|
+
on('diskUUID', 'diskuuid', DiskUuid.local)
|
|
80
|
+
on('nfs', Nfs.local)
|
|
81
|
+
on('tmpfs', Tmpfs.local)
|
|
82
|
+
end,
|
|
61
83
|
)
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
84
|
+
attribute(:fstype, JsonModel::Types::String.enum('ext3', 'ext4', 'btrfs'))
|
|
85
|
+
attribute(
|
|
86
|
+
:options,
|
|
87
|
+
JsonModel::Types::Array.of(JsonModel::Types::String).constrained(min_size: 1, unique: true).optional,
|
|
88
|
+
)
|
|
89
|
+
attribute?(:readonly, JsonModel::Types::Bool.optional)
|
|
65
90
|
end,
|
|
66
91
|
)
|
|
67
92
|
end
|
|
@@ -73,7 +98,7 @@ RSpec.describe('File system schema') do
|
|
|
73
98
|
{
|
|
74
99
|
description: 'JSON Schema for an fstab entry',
|
|
75
100
|
type: 'object',
|
|
76
|
-
required: %i(storage),
|
|
101
|
+
required: %i(fstype storage),
|
|
77
102
|
properties: {
|
|
78
103
|
storage: {
|
|
79
104
|
oneOf: [
|
|
@@ -87,15 +112,23 @@ RSpec.describe('File system schema') do
|
|
|
87
112
|
enum: %w(ext3 ext4 btrfs),
|
|
88
113
|
},
|
|
89
114
|
options: {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
115
|
+
anyOf: [
|
|
116
|
+
{ type: 'null' },
|
|
117
|
+
{
|
|
118
|
+
type: 'array',
|
|
119
|
+
minItems: 1,
|
|
120
|
+
items: {
|
|
121
|
+
type: 'string',
|
|
122
|
+
},
|
|
123
|
+
uniqueItems: true,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
96
126
|
},
|
|
97
127
|
readonly: {
|
|
98
|
-
|
|
128
|
+
anyOf: [
|
|
129
|
+
{ type: 'null' },
|
|
130
|
+
{ type: 'boolean' },
|
|
131
|
+
],
|
|
99
132
|
},
|
|
100
133
|
},
|
|
101
134
|
'$defs': {
|
|
@@ -103,6 +136,7 @@ RSpec.describe('File system schema') do
|
|
|
103
136
|
properties: {
|
|
104
137
|
type: {
|
|
105
138
|
const: 'disk',
|
|
139
|
+
type: 'string',
|
|
106
140
|
},
|
|
107
141
|
device: {
|
|
108
142
|
type: 'string',
|
|
@@ -125,7 +159,10 @@ RSpec.describe('File system schema') do
|
|
|
125
159
|
},
|
|
126
160
|
Nfs: {
|
|
127
161
|
properties: {
|
|
128
|
-
type: {
|
|
162
|
+
type: {
|
|
163
|
+
const: 'nfs',
|
|
164
|
+
type: 'string',
|
|
165
|
+
},
|
|
129
166
|
remotePath: {
|
|
130
167
|
type: 'string',
|
|
131
168
|
pattern: '\\A(/[^/]+)+\\z',
|
|
@@ -140,7 +177,10 @@ RSpec.describe('File system schema') do
|
|
|
140
177
|
},
|
|
141
178
|
Tmpfs: {
|
|
142
179
|
properties: {
|
|
143
|
-
type: {
|
|
180
|
+
type: {
|
|
181
|
+
const: 'tmpfs',
|
|
182
|
+
type: 'string',
|
|
183
|
+
},
|
|
144
184
|
sizeInMB: { type: 'integer', minimum: 16, maximum: 512 },
|
|
145
185
|
},
|
|
146
186
|
required: %i(sizeInMB type),
|
data/spec/examples/user_spec.rb
CHANGED
|
@@ -4,38 +4,38 @@ require('spec_helper')
|
|
|
4
4
|
|
|
5
5
|
RSpec.describe('User schema') do
|
|
6
6
|
before do
|
|
7
|
-
address_class = Class.new do
|
|
7
|
+
address_class = Class.new(Dry::Struct) do
|
|
8
8
|
include(JsonModel::Schema)
|
|
9
9
|
|
|
10
10
|
def self.name
|
|
11
11
|
'Address'
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
attribute(:street, JsonModel::Types::String)
|
|
15
|
+
attribute(:city, JsonModel::Types::String)
|
|
16
|
+
attribute?(:state, JsonModel::Types::String.optional)
|
|
17
|
+
attribute?(:postal_code, JsonModel::Types::String.constrained(format: /\A\d{5}(-\d{4})?\z/).optional)
|
|
18
|
+
attribute(:country, JsonModel::Types::String.default('USA'))
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
stub_const('Address', address_class)
|
|
22
22
|
|
|
23
|
-
user_class = Class.new do
|
|
23
|
+
user_class = Class.new(Dry::Struct) do
|
|
24
24
|
include(JsonModel::Schema)
|
|
25
25
|
|
|
26
26
|
def self.name
|
|
27
27
|
'User'
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
attribute(:name, JsonModel::Types::String.constrained(min_size: 3))
|
|
31
|
+
attribute(:email, JsonModel::Types::Email)
|
|
32
|
+
attribute?(:age, JsonModel::Types::Integer.constrained(gteq: 0, lteq: 120).optional)
|
|
33
|
+
attribute(:active, JsonModel::Types::Bool.optional.default(true))
|
|
34
|
+
attribute(:addresses, JsonModel::Types::Array.of(Address.local))
|
|
35
|
+
attribute?(:tags, JsonModel::Types::Array.of(JsonModel::Types::String).optional)
|
|
36
|
+
attribute(:birthday, JsonModel::Types::Date.optional)
|
|
37
|
+
attribute?(:websites, JsonModel::Types::Array.of(JsonModel::Types::URI).optional)
|
|
38
|
+
attribute?(:height, JsonModel::Types::Float.optional)
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
stub_const('User', user_class)
|
|
@@ -48,18 +48,49 @@ RSpec.describe('User schema') do
|
|
|
48
48
|
{
|
|
49
49
|
type: 'object',
|
|
50
50
|
properties: {
|
|
51
|
-
name: { type: 'string' },
|
|
51
|
+
name: { type: 'string', minLength: 3 },
|
|
52
52
|
email: { type: 'string', format: 'email' },
|
|
53
|
-
age: {
|
|
54
|
-
|
|
53
|
+
age: {
|
|
54
|
+
anyOf: [
|
|
55
|
+
{ type: 'null' },
|
|
56
|
+
{ type: 'integer', minimum: 0, maximum: 120 },
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
active: {
|
|
60
|
+
anyOf: [
|
|
61
|
+
{ type: 'null' },
|
|
62
|
+
{ type: 'boolean' },
|
|
63
|
+
],
|
|
64
|
+
default: true,
|
|
65
|
+
},
|
|
55
66
|
addresses: {
|
|
56
67
|
type: 'array',
|
|
57
68
|
items: { '$ref': '#/$defs/Address' },
|
|
58
69
|
},
|
|
59
|
-
tags: {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
tags: {
|
|
71
|
+
anyOf: [
|
|
72
|
+
{ type: 'null' },
|
|
73
|
+
{ type: 'array', items: { type: 'string' } },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
birthday: {
|
|
77
|
+
anyOf: [
|
|
78
|
+
{ type: 'null' },
|
|
79
|
+
{ type: 'string', format: 'date' },
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
websites: {
|
|
83
|
+
anyOf: [
|
|
84
|
+
{ type: 'null' },
|
|
85
|
+
{ type: 'array', items: { type: 'string', format: 'uri' } },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
height: {
|
|
89
|
+
anyOf: [
|
|
90
|
+
{ type: 'null' },
|
|
91
|
+
{ type: 'number' },
|
|
92
|
+
],
|
|
93
|
+
},
|
|
63
94
|
},
|
|
64
95
|
required: %i(addresses email name),
|
|
65
96
|
'$defs': {
|
|
@@ -68,8 +99,18 @@ RSpec.describe('User schema') do
|
|
|
68
99
|
properties: {
|
|
69
100
|
city: { type: 'string' },
|
|
70
101
|
country: { type: 'string', default: 'USA' },
|
|
71
|
-
postal_code: {
|
|
72
|
-
|
|
102
|
+
postal_code: {
|
|
103
|
+
anyOf: [
|
|
104
|
+
{ type: 'null' },
|
|
105
|
+
{ type: 'string', pattern: '\A\d{5}(-\d{4})?\z' },
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
state: {
|
|
109
|
+
anyOf: [
|
|
110
|
+
{ type: 'null' },
|
|
111
|
+
{ type: 'string' },
|
|
112
|
+
],
|
|
113
|
+
},
|
|
73
114
|
street: { type: 'string' },
|
|
74
115
|
},
|
|
75
116
|
required: %i(city country street),
|
|
@@ -85,7 +126,7 @@ RSpec.describe('User schema') do
|
|
|
85
126
|
name: 'Foo',
|
|
86
127
|
email: 'foo@example.com',
|
|
87
128
|
addresses: [{ street: '123 Main St', city: 'Anytown' }],
|
|
88
|
-
birthday:
|
|
129
|
+
birthday: Date.new(2000, 1, 1),
|
|
89
130
|
)
|
|
90
131
|
|
|
91
132
|
expect(user.name).to(eq('Foo'))
|
data/spec/schema_meta_spec.rb
CHANGED
|
@@ -95,44 +95,4 @@ RSpec.describe(JsonModel::SchemaMeta) do
|
|
|
95
95
|
.to(eq('This is a test title.'))
|
|
96
96
|
end
|
|
97
97
|
end
|
|
98
|
-
|
|
99
|
-
describe('.additional_properties') do
|
|
100
|
-
let(:klass) do
|
|
101
|
-
Class.new do
|
|
102
|
-
include(JsonModel::SchemaMeta)
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
it('is missing by default') do
|
|
107
|
-
expect(klass.meta_attributes)
|
|
108
|
-
.to(eq({}))
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
it('can be changed') do
|
|
112
|
-
klass.additional_properties(true)
|
|
113
|
-
|
|
114
|
-
expect(klass.meta_attributes)
|
|
115
|
-
.to(eq({ additionalProperties: true }))
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
describe('.additional_properties') do
|
|
120
|
-
let(:klass) do
|
|
121
|
-
Class.new do
|
|
122
|
-
include(JsonModel::SchemaMeta)
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
it('is missing by default') do
|
|
127
|
-
expect(klass.meta_attributes)
|
|
128
|
-
.to(eq({}))
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
it('can be changed') do
|
|
132
|
-
klass.unevaluated_properties(true)
|
|
133
|
-
|
|
134
|
-
expect(klass.meta_attributes)
|
|
135
|
-
.to(eq({ unevaluatedProperties: true }))
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
98
|
end
|
data/spec/schema_spec.rb
CHANGED
|
@@ -5,10 +5,10 @@ require('spec_helper')
|
|
|
5
5
|
RSpec.describe(JsonModel::Schema) do
|
|
6
6
|
describe('.initialize') do
|
|
7
7
|
let(:klass) do
|
|
8
|
-
Class.new do
|
|
8
|
+
Class.new(Dry::Struct) do
|
|
9
9
|
include(JsonModel::Schema)
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
attribute?(:foo, JsonModel::Types::String.optional)
|
|
12
12
|
|
|
13
13
|
def self.name
|
|
14
14
|
'Foo'
|
|
@@ -28,49 +28,25 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it('raises an error for unknown attributes when additional properties are not allowed') do
|
|
31
|
+
klass.schema(klass.schema.strict)
|
|
32
|
+
|
|
31
33
|
expect { klass.new(bar: 'baz') }
|
|
32
|
-
.to(raise_error(
|
|
34
|
+
.to(raise_error(Dry::Struct::Error))
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
it('does not raise an error for unknown attributes when additional properties are allowed') do
|
|
36
|
-
klass.additional_properties(true)
|
|
37
|
-
|
|
38
38
|
klass.new(bar: 'baz')
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
describe('#valid?') do
|
|
43
|
-
let(:klass) do
|
|
44
|
-
Class.new do
|
|
45
|
-
include(JsonModel::Schema)
|
|
46
|
-
|
|
47
|
-
property(:foo, type: T::String[min_length: 3])
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
before do
|
|
52
|
-
JsonModel.configure { |config| config.validate_after_instantiation = false }
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
it('returns false for invalid values') do
|
|
56
|
-
expect(klass.new(foo: 'ba').valid?)
|
|
57
|
-
.to(be(false))
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
it('returns true for valid values') do
|
|
61
|
-
expect(klass.new(foo: 'bar').valid?)
|
|
62
|
-
.to(be(true))
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
42
|
describe('.from_json') do
|
|
67
43
|
before do
|
|
68
44
|
stub_const(
|
|
69
45
|
'Foo',
|
|
70
|
-
Class.new do
|
|
46
|
+
Class.new(Dry::Struct) do
|
|
71
47
|
include(JsonModel::Schema)
|
|
72
48
|
|
|
73
|
-
|
|
49
|
+
attribute(:foo_bar, JsonModel::Types::String.as(:fooBar))
|
|
74
50
|
end,
|
|
75
51
|
)
|
|
76
52
|
|
|
@@ -93,7 +69,7 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
93
69
|
|
|
94
70
|
describe('.as_schema') do
|
|
95
71
|
let(:klass) do
|
|
96
|
-
Class.new do
|
|
72
|
+
Class.new(Dry::Struct) do
|
|
97
73
|
include(JsonModel::Schema)
|
|
98
74
|
end
|
|
99
75
|
end
|
|
@@ -119,11 +95,11 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
119
95
|
|
|
120
96
|
it('returns properties as schema') do
|
|
121
97
|
klass.schema_id('https://example.com/schemas/example.json')
|
|
122
|
-
klass.
|
|
123
|
-
klass.
|
|
124
|
-
klass.
|
|
125
|
-
klass.
|
|
126
|
-
klass.
|
|
98
|
+
klass.attribute(:foo, JsonModel::Types::String)
|
|
99
|
+
klass.attribute(:bar, JsonModel::Types::Float.optional)
|
|
100
|
+
klass.attribute(:baz, JsonModel::Types::String.enum(1, 'a', nil))
|
|
101
|
+
klass.attribute(:bam, JsonModel::Types::Array.of(JsonModel::Types::String & JsonModel::Types::Float))
|
|
102
|
+
klass.attribute(:bal, klass.external.optional)
|
|
127
103
|
|
|
128
104
|
expect(klass.as_schema)
|
|
129
105
|
.to(
|
|
@@ -132,14 +108,24 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
132
108
|
'$id': 'https://example.com/schemas/example.json',
|
|
133
109
|
type: 'object',
|
|
134
110
|
properties: {
|
|
135
|
-
bal: {
|
|
111
|
+
bal: {
|
|
112
|
+
anyOf: [
|
|
113
|
+
{ type: 'null' },
|
|
114
|
+
{ '$ref': 'https://example.com/schemas/example.json' },
|
|
115
|
+
],
|
|
116
|
+
},
|
|
136
117
|
bam: {
|
|
137
118
|
type: 'array',
|
|
138
119
|
items: {
|
|
139
120
|
allOf: [{ type: 'string' }, { type: 'number' }],
|
|
140
121
|
},
|
|
141
122
|
},
|
|
142
|
-
bar: {
|
|
123
|
+
bar: {
|
|
124
|
+
anyOf: [
|
|
125
|
+
{ type: 'null' },
|
|
126
|
+
{ type: 'number' },
|
|
127
|
+
],
|
|
128
|
+
},
|
|
143
129
|
baz: { enum: [1, 'a', nil] },
|
|
144
130
|
foo: { type: 'string' },
|
|
145
131
|
},
|
|
@@ -150,40 +136,38 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
150
136
|
end
|
|
151
137
|
|
|
152
138
|
it('collects local references in $defs') do
|
|
153
|
-
klass.
|
|
139
|
+
klass.attribute(
|
|
154
140
|
:foo,
|
|
155
|
-
|
|
141
|
+
Class.new(Dry::Struct) do
|
|
156
142
|
include(JsonModel::Schema)
|
|
157
143
|
|
|
158
|
-
|
|
144
|
+
attribute(:foo, JsonModel::Types::String)
|
|
159
145
|
end,
|
|
160
146
|
)
|
|
161
|
-
klass.
|
|
147
|
+
klass.attribute(
|
|
162
148
|
:bam,
|
|
163
|
-
|
|
149
|
+
Class.new(Dry::Struct) do
|
|
164
150
|
include(JsonModel::Schema)
|
|
165
151
|
|
|
166
|
-
property(:bam, type: String)
|
|
167
152
|
schema_id('https://example.com/schemas/bam.json')
|
|
168
|
-
|
|
169
|
-
|
|
153
|
+
attribute(:bam, JsonModel::Types::String)
|
|
154
|
+
end.external,
|
|
170
155
|
)
|
|
171
|
-
klass.
|
|
156
|
+
klass.attribute(
|
|
172
157
|
:bar,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
Class.new do
|
|
158
|
+
JsonModel::Types::Array.of(
|
|
159
|
+
JsonModel::Types::Array.of(
|
|
160
|
+
Class.new(Dry::Struct) do
|
|
176
161
|
include(JsonModel::Schema)
|
|
177
162
|
|
|
178
|
-
|
|
163
|
+
attribute(:bar, JsonModel::Types::String)
|
|
179
164
|
|
|
180
165
|
def self.name
|
|
181
166
|
'Bar'
|
|
182
167
|
end
|
|
183
|
-
end,
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
ref_mode: JsonModel::RefMode::LOCAL,
|
|
168
|
+
end.local,
|
|
169
|
+
),
|
|
170
|
+
),
|
|
187
171
|
)
|
|
188
172
|
|
|
189
173
|
expect(klass.as_schema)
|
|
@@ -223,20 +207,20 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
223
207
|
let(:child) do
|
|
224
208
|
Class.new(klass) do
|
|
225
209
|
schema_id('https://example.com/schemas/child.json')
|
|
226
|
-
|
|
210
|
+
attribute(:baz, JsonModel::Types::String)
|
|
227
211
|
end
|
|
228
212
|
end
|
|
229
213
|
let(:second_child) do
|
|
230
214
|
Class.new(klass) do
|
|
231
215
|
schema_id('https://example.com/schemas/second-child.json')
|
|
232
216
|
title('SecondChild')
|
|
233
|
-
|
|
217
|
+
attribute(:bar, JsonModel::Types::String)
|
|
234
218
|
end
|
|
235
219
|
end
|
|
236
220
|
|
|
237
221
|
it('uses $ref for inherited schemas if they have a schema id') do
|
|
238
222
|
klass.schema_id('https://example.com/schemas/example.json')
|
|
239
|
-
klass.
|
|
223
|
+
klass.attribute(:foo, JsonModel::Types::String)
|
|
240
224
|
|
|
241
225
|
expect(child.as_schema)
|
|
242
226
|
.to(
|