schemacop 3.0.17 → 3.0.19

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c486c27262569e4a3f3f3b9751aedfab72a15611c457c9748577e2d2625f43ec
4
- data.tar.gz: dc61cc778afc5403904fcc51cb74c81fadb0d5b3142ffb91f0b09b2eb82f4c69
3
+ metadata.gz: 8164d9dd2a81d5ec5ae76513ed811684a4d8cadeabd5b4b4781a8a73fa921461
4
+ data.tar.gz: 4bd7ec87972185ada98efa998077346ace90a52cbd8f447cb245515f0089b65e
5
5
  SHA512:
6
- metadata.gz: 2ad16e54c5d883e62b456718dbb8e086c6e0cad805b88996817876176451fe04b0ababa4410762e2d58adbefbdce34e69a8a0f0ff9499b683dbdfcb3b89185f9
7
- data.tar.gz: b08189742b46257f92dd7e54fd0f917cbb03b95092add3557a90b428f64fdc7ad7428ae96004ee5a645eefec83b70934b3a03f39152fa71d35dc4132bfa9694b
6
+ metadata.gz: 7a8528412066ec7cd2569750c75838dfcafc101ca3deb708bde0b866c9b161699ef1f44f26fae2a198a6dc36e7d66d038f72179e2e64b63a159dab5e9b7050a7
7
+ data.tar.gz: e9354474a36a62234aa28a6186bf48b6d1c292a3b7382ccd61a0d85469dd9b50d803aad9c3b81623e4a2ae7b735c3c0ec69fc148c3407c4af67db670d96b5a32
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change log
2
2
 
3
+ ## 3.0.19 (2022-09-19)
4
+
5
+ * Make `cast_str` of `BooleanNode` in `v3` case-insensitive
6
+
7
+ ## 3.0.18 (2022-07-27)
8
+
9
+ * Add option `ignore_obsolete_properties` to `HashNode` for Schemacop v3
10
+
3
11
  ## 3.0.17 (2022-06-08)
4
12
 
5
13
  * Add options `filter` and `reject` to array nodes which allow to filter arrays
data/README_V3.md CHANGED
@@ -235,7 +235,9 @@ transformed into various types.
235
235
 
236
236
  * `boolean`
237
237
  The string must be either `true`, `false`, `0` or `1`. This value will be
238
- casted to Ruby's `TrueClass` or `FalseClass`.
238
+ casted to Ruby's `TrueClass` or `FalseClass`. Please note that the strings
239
+ `true` and `false` are case-insensitive, i.e. `True`, `TRUE` etc. will also
240
+ work.
239
241
 
240
242
  * `binary`
241
243
  The string is expected to contain binary contents. No casting or additional
@@ -529,7 +531,7 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
529
531
  * `cast_str`
530
532
  When set to `true`, this node also accepts strings that can be casted to a
531
533
  boolean, namely the values `'true'`, `'false'`, `'1'` and `'0'`. Blank strings
532
- will be treated equally as `nil`.
534
+ will be treated equally as `nil`. This casting is case-insensitive.
533
535
 
534
536
  #### Examples
535
537
 
@@ -864,6 +866,13 @@ It consists of key-value-pairs that can be validated using arbitrary nodes.
864
866
  * `max_properties`
865
867
  Specifies the (inclusive) maximum number of properties a hash must contain.
866
868
 
869
+ * `ignore_obsolete_properties`
870
+ Similar to `additional_properties`. If this is set to `true`, all additional
871
+ properties are allowed (i.e. they pass the validation), but they are removed
872
+ from the result hash. This is useful e.g. to validate params coming from the
873
+ controller, as this only allows white-listed params and removes any params
874
+ which are not whitelisted (i.e. similar to strong params from Rails).
875
+
867
876
  #### Specifying properties
868
877
 
869
878
  Hash nodes support a block in which you can specify the required hash contents.
@@ -1071,6 +1080,22 @@ schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError:
1071
1080
  schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, got type "Symbol", expected "array".
1072
1081
  ```
1073
1082
 
1083
+ ##### Ignoring obsolete properties
1084
+
1085
+ By enabling `ignore_obsolete_properties`, you can filter out any unspecified params,
1086
+ while still passing validation:
1087
+
1088
+ ```ruby
1089
+ # This schema will accept any additional properties, but remove them from the result
1090
+ schema = Schemacop::Schema3.new :hash, ignore_obsolete_properties: true do
1091
+ int? :foo
1092
+ end
1093
+
1094
+ schema.validate!({}) # => {}
1095
+ schema.validate!({foo: :bar}) # => {"foo"=>:bar}
1096
+ schema.validate!({foo: :bar, baz: 42}) # => {"foo"=>:bar}
1097
+ ```
1098
+
1074
1099
  ##### Dependencies
1075
1100
 
1076
1101
  Using the DSL method `dep`, you can specifiy (non-nested) property dependencies:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.17
1
+ 3.0.19
@@ -14,7 +14,7 @@ module Schemacop
14
14
  attr_reader :properties
15
15
 
16
16
  def self.allowed_options
17
- super + ATTRIBUTES - %i[dependencies] + %i[additional_properties]
17
+ super + ATTRIBUTES - %i[dependencies] + %i[additional_properties ignore_obsolete_properties]
18
18
  end
19
19
 
20
20
  def self.dsl_methods
@@ -141,7 +141,7 @@ module Schemacop
141
141
  result.in_path(name) do
142
142
  property_patterns[match]._validate(additional_property, result: result)
143
143
  end
144
- else
144
+ elsif !options[:ignore_obsolete_properties]
145
145
  result.error "Obsolete property #{name.to_s.inspect}."
146
146
  end
147
147
  elsif options[:additional_properties].is_a?(Node)
@@ -235,10 +235,20 @@ module Schemacop
235
235
  fail Schemacop::Exceptions::InvalidSchemaError, 'Option "additional_properties" must be a boolean value'
236
236
  end
237
237
 
238
+ # Cannot set additional_properties and ignore_obsolete_properties option to true at the same time
239
+ if @options[:additional_properties].is_a?(TrueClass) && options[:ignore_obsolete_properties].is_a?(TrueClass)
240
+ fail Schemacop::Exceptions::InvalidSchemaError, 'Cannot set "additional_properties" and "ignore_obsolete_properties" to true at the same time'
241
+ end
242
+
238
243
  # Default the additional_properties option to false if it's not given
239
244
  if @options[:additional_properties].nil?
240
245
  @options[:additional_properties] = false
241
246
  end
247
+
248
+ # Default the ignore_obsolete_properties option to false if it's not given
249
+ if @options[:ignore_obsolete_properties].nil?
250
+ @options[:ignore_obsolete_properties] = false
251
+ end
242
252
  end
243
253
 
244
254
  def validate_self
data/lib/schemacop.rb CHANGED
@@ -56,8 +56,8 @@ module Schemacop
56
56
 
57
57
  register_string_formatter(
58
58
  :boolean,
59
- pattern: /^(true|false|0|1)$/,
60
- handler: ->(value) { %w[true 1].include?(value) }
59
+ pattern: /^(true|false|0|1)$/i,
60
+ handler: ->(value) { %w[true 1].include?(value&.downcase) }
61
61
  )
62
62
 
63
63
  register_string_formatter(
data/schemacop.gemspec CHANGED
@@ -1,49 +1,37 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 3.0.17 ruby lib
2
+ # stub: schemacop 3.0.19 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "3.0.17"
6
+ s.version = "3.0.19"
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 = "2022-06-08"
11
+ s.date = "2022-09-19"
12
12
  s.files = [".github/workflows/ruby.yml".freeze, ".gitignore".freeze, ".releaser_config".freeze, ".rubocop.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]
15
- s.rubygems_version = "3.0.3".freeze
15
+ s.rubygems_version = "3.2.15".freeze
16
16
  s.summary = "Schemacop validates ruby structures consisting of nested hashes and arrays against simple schema definitions.".freeze
17
17
  s.test_files = ["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]
18
18
 
19
19
  if s.respond_to? :specification_version then
20
20
  s.specification_version = 4
21
+ end
21
22
 
22
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
23
- s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.0"])
24
- s.add_runtime_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
25
- s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
26
- s.add_development_dependency(%q<rake>.freeze, [">= 0"])
27
- s.add_development_dependency(%q<minitest>.freeze, [">= 0"])
28
- s.add_development_dependency(%q<minitest-reporters>.freeze, [">= 0"])
29
- s.add_development_dependency(%q<colorize>.freeze, [">= 0"])
30
- s.add_development_dependency(%q<rubocop>.freeze, ["= 1.24.1"])
31
- s.add_development_dependency(%q<pry>.freeze, [">= 0"])
32
- s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
33
- s.add_development_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
34
- else
35
- s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
36
- s.add_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
37
- s.add_dependency(%q<bundler>.freeze, [">= 0"])
38
- s.add_dependency(%q<rake>.freeze, [">= 0"])
39
- s.add_dependency(%q<minitest>.freeze, [">= 0"])
40
- s.add_dependency(%q<minitest-reporters>.freeze, [">= 0"])
41
- s.add_dependency(%q<colorize>.freeze, [">= 0"])
42
- s.add_dependency(%q<rubocop>.freeze, ["= 1.24.1"])
43
- s.add_dependency(%q<pry>.freeze, [">= 0"])
44
- s.add_dependency(%q<byebug>.freeze, [">= 0"])
45
- s.add_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
46
- end
23
+ if s.respond_to? :add_runtime_dependency then
24
+ s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.0"])
25
+ s.add_runtime_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
26
+ s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
27
+ s.add_development_dependency(%q<rake>.freeze, [">= 0"])
28
+ s.add_development_dependency(%q<minitest>.freeze, [">= 0"])
29
+ s.add_development_dependency(%q<minitest-reporters>.freeze, [">= 0"])
30
+ s.add_development_dependency(%q<colorize>.freeze, [">= 0"])
31
+ s.add_development_dependency(%q<rubocop>.freeze, ["= 1.24.1"])
32
+ s.add_development_dependency(%q<pry>.freeze, [">= 0"])
33
+ s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
34
+ s.add_development_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
47
35
  else
48
36
  s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
49
37
  s.add_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
@@ -184,6 +184,9 @@ module Schemacop
184
184
  assert_cast(true, true)
185
185
  assert_cast(false, false)
186
186
 
187
+ assert_cast('True', true)
188
+ assert_cast('False', false)
189
+
187
190
  assert_validation('5') do
188
191
  error '/', 'Matches 0 definitions but should match exactly 1.'
189
192
  end
@@ -206,6 +209,13 @@ module Schemacop
206
209
  assert_cast(true, true)
207
210
  assert_cast(false, false)
208
211
 
212
+ # Test case-insentiveness
213
+ assert_cast('True', true)
214
+ assert_cast('False', false)
215
+
216
+ assert_cast('TRUE', true)
217
+ assert_cast('FALSE', false)
218
+
209
219
  assert_validation('4') do
210
220
  error '/', 'Matches 0 definitions but should match exactly 1.'
211
221
  end
@@ -1005,6 +1005,133 @@ module Schemacop
1005
1005
  assert_validation({ active: '', id: '' })
1006
1006
  assert_cast({ active: '', id: '' }, { active: nil, id: nil }.with_indifferent_access)
1007
1007
  end
1008
+
1009
+ def test_ignore_obsolete_properties_true
1010
+ schema :hash, ignore_obsolete_properties: true do
1011
+ int? :foo
1012
+ str? :bar
1013
+ end
1014
+
1015
+ # Some standard validations first
1016
+ assert_validation({})
1017
+ assert_validation({ foo: 1 })
1018
+ assert_validation({ bar: 'baz' })
1019
+ assert_validation({ foo: 1, bar: 'baz' })
1020
+
1021
+ assert_cast({}, {}.with_indifferent_access)
1022
+ assert_cast({ foo: 1 }, { foo: 1 }.with_indifferent_access)
1023
+ assert_cast({ bar: 'baz' }, { bar: 'baz' }.with_indifferent_access)
1024
+ assert_cast({ foo: 1, bar: 'baz' }, { foo: 1, bar: 'baz' }.with_indifferent_access)
1025
+
1026
+ # Should allow obsolete properties and remove them from the result
1027
+ assert_validation({ obsolete_key: 42 })
1028
+ assert_validation({ foo: 1, obsolete_key: 42 })
1029
+ assert_validation({ bar: 'baz', obsolete_key: 42 })
1030
+ assert_validation({ foo: 1, bar: 'baz', obsolete_key: 42 })
1031
+
1032
+ assert_cast({ obsolete_key: 42 }, {}.with_indifferent_access)
1033
+ assert_cast({ foo: 1, obsolete_key: 42 }, { foo: 1 }.with_indifferent_access)
1034
+ assert_cast({ bar: 'baz', obsolete_key: 42 }, { bar: 'baz' }.with_indifferent_access)
1035
+ assert_cast({ foo: 1, bar: 'baz', obsolete_key: 42 }, { foo: 1, bar: 'baz' }.with_indifferent_access)
1036
+ end
1037
+
1038
+ def test_ignore_obsolete_properties_false
1039
+ schema :hash, ignore_obsolete_properties: false do
1040
+ int? :foo
1041
+ str? :bar
1042
+ end
1043
+
1044
+ # Some standard validations first
1045
+ assert_validation({})
1046
+ assert_validation({ foo: 1 })
1047
+ assert_validation({ bar: 'baz' })
1048
+ assert_validation({ foo: 1, bar: 'baz' })
1049
+
1050
+ assert_cast({}, {}.with_indifferent_access)
1051
+ assert_cast({ foo: 1 }, { foo: 1 }.with_indifferent_access)
1052
+ assert_cast({ bar: 'baz' }, { bar: 'baz' }.with_indifferent_access)
1053
+ assert_cast({ foo: 1, bar: 'baz' }, { foo: 1, bar: 'baz' }.with_indifferent_access)
1054
+
1055
+ # Should not allow obsolete properties as the option is set to false
1056
+ assert_validation({ obsolete_key: 42 }) do
1057
+ error '/', 'Obsolete property "obsolete_key".'
1058
+ end
1059
+ assert_validation({ foo: 1, obsolete_key: 42 }) do
1060
+ error '/', 'Obsolete property "obsolete_key".'
1061
+ end
1062
+ assert_validation({ bar: 'baz', obsolete_key: 42 }) do
1063
+ error '/', 'Obsolete property "obsolete_key".'
1064
+ end
1065
+ assert_validation({ foo: 1, bar: 'baz', obsolete_key: 42 }) do
1066
+ error '/', 'Obsolete property "obsolete_key".'
1067
+ end
1068
+ end
1069
+
1070
+ def test_ignore_obsolete_properties_true_and_additional_properties_true
1071
+ # Cannot set both options to true at the same time
1072
+ assert_raises_with_message Exceptions::InvalidSchemaError,
1073
+ 'Cannot set "additional_properties" and "ignore_obsolete_properties" to true at the same time' do
1074
+ schema :hash, ignore_obsolete_properties: true, additional_properties: true
1075
+ end
1076
+ end
1077
+
1078
+ def test_ignore_obsolete_properties_false_and_additional_properties_true
1079
+ # This should allow any additional properties and keep them in the hash
1080
+ schema :hash, ignore_obsolete_properties: false, additional_properties: true do
1081
+ int? :foo
1082
+ str? :bar
1083
+ end
1084
+
1085
+ assert_validation({ obsolete_key: 42 })
1086
+ assert_validation({ foo: 1, obsolete_key: 42 })
1087
+ assert_validation({ bar: 'baz', obsolete_key: 42 })
1088
+ assert_validation({ foo: 1, bar: 'baz', obsolete_key: 42 })
1089
+
1090
+ assert_cast({ obsolete_key: 42 }, { obsolete_key: 42 }.with_indifferent_access)
1091
+ assert_cast({ foo: 1, obsolete_key: 42 }, { foo: 1, obsolete_key: 42 }.with_indifferent_access)
1092
+ assert_cast({ bar: 'baz', obsolete_key: 42 }, { bar: 'baz', obsolete_key: 42 }.with_indifferent_access)
1093
+ assert_cast({ foo: 1, bar: 'baz', obsolete_key: 42 }, { foo: 1, bar: 'baz', obsolete_key: 42 }.with_indifferent_access)
1094
+ end
1095
+
1096
+ def test_ignore_obsolete_properties_true_and_additional_properties_false
1097
+ # This should allow any additional properties and keep them in the hash
1098
+ schema :hash, ignore_obsolete_properties: true, additional_properties: false do
1099
+ int? :foo
1100
+ str? :bar
1101
+ end
1102
+
1103
+ # Should allow obsolete properties and remove them from the result
1104
+ assert_validation({ obsolete_key: 42 })
1105
+ assert_validation({ foo: 1, obsolete_key: 42 })
1106
+ assert_validation({ bar: 'baz', obsolete_key: 42 })
1107
+ assert_validation({ foo: 1, bar: 'baz', obsolete_key: 42 })
1108
+
1109
+ assert_cast({ obsolete_key: 42 }, {}.with_indifferent_access)
1110
+ assert_cast({ foo: 1, obsolete_key: 42 }, { foo: 1 }.with_indifferent_access)
1111
+ assert_cast({ bar: 'baz', obsolete_key: 42 }, { bar: 'baz' }.with_indifferent_access)
1112
+ assert_cast({ foo: 1, bar: 'baz', obsolete_key: 42 }, { foo: 1, bar: 'baz' }.with_indifferent_access)
1113
+ end
1114
+
1115
+ def test_ignore_obsolete_properties_false_and_additional_properties_false
1116
+ # This should not allow any additional properties
1117
+ schema :hash, ignore_obsolete_properties: false, additional_properties: false do
1118
+ int? :foo
1119
+ str? :bar
1120
+ end
1121
+
1122
+ assert_validation({ obsolete_key: 42 }) do
1123
+ error '/', 'Obsolete property "obsolete_key".'
1124
+ end
1125
+ assert_validation({ foo: 1, obsolete_key: 42 }) do
1126
+ error '/', 'Obsolete property "obsolete_key".'
1127
+ end
1128
+ assert_validation({ bar: 'baz', obsolete_key: 42 }) do
1129
+ error '/', 'Obsolete property "obsolete_key".'
1130
+ end
1131
+ assert_validation({ foo: 1, bar: 'baz', obsolete_key: 42 }) do
1132
+ error '/', 'Obsolete property "obsolete_key".'
1133
+ end
1134
+ end
1008
1135
  end
1009
1136
  end
1010
1137
  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.17
4
+ version: 3.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-08 00:00:00.000000000 Z
11
+ date: 2022-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport