schemacop 3.0.10 → 3.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8700be126ea3aced42828ce418ad665095584ba0e217eebb0cf2e42faf5b4aef
4
- data.tar.gz: 9774973e128d64f02a06e84cfa19990b0a7b0890cf57d4fb94c0f5058337dd20
3
+ metadata.gz: 40958dccae4c661f133f181907cc729c0002ca02702a6675c1beb1c90fed425e
4
+ data.tar.gz: 4ef5dcf205bd5f5cc8203867e95d07ff5a56a8c4654f3eac89e90596cb9a2e39
5
5
  SHA512:
6
- metadata.gz: 0cdd515a0d47c7d707b8418fa5f0c7e6e73375ccb28bd2552b8c5f960e0279c75fb3a89c99b4ff677069c7a75e3d2f2376645d720d0d65980688602af09e4d28
7
- data.tar.gz: 27cb5b5b8c4c8e14601c907c443d4fd904457ad0762c31af26aabf7d75febbf49459555e19665a1e56674b05601b4a223ccc18cb12fcebf9fddd526c3ec37442
6
+ metadata.gz: 1af517385c88340f64067895363b6e79d2ce15e00415ef5d0110200706b2e264701e6a0eeac9332e2ea8d9c5967989e8e05a9e311291a1e942d1effc5cc15219
7
+ data.tar.gz: c773bd33014ae3a8ccb4f1cd22c2066b375de678fdde702d2d95dcbc78880bd6d19940bb06b8052b06f955bc740834b7761f5c389d21bec56250ee18a4680ce6
data/.releaser_config CHANGED
@@ -1,4 +1,4 @@
1
1
  version_file: VERSION
2
2
  always_from_master: true
3
3
  gem_style: github
4
- ruby_command: ruby -S
4
+ ruby_command: ruby -S
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change log
2
2
 
3
+ ## 3.0.11 (2021-03-26)
4
+
5
+ * Treat blank string as nil when using `cast_str` option
6
+
3
7
  ## 3.0.10 (2021-03-19)
4
8
 
5
9
  * If wrong type is given, report class of wrong type in error message
data/README_V3.md CHANGED
@@ -317,7 +317,7 @@ integer can be done.
317
317
  * `cast_str`
318
318
  When set to `true`, this node also accepts strings that can be casted to an integer, e.g.
319
319
  the values `'-5'` or `'42'`. Please note that you can only validate numbers which
320
- are in the `Integer` format.
320
+ are in the `Integer` format. Blank strings will be treated equally as `nil`.
321
321
 
322
322
  #### Examples
323
323
 
@@ -346,6 +346,19 @@ schema.validate!('102') # => Schemacop::Exceptions::ValidationError: /
346
346
  schema.validate!('42.1') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
347
347
  schema.validate!('4r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
348
348
  schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
349
+ schema.validate!(nil) # => nil
350
+ schema.validate!('') # => nil
351
+ ```
352
+
353
+ Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
354
+ and validating a blank string will return `nil`.
355
+ If you need a value, use the `required` option:
356
+
357
+ ```ruby
358
+ schema = Schemacop::Schema3.new(:integer, minimum: 0, maximum: 100, multiple_of: 2, cast_str: true, required: true)
359
+ schema.validate!('42') # => 42
360
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
361
+ schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
349
362
  ```
350
363
 
351
364
  ### Number
@@ -386,7 +399,7 @@ With the various available options, validations on the value of the number can b
386
399
  When set to `true`, this node also accepts strings that can be casted to a number, e.g.
387
400
  the values `'0.1'` or `'3.1415'`. Please note that you can only validate numbers which
388
401
  are in the `Integer` or `Float` format, i.e. values like `'1.5r'` or `'(4 + 0i)'` will
389
- not work.
402
+ not work. Blank strings will be treated equally as `nil`.
390
403
 
391
404
  #### Examples
392
405
 
@@ -414,6 +427,19 @@ schema.validate!('51') # => Schemacop::Exceptions::ValidationError: /: Ma
414
427
  schema.validate!('42.5') # => 42.5
415
428
  schema.validate!('1.5r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
416
429
  schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
430
+ schema.validate!(nil) # => nil
431
+ schema.validate!('') # => nil
432
+ ```
433
+
434
+ Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
435
+ and validating a blank string will return `nil`.
436
+ If you need a value, use the `required` option:
437
+
438
+ ```ruby
439
+ schema = Schemacop::Schema3.new(:number, cast_str: true, minimum: 0.0, maximum: (50r), multiple_of: BigDecimal('0.5'), require: true)
440
+ schema.validate!('42.5') # => 42.5
441
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
442
+ schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
417
443
  ```
418
444
 
419
445
  ### Symbol
@@ -426,7 +452,7 @@ The symbol type is used to validate elements for the Ruby `Symbol` class.
426
452
  #### Options
427
453
 
428
454
  * `cast_str`
429
- When set to `true`, this node also accepts strings that can be casted to a symbol.
455
+ When set to `true`, this node also accepts strings that can be casted to a symbol. Blank strings will be treated equally as `nil`.
430
456
 
431
457
  #### Examples
432
458
 
@@ -450,6 +476,19 @@ schema.validate!('foo') # => :foo
450
476
  schema.validate!('123') # => :"123"
451
477
  schema.validate!('false') # => :false
452
478
  schema.validate!(':false') # => :":false"
479
+ schema.validate!(nil) # => nil
480
+ schema.validate!('') # => nil
481
+ ```
482
+
483
+ Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
484
+ and validating a blank string will return `nil`.
485
+ If you need a value, use the `required` option:
486
+
487
+ ```ruby
488
+ schema = Schemacop::Schema3.new(:symbol, cast_str: true, required: true)
489
+ schema.validate!('foo') # => :foo
490
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
491
+ schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
453
492
  ```
454
493
 
455
494
  ### Boolean
@@ -463,7 +502,7 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
463
502
 
464
503
  * `cast_str`
465
504
  When set to `true`, this node also accepts strings that can be casted to a boolean, i.e.
466
- the values `'true'` and `'false'`
505
+ the values `'true'` and `'false'`. Blank strings will be treated equally as `nil`.
467
506
 
468
507
  #### Examples
469
508
 
@@ -486,6 +525,19 @@ schema.validate!(false) # => false
486
525
  schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
487
526
  schema.validate!('false') # => false
488
527
  schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
528
+ schema.validate!(nil) # => nil
529
+ schema.validate!('') # => nil
530
+ ```
531
+
532
+ Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
533
+ and validating a blank string will return `nil`.
534
+ If you need a value, use the `required` option:
535
+
536
+ ```ruby
537
+ schema = Schemacop::Schema3.new(:boolean, cast_str: true, required: true)
538
+ schema.validate!('false') # => false
539
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
540
+ schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
489
541
  ```
490
542
 
491
543
  ### Array
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.10
1
+ 3.0.11
@@ -33,10 +33,11 @@ module Schemacop
33
33
  if options.delete(:cast_str)
34
34
  format = NodeRegistry.name(klass)
35
35
  one_of_options = {
36
- required: options.delete(:required),
37
- name: options.delete(:name),
38
- as: options.delete(:as),
39
- description: options.delete(:description)
36
+ required: options.delete(:required),
37
+ treat_blank_as_nil: true,
38
+ name: options.delete(:name),
39
+ as: options.delete(:as),
40
+ description: options.delete(:description)
40
41
  }
41
42
  node = create(:one_of, **one_of_options) do
42
43
  self.node node
@@ -5,8 +5,30 @@ module Schemacop
5
5
  :oneOf
6
6
  end
7
7
 
8
+ def self.allowed_options
9
+ super + %i[treat_blank_as_nil]
10
+ end
11
+
12
+ def cast(value)
13
+ item = match(value)
14
+
15
+ unless item
16
+ if options[:treat_blank_as_nil] && value.blank? && !value.is_a?(FalseClass)
17
+ return nil
18
+ else
19
+ return value
20
+ end
21
+ end
22
+
23
+ return item.cast(value)
24
+ end
25
+
8
26
  def _validate(data, result:)
9
- super_data = super
27
+ if options[:treat_blank_as_nil] && data.blank? && !data.is_a?(FalseClass)
28
+ data = nil
29
+ end
30
+
31
+ super_data = super(data, result: result)
10
32
  return if super_data.nil?
11
33
 
12
34
  matches = matches(super_data)
data/schemacop.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 3.0.10 ruby lib
2
+ # stub: schemacop 3.0.11 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "3.0.10"
6
+ s.version = "3.0.11"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Sitrox".freeze]
11
- s.date = "2021-03-19"
11
+ s.date = "2021-03-26"
12
12
  s.files = [".gitignore".freeze, ".releaser_config".freeze, ".rubocop.yml".freeze, ".travis.yml".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "README_V2.md".freeze, "README_V3.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/base_schema.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/railtie.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/schema2.rb".freeze, "lib/schemacop/schema3.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/v2.rb".freeze, "lib/schemacop/v2/caster.rb".freeze, "lib/schemacop/v2/collector.rb".freeze, "lib/schemacop/v2/dupper.rb".freeze, "lib/schemacop/v2/field_node.rb".freeze, "lib/schemacop/v2/node.rb".freeze, "lib/schemacop/v2/node_resolver.rb".freeze, "lib/schemacop/v2/node_supporting_field.rb".freeze, "lib/schemacop/v2/node_supporting_type.rb".freeze, "lib/schemacop/v2/node_with_block.rb".freeze, "lib/schemacop/v2/validator/array_validator.rb".freeze, "lib/schemacop/v2/validator/boolean_validator.rb".freeze, "lib/schemacop/v2/validator/float_validator.rb".freeze, "lib/schemacop/v2/validator/hash_validator.rb".freeze, "lib/schemacop/v2/validator/integer_validator.rb".freeze, "lib/schemacop/v2/validator/nil_validator.rb".freeze, "lib/schemacop/v2/validator/number_validator.rb".freeze, "lib/schemacop/v2/validator/object_validator.rb".freeze, "lib/schemacop/v2/validator/string_validator.rb".freeze, "lib/schemacop/v2/validator/symbol_validator.rb".freeze, "lib/schemacop/v3.rb".freeze, "lib/schemacop/v3/all_of_node.rb".freeze, "lib/schemacop/v3/any_of_node.rb".freeze, "lib/schemacop/v3/array_node.rb".freeze, "lib/schemacop/v3/boolean_node.rb".freeze, "lib/schemacop/v3/combination_node.rb".freeze, "lib/schemacop/v3/context.rb".freeze, "lib/schemacop/v3/dsl_scope.rb".freeze, "lib/schemacop/v3/global_context.rb".freeze, "lib/schemacop/v3/hash_node.rb".freeze, "lib/schemacop/v3/integer_node.rb".freeze, "lib/schemacop/v3/is_not_node.rb".freeze, "lib/schemacop/v3/node.rb".freeze, "lib/schemacop/v3/node_registry.rb".freeze, "lib/schemacop/v3/number_node.rb".freeze, "lib/schemacop/v3/numeric_node.rb".freeze, "lib/schemacop/v3/object_node.rb".freeze, "lib/schemacop/v3/one_of_node.rb".freeze, "lib/schemacop/v3/reference_node.rb".freeze, "lib/schemacop/v3/result.rb".freeze, "lib/schemacop/v3/string_node.rb".freeze, "lib/schemacop/v3/symbol_node.rb".freeze, "schemacop.gemspec".freeze, "test/lib/test_helper.rb".freeze, "test/schemas/nested/group.rb".freeze, "test/schemas/user.rb".freeze, "test/unit/schemacop/v2/casting_test.rb".freeze, "test/unit/schemacop/v2/collector_test.rb".freeze, "test/unit/schemacop/v2/custom_check_test.rb".freeze, "test/unit/schemacop/v2/custom_if_test.rb".freeze, "test/unit/schemacop/v2/defaults_test.rb".freeze, "test/unit/schemacop/v2/empty_test.rb".freeze, "test/unit/schemacop/v2/nil_dis_allow_test.rb".freeze, "test/unit/schemacop/v2/node_resolver_test.rb".freeze, "test/unit/schemacop/v2/short_forms_test.rb".freeze, "test/unit/schemacop/v2/types_test.rb".freeze, "test/unit/schemacop/v2/validator_array_test.rb".freeze, "test/unit/schemacop/v2/validator_boolean_test.rb".freeze, "test/unit/schemacop/v2/validator_float_test.rb".freeze, "test/unit/schemacop/v2/validator_hash_test.rb".freeze, "test/unit/schemacop/v2/validator_integer_test.rb".freeze, "test/unit/schemacop/v2/validator_nil_test.rb".freeze, "test/unit/schemacop/v2/validator_number_test.rb".freeze, "test/unit/schemacop/v2/validator_object_test.rb".freeze, "test/unit/schemacop/v2/validator_string_test.rb".freeze, "test/unit/schemacop/v2/validator_symbol_test.rb".freeze, "test/unit/schemacop/v3/all_of_node_test.rb".freeze, "test/unit/schemacop/v3/any_of_node_test.rb".freeze, "test/unit/schemacop/v3/array_node_test.rb".freeze, "test/unit/schemacop/v3/boolean_node_test.rb".freeze, "test/unit/schemacop/v3/global_context_test.rb".freeze, "test/unit/schemacop/v3/hash_node_test.rb".freeze, "test/unit/schemacop/v3/integer_node_test.rb".freeze, "test/unit/schemacop/v3/is_not_node_test.rb".freeze, "test/unit/schemacop/v3/node_test.rb".freeze, "test/unit/schemacop/v3/number_node_test.rb".freeze, "test/unit/schemacop/v3/object_node_test.rb".freeze, "test/unit/schemacop/v3/one_of_node_test.rb".freeze, "test/unit/schemacop/v3/reference_node_test.rb".freeze, "test/unit/schemacop/v3/string_node_test.rb".freeze, "test/unit/schemacop/v3/symbol_node_test.rb".freeze]
13
13
  s.homepage = "https://github.com/sitrox/schemacop".freeze
14
14
  s.licenses = ["MIT".freeze]
@@ -152,6 +152,36 @@ module Schemacop
152
152
  assert_validation('1') do
153
153
  error '/', 'Matches 0 definitions but should match exactly 1.'
154
154
  end
155
+
156
+ # Nil can be validated, as it's not required
157
+ assert_validation(nil)
158
+
159
+ assert_validation('')
160
+
161
+ assert_cast('', nil)
162
+ assert_cast(nil, nil)
163
+ end
164
+
165
+ def test_cast_str_required
166
+ schema :boolean, cast_str: true, required: true
167
+
168
+ assert_cast('true', true)
169
+ assert_cast('false', false)
170
+
171
+ assert_cast(true, true)
172
+ assert_cast(false, false)
173
+
174
+ assert_validation('1') do
175
+ error '/', 'Matches 0 definitions but should match exactly 1.'
176
+ end
177
+
178
+ assert_validation(nil) do
179
+ error '/', 'Value must be given.'
180
+ end
181
+
182
+ assert_validation('') do
183
+ error '/', 'Value must be given.'
184
+ end
155
185
  end
156
186
  end
157
187
  end
@@ -965,6 +965,46 @@ module Schemacop
965
965
  assert_cast({ foo: 42, bar: 13 }, { bar: 42 }.with_indifferent_access)
966
966
  assert_cast({ bar: 13, foo: 42 }, { bar: 42 }.with_indifferent_access)
967
967
  end
968
+
969
+ def test_cast_str_required
970
+ schema :hash do
971
+ boo! :active, cast_str: true
972
+ int! :id, cast_str: true
973
+ end
974
+
975
+ assert_validation({ active: true, id: 1 })
976
+ assert_validation({ active: 'true', id: '1' })
977
+
978
+ assert_validation({}) do
979
+ error '/active', 'Value must be given.'
980
+ error '/id', 'Value must be given.'
981
+ end
982
+
983
+ assert_cast({ active: 'true', id: '1' }, { active: true, id: 1 }.with_indifferent_access)
984
+
985
+ assert_validation({ active: '', id: '' }) do
986
+ error '/active', 'Value must be given.'
987
+ error '/id', 'Value must be given.'
988
+ end
989
+ end
990
+
991
+ def test_cast_str_optional
992
+ schema :hash do
993
+ boo? :active, cast_str: true
994
+ int? :id, cast_str: true
995
+ end
996
+
997
+ assert_validation({ active: true, id: 1 })
998
+ assert_validation({ active: 'true', id: '1' })
999
+
1000
+ assert_cast({ active: 'true', id: '1' }, { active: true, id: 1 }.with_indifferent_access)
1001
+
1002
+ assert_validation({})
1003
+ assert_validation({ active: nil, id: nil })
1004
+
1005
+ assert_validation({ active: '', id: '' })
1006
+ assert_cast({ active: '', id: '' }, { active: nil, id: nil }.with_indifferent_access)
1007
+ end
968
1008
  end
969
1009
  end
970
1010
  end
@@ -339,6 +339,28 @@ module Schemacop
339
339
  assert_cast('1', 1)
340
340
  assert_cast(1, 1)
341
341
 
342
+ assert_cast(nil, nil)
343
+ assert_cast('', nil)
344
+
345
+ assert_validation('true') do
346
+ error '/', 'Matches 0 definitions but should match exactly 1.'
347
+ end
348
+ end
349
+
350
+ def test_cast_str_required
351
+ schema :integer, cast_str: true, required: true
352
+
353
+ assert_cast('1', 1)
354
+ assert_cast(1, 1)
355
+
356
+ assert_validation(nil) do
357
+ error '/', 'Value must be given.'
358
+ end
359
+
360
+ assert_validation('') do
361
+ error '/', 'Value must be given.'
362
+ end
363
+
342
364
  assert_validation('true') do
343
365
  error '/', 'Matches 0 definitions but should match exactly 1.'
344
366
  end
@@ -93,6 +93,10 @@ module Schemacop
93
93
  error '/', 'Value must be given.'
94
94
  end
95
95
 
96
+ assert_validation('') do
97
+ error '/', 'Value must be given.'
98
+ end
99
+
96
100
  assert_validation('5')
97
101
  assert_validation('5.3') do
98
102
  error '/', 'Matches 0 definitions but should match exactly 1.'
@@ -156,6 +160,12 @@ module Schemacop
156
160
  assert_validation([nil]) do
157
161
  error '/[0]', 'Value must be given.'
158
162
  end
163
+ assert_validation(['']) do
164
+ error '/[0]', 'Value must be given.'
165
+ end
166
+ assert_validation([]) do
167
+ error '/', 'Array has 0 items but must have exactly 1.'
168
+ end
159
169
  end
160
170
 
161
171
  def test_not_support_block
@@ -308,6 +308,9 @@ module Schemacop
308
308
  assert_cast('1', 1)
309
309
  assert_cast(1, 1)
310
310
 
311
+ assert_validation(nil)
312
+ assert_validation('')
313
+
311
314
  assert_cast('1.0', 1.0)
312
315
  assert_cast(1.0, 1.0)
313
316
 
@@ -182,6 +182,18 @@ module Schemacop
182
182
  schema :one_of
183
183
  end
184
184
  end
185
+
186
+ def test_treat_blank_as_nil
187
+ schema :one_of, treat_blank_as_nil: true do
188
+ boo
189
+ str format: :boolean
190
+ end
191
+
192
+ assert_validation(nil)
193
+ assert_validation('')
194
+ assert_validation('true')
195
+ assert_validation(true)
196
+ end
185
197
  end
186
198
  end
187
199
  end
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.10
4
+ version: 3.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-19 00:00:00.000000000 Z
11
+ date: 2021-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport