schemacop 3.0.0.rc1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -143,6 +143,20 @@ module Schemacop
143
143
  error '/[0]', 'Value must be given.'
144
144
  end
145
145
  end
146
+
147
+ def test_not_support_block
148
+ assert_raises_with_message Schemacop::Exceptions::InvalidSchemaError, 'Node Schemacop::V3::IntegerNode does not support blocks.' do
149
+ schema :integer do
150
+ int :foo
151
+ end
152
+ end
153
+ end
154
+
155
+ def test_node_no_children
156
+ @schema = Schemacop::Schema3.new
157
+
158
+ assert_equal(@schema.root.children, [])
159
+ end
146
160
  end
147
161
  end
148
162
  end
@@ -102,11 +102,11 @@ module Schemacop
102
102
 
103
103
  assert_cast(
104
104
  { created_at: '2020-01-01' },
105
- created_at: Date.new(2020, 1, 1)
105
+ { created_at: Date.new(2020, 1, 1) }.with_indifferent_access
106
106
  )
107
107
  assert_cast(
108
108
  { created_at: '2020-01-01T17:38:20' },
109
- created_at: DateTime.new(2020, 1, 1, 17, 38, 20)
109
+ { created_at: DateTime.new(2020, 1, 1, 17, 38, 20) }.with_indifferent_access
110
110
  )
111
111
  end
112
112
 
@@ -127,12 +127,12 @@ module Schemacop
127
127
 
128
128
  assert_cast(
129
129
  { foo: { bar: nil } },
130
- foo: { bar: nil }
130
+ { foo: { bar: nil } }.with_indifferent_access
131
131
  )
132
132
 
133
133
  assert_cast(
134
134
  { foo: { baz: nil } },
135
- foo: { baz: 'Baz' }
135
+ { foo: { baz: 'Baz' } }.with_indifferent_access
136
136
  )
137
137
 
138
138
  schema do
@@ -144,12 +144,12 @@ module Schemacop
144
144
 
145
145
  assert_cast(
146
146
  { foo: { bar: '1990-01-13' } },
147
- foo: { bar: Date.new(1990, 1, 13) }
147
+ { foo: { bar: Date.new(1990, 1, 13) } }.with_indifferent_access
148
148
  )
149
149
 
150
150
  assert_cast(
151
151
  { foo: { bar: '1990-01-13T10:00:00Z' } },
152
- foo: { bar: DateTime.new(1990, 1, 13, 10, 0, 0) }
152
+ { foo: { bar: DateTime.new(1990, 1, 13, 10, 0, 0) } }.with_indifferent_access
153
153
  )
154
154
  end
155
155
 
@@ -249,6 +249,8 @@ module Schemacop
249
249
  ref? :node, :Node
250
250
  end
251
251
 
252
+ assert_equal(@schema.root.used_external_schemas, [])
253
+
252
254
  assert_validation({})
253
255
  assert_validation(node: { name: '1', children: [{ name: '1' }, { name: '2' }] })
254
256
  assert_validation(
@@ -313,6 +315,8 @@ module Schemacop
313
315
  ref! :person, :Person
314
316
  end
315
317
 
318
+ assert_equal(@schema.root.used_external_schemas, %i[Person PersonInfo])
319
+
316
320
  assert_validation(person: { first_name: 'John', last_name: 'Doe' })
317
321
  assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: '1990-01-13' } })
318
322
  assert_validation(person: { first_name_x: 'John', last_name: 'Doe' }) do
@@ -323,6 +327,18 @@ module Schemacop
323
327
  error '/person/info/born_at', 'String does not match format "date".'
324
328
  end
325
329
  end
330
+
331
+ with_context context do
332
+ schema do
333
+ scm :PersonNode do
334
+ ref! :person, :Person
335
+ end
336
+
337
+ ref! :personNode, :PersonNode
338
+ end
339
+
340
+ assert_equal(@schema.root.used_external_schemas, %i[Person PersonInfo])
341
+ end
326
342
  end
327
343
 
328
344
  def test_defaults
@@ -333,7 +349,7 @@ module Schemacop
333
349
  ref? :person, :Person, default: {}
334
350
  end
335
351
 
336
- assert_cast({}, person: { foo: 'bar' })
352
+ assert_cast({}, { person: { foo: 'bar' } }.with_indifferent_access)
337
353
  end
338
354
 
339
355
  def test_casting
@@ -344,7 +360,7 @@ module Schemacop
344
360
  ref? :person, :Person, default: {}
345
361
  end
346
362
 
347
- assert_cast({ person: { born_at: '1990-01-13' } }, person: { born_at: Date.new(1990, 1, 13) })
363
+ assert_cast({ person: { born_at: '1990-01-13' } }, { person: { born_at: Date.new(1990, 1, 13) } }.with_indifferent_access)
348
364
  end
349
365
  end
350
366
  end
@@ -111,6 +111,9 @@ module Schemacop
111
111
  assert_validation 'foo 2020-01-29 bar' do
112
112
  error '/', 'String does not match format "date".'
113
113
  end
114
+
115
+ assert_cast(nil, nil)
116
+ assert_cast('2020-01-13', Date.new(2020, 1, 13))
114
117
  end
115
118
 
116
119
  def test_format_date_time
@@ -132,6 +135,9 @@ module Schemacop
132
135
  assert_validation '2018-11-13T20:20:39Y' do
133
136
  error '/', 'String does not match format "date-time".'
134
137
  end
138
+
139
+ assert_cast(nil, nil)
140
+ assert_cast('2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39))
135
141
  end
136
142
 
137
143
  def test_format_email
@@ -154,6 +160,9 @@ module Schemacop
154
160
  assert_validation '@john@example.com' do
155
161
  error '/', 'String does not match format "email".'
156
162
  end
163
+
164
+ assert_cast(nil, nil)
165
+ assert_cast('john.doe@example.com', 'john.doe@example.com')
157
166
  end
158
167
 
159
168
  def test_enum
@@ -187,18 +196,24 @@ module Schemacop
187
196
 
188
197
  assert_cast 'true', true
189
198
  assert_cast 'false', false
199
+
200
+ assert_cast nil, nil
190
201
  end
191
202
 
192
203
  def test_time_casting
193
204
  schema :string, format: :time
194
205
  assert_json(type: :string, format: :time)
195
206
  assert_cast '20:30:39+00:00', Time.strptime('20:30:39+00:00', '%H:%M:%S%z')
207
+
208
+ assert_cast nil, nil
196
209
  end
197
210
 
198
211
  def test_date_casting
199
212
  schema :string, format: :date
200
213
  assert_json(type: :string, format: :date)
201
214
  assert_cast '2018-11-13', Date.new(2018, 11, 13)
215
+
216
+ assert_cast nil, nil
202
217
  end
203
218
 
204
219
  def test_date_time_casting
@@ -207,12 +222,16 @@ module Schemacop
207
222
  assert_cast '2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39)
208
223
  assert_cast '2018-11-13T20:20:39Z', DateTime.new(2018, 11, 13, 20, 20, 39)
209
224
  assert_cast '2018-11-13T20:20:39+01:00', DateTime.new(2018, 11, 13, 20, 20, 39, '+1')
225
+
226
+ assert_cast nil, nil
210
227
  end
211
228
 
212
229
  def test_email_casting
213
230
  schema :string, format: :email
214
231
  assert_json(type: :string, format: :email)
215
232
  assert_cast 'support@example.com', 'support@example.com'
233
+
234
+ assert_cast nil, nil
216
235
  end
217
236
 
218
237
  def test_default
@@ -233,6 +252,25 @@ module Schemacop
233
252
  assert_cast(nil, 'Hello')
234
253
  end
235
254
 
255
+ def test_default_casting
256
+ schema :string, format: :integer, default: '42'
257
+
258
+ assert_json(
259
+ type: :string,
260
+ format: :integer,
261
+ default: '42'
262
+ )
263
+
264
+ assert_validation(nil)
265
+ assert_validation('123')
266
+ assert_validation(5) do
267
+ error '/', 'Invalid type, expected "string".'
268
+ end
269
+
270
+ assert_cast('123', 123)
271
+ assert_cast(nil, 42)
272
+ end
273
+
236
274
  # Helper function that checks for all the options if the option is
237
275
  # an integer or something else, in which case it needs to raise
238
276
  def validate_self_should_error(value_to_check)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemacop
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.rc1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby2_keywords
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.4
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -188,7 +202,6 @@ files:
188
202
  - lib/schemacop/v2/node_supporting_field.rb
189
203
  - lib/schemacop/v2/node_supporting_type.rb
190
204
  - lib/schemacop/v2/node_with_block.rb
191
- - lib/schemacop/v2/root_node.rb
192
205
  - lib/schemacop/v2/validator/array_validator.rb
193
206
  - lib/schemacop/v2/validator/boolean_validator.rb
194
207
  - lib/schemacop/v2/validator/float_validator.rb
@@ -275,9 +288,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
275
288
  version: '0'
276
289
  required_rubygems_version: !ruby/object:Gem::Requirement
277
290
  requirements:
278
- - - ">"
291
+ - - ">="
279
292
  - !ruby/object:Gem::Version
280
- version: 1.3.1
293
+ version: '0'
281
294
  requirements: []
282
295
  rubygems_version: 3.0.3
283
296
  signing_key:
@@ -1,6 +0,0 @@
1
- module Schemacop
2
- module V2
3
- class RootNode < NodeSupportingType
4
- end
5
- end
6
- end