schemacop 3.0.0.rc2 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,8 +25,10 @@ module Schemacop
25
25
 
26
26
  def test_schemas
27
27
  assert_equal({}, GlobalContext.instance.schemas)
28
+ assert_equal({}, GlobalContext.schemas)
28
29
  GlobalContext.instance.eager_load!
29
30
  assert_equal(%i[nested/group user], GlobalContext.instance.schemas.keys)
31
+ assert_equal(%i[nested/group user], GlobalContext.schemas.keys)
30
32
  assert_is_a Node, GlobalContext.instance.schemas.values.first
31
33
  end
32
34
 
@@ -16,7 +16,7 @@ module Schemacop
16
16
  assert_json(type: :object, additionalProperties: false)
17
17
  end
18
18
 
19
- def test_additional_properties_false
19
+ def test_additional_properties_false_by_default
20
20
  schema
21
21
  assert_validation({})
22
22
  assert_validation(foo: :bar, bar: :baz) do
@@ -26,6 +26,17 @@ module Schemacop
26
26
  assert_json(type: :object, additionalProperties: false)
27
27
  end
28
28
 
29
+ def test_additional_properties_false
30
+ schema :hash, additional_properties: false
31
+
32
+ assert_validation({})
33
+ assert_validation(foo: :bar, bar: :baz) do
34
+ error '/', 'Obsolete property "foo".'
35
+ error '/', 'Obsolete property "bar".'
36
+ end
37
+ assert_json(type: :object, additionalProperties: false)
38
+ end
39
+
29
40
  def test_additional_properties_true
30
41
  schema :hash, additional_properties: true
31
42
  assert_validation({})
@@ -874,11 +885,88 @@ module Schemacop
874
885
  end
875
886
  end
876
887
 
877
- # def test_invalid_key_names
878
- # schema :hash do
879
- # int!
880
- # end
881
- # end
888
+ def test_invalid_options
889
+ assert_raises_with_message Schemacop::Exceptions::InvalidSchemaError, 'Options [:foo] are not allowed for this node.' do
890
+ schema :hash, foo: 'bar' do
891
+ int! :id
892
+ end
893
+ end
894
+ end
895
+
896
+ def test_casting_optional
897
+ schema :hash do
898
+ str? :foo, format: :integer
899
+ end
900
+
901
+ assert_validation(nil)
902
+ assert_validation({})
903
+
904
+ assert_cast(nil, nil)
905
+ assert_cast({}, {}.with_indifferent_access)
906
+ end
907
+
908
+ def test_as_option
909
+ schema :hash do
910
+ int! :foo, as: :bar
911
+ end
912
+
913
+ assert_validation(nil)
914
+ assert_validation({ foo: 42 })
915
+ assert_validation({ bar: 13 }) do
916
+ error '/', 'Obsolete property "bar".'
917
+ error '/foo', 'Value must be given.'
918
+ end
919
+
920
+ assert_validation({ foo: '13' }) do
921
+ error '/foo', 'Invalid type, expected "integer".'
922
+ end
923
+
924
+ assert_cast(nil, nil)
925
+ assert_cast({ foo: 42 }, { bar: 42 }.with_indifferent_access)
926
+ end
927
+
928
+ def test_as_option_overriding
929
+ schema :hash do
930
+ int? :foo, as: :bar
931
+ int? :bar
932
+ end
933
+
934
+ assert_validation(nil)
935
+ assert_validation({})
936
+ assert_validation({ foo: 42 })
937
+ assert_validation({ foo: 42, bar: 13 })
938
+
939
+ assert_validation({ foo: '13' }) do
940
+ error '/foo', 'Invalid type, expected "integer".'
941
+ end
942
+
943
+ # assert_cast(nil, nil)
944
+ assert_cast({ foo: 42 }, { bar: 42 }.with_indifferent_access)
945
+ assert_cast({ foo: 42, bar: 13 }, { bar: 13 }.with_indifferent_access)
946
+ assert_cast({ bar: 13, foo: 42 }, { bar: 13 }.with_indifferent_access)
947
+
948
+ # Swap order
949
+ schema :hash do
950
+ int? :bar
951
+ int! :foo, as: :bar
952
+ end
953
+
954
+ assert_validation(nil)
955
+ assert_validation({ foo: 42 })
956
+ assert_validation({ foo: 42, bar: 13 })
957
+ assert_validation({ bar: 13 }) do
958
+ error '/foo', 'Value must be given.'
959
+ end
960
+
961
+ assert_validation({ foo: '13' }) do
962
+ error '/foo', 'Invalid type, expected "integer".'
963
+ end
964
+
965
+ assert_cast(nil, nil)
966
+ assert_cast({ foo: 42 }, { bar: 42 }.with_indifferent_access)
967
+ assert_cast({ foo: 42, bar: 13 }, { bar: 42 }.with_indifferent_access)
968
+ assert_cast({ bar: 13, foo: 42 }, { bar: 42 }.with_indifferent_access)
969
+ end
882
970
  end
883
971
  end
884
972
  end
@@ -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
@@ -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
@@ -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,26 @@ 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')
166
+ end
167
+
168
+ def test_format_symbol
169
+ schema :string, format: :symbol
170
+
171
+ assert_json(type: :string, format: :symbol)
172
+
173
+ assert_validation 'foobar'
174
+ assert_validation ''
175
+
176
+ assert_validation 234 do
177
+ error '/', 'Invalid type, expected "string".'
178
+ end
179
+
180
+ assert_cast(nil, nil)
181
+ assert_cast('foobar', :foobar)
182
+ assert_cast('039n23$g- sfk3/', :'039n23$g- sfk3/')
157
183
  end
158
184
 
159
185
  def test_enum
@@ -187,18 +213,24 @@ module Schemacop
187
213
 
188
214
  assert_cast 'true', true
189
215
  assert_cast 'false', false
216
+
217
+ assert_cast nil, nil
190
218
  end
191
219
 
192
220
  def test_time_casting
193
221
  schema :string, format: :time
194
222
  assert_json(type: :string, format: :time)
195
223
  assert_cast '20:30:39+00:00', Time.strptime('20:30:39+00:00', '%H:%M:%S%z')
224
+
225
+ assert_cast nil, nil
196
226
  end
197
227
 
198
228
  def test_date_casting
199
229
  schema :string, format: :date
200
230
  assert_json(type: :string, format: :date)
201
231
  assert_cast '2018-11-13', Date.new(2018, 11, 13)
232
+
233
+ assert_cast nil, nil
202
234
  end
203
235
 
204
236
  def test_date_time_casting
@@ -207,12 +239,16 @@ module Schemacop
207
239
  assert_cast '2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39)
208
240
  assert_cast '2018-11-13T20:20:39Z', DateTime.new(2018, 11, 13, 20, 20, 39)
209
241
  assert_cast '2018-11-13T20:20:39+01:00', DateTime.new(2018, 11, 13, 20, 20, 39, '+1')
242
+
243
+ assert_cast nil, nil
210
244
  end
211
245
 
212
246
  def test_email_casting
213
247
  schema :string, format: :email
214
248
  assert_json(type: :string, format: :email)
215
249
  assert_cast 'support@example.com', 'support@example.com'
250
+
251
+ assert_cast nil, nil
216
252
  end
217
253
 
218
254
  def test_default
@@ -233,6 +269,25 @@ module Schemacop
233
269
  assert_cast(nil, 'Hello')
234
270
  end
235
271
 
272
+ def test_default_casting
273
+ schema :string, format: :integer, default: '42'
274
+
275
+ assert_json(
276
+ type: :string,
277
+ format: :integer,
278
+ default: '42'
279
+ )
280
+
281
+ assert_validation(nil)
282
+ assert_validation('123')
283
+ assert_validation(5) do
284
+ error '/', 'Invalid type, expected "string".'
285
+ end
286
+
287
+ assert_cast('123', 123)
288
+ assert_cast(nil, 42)
289
+ end
290
+
236
291
  # Helper function that checks for all the options if the option is
237
292
  # an integer or something else, in which case it needs to raise
238
293
  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.rc2
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-28 00:00:00.000000000 Z
11
+ date: 2021-02-11 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
@@ -150,8 +164,8 @@ dependencies:
150
164
  - - '='
151
165
  - !ruby/object:Gem::Version
152
166
  version: 0.21.2
153
- description:
154
- email:
167
+ description:
168
+ email:
155
169
  executables: []
156
170
  extensions: []
157
171
  extra_rdoc_files: []
@@ -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
@@ -264,7 +277,7 @@ homepage: https://github.com/sitrox/schemacop
264
277
  licenses:
265
278
  - MIT
266
279
  metadata: {}
267
- post_install_message:
280
+ post_install_message:
268
281
  rdoc_options: []
269
282
  require_paths:
270
283
  - lib
@@ -275,12 +288,12 @@ 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
- rubygems_version: 3.0.3
283
- signing_key:
295
+ rubygems_version: 3.1.4
296
+ signing_key:
284
297
  specification_version: 4
285
298
  summary: Schemacop validates ruby structures consisting of nested hashes and arrays
286
299
  against simple schema definitions.
@@ -1,6 +0,0 @@
1
- module Schemacop
2
- module V2
3
- class RootNode < NodeSupportingType
4
- end
5
- end
6
- end