schemacop 3.0.0.rc0 → 3.0.0.rc5

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(
@@ -286,66 +288,80 @@ module Schemacop
286
288
  end
287
289
  end
288
290
 
289
- # def test_external_schemas
290
- # context = Context.new
291
-
292
- # context.schema :Person do
293
- # str! :first_name
294
- # str! :last_name
295
- # ref? :info, :PersonInfo
296
- # end
297
-
298
- # context.schema :PersonInfo do
299
- # str! :born_at, format: :date
300
- # end
301
-
302
- # schema :reference, path: :Person
303
-
304
- # with_context context do
305
- # assert_validation(first_name: 'John', last_name: 'Doe')
306
- # assert_validation(first_name: 'John', last_name: 42) do
307
- # error '/last_name', 'Invalid type, expected "string".'
308
- # end
309
- # end
310
-
311
- # with_context context do
312
- # schema do
313
- # ref! :person, :Person
314
- # end
315
-
316
- # assert_validation(person: { first_name: 'John', last_name: 'Doe' })
317
- # assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: '1990-01-13' } })
318
- # assert_validation(person: { first_name_x: 'John', last_name: 'Doe' }) do
319
- # error '/person', 'Obsolete property "first_name_x".'
320
- # error '/person/first_name', 'Value must be given.'
321
- # end
322
- # assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: 'never' } }) do
323
- # error '/person/info/born_at', 'String does not match format "date".'
324
- # end
325
- # end
326
- # end
327
-
328
- # def test_defaults
329
- # schema do
330
- # scm :Person do
331
- # str? :foo, default: 'bar'
332
- # end
333
- # ref? :person, :Person, default: {}
334
- # end
335
-
336
- # assert_cast({}, person: { foo: 'bar' })
337
- # end
338
-
339
- # def test_casting
340
- # schema do
341
- # scm :Person do
342
- # str! :born_at, format: :date
343
- # end
344
- # ref? :person, :Person, default: {}
345
- # end
346
-
347
- # assert_cast({ person: { born_at: '1990-01-13' } }, person: { born_at: Date.new(1990, 1, 13) })
348
- # end
291
+ def test_external_schemas
292
+ context = Context.new
293
+
294
+ context.schema :Person do
295
+ str! :first_name
296
+ str! :last_name
297
+ ref? :info, :PersonInfo
298
+ end
299
+
300
+ context.schema :PersonInfo do
301
+ str! :born_at, format: :date
302
+ end
303
+
304
+ schema :reference, path: :Person
305
+
306
+ with_context context do
307
+ assert_validation(first_name: 'John', last_name: 'Doe')
308
+ assert_validation(first_name: 'John', last_name: 42) do
309
+ error '/last_name', 'Invalid type, expected "string".'
310
+ end
311
+ end
312
+
313
+ with_context context do
314
+ schema do
315
+ ref! :person, :Person
316
+ end
317
+
318
+ assert_equal(@schema.root.used_external_schemas, %i[Person PersonInfo])
319
+
320
+ assert_validation(person: { first_name: 'John', last_name: 'Doe' })
321
+ assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: '1990-01-13' } })
322
+ assert_validation(person: { first_name_x: 'John', last_name: 'Doe' }) do
323
+ error '/person', 'Obsolete property "first_name_x".'
324
+ error '/person/first_name', 'Value must be given.'
325
+ end
326
+ assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: 'never' } }) do
327
+ error '/person/info/born_at', 'String does not match format "date".'
328
+ end
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
342
+ end
343
+
344
+ def test_defaults
345
+ schema do
346
+ scm :Person do
347
+ str? :foo, default: 'bar'
348
+ end
349
+ ref? :person, :Person, default: {}
350
+ end
351
+
352
+ assert_cast({}, { person: { foo: 'bar' } }.with_indifferent_access)
353
+ end
354
+
355
+ def test_casting
356
+ schema do
357
+ scm :Person do
358
+ str! :born_at, format: :date
359
+ end
360
+ ref? :person, :Person, default: {}
361
+ end
362
+
363
+ assert_cast({ person: { born_at: '1990-01-13' } }, { person: { born_at: Date.new(1990, 1, 13) } }.with_indifferent_access)
364
+ end
349
365
  end
350
366
  end
351
367
  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.rc0
4
+ version: 3.0.0.rc5
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-14 00:00:00.000000000 Z
11
+ date: 2021-02-05 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
@@ -1,6 +0,0 @@
1
- module Schemacop
2
- module V2
3
- class RootNode < NodeSupportingType
4
- end
5
- end
6
- end