schemacop 3.0.28 → 3.0.29

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: ff2b18b4f3426f6a06e4a9183c3b20375c5d55496929bdf30a569c19ad1cc28c
4
- data.tar.gz: edb96299b93d5e49b6d5a8125fa2c95a73d6158c2cae4d4428c3fe9066b41244
3
+ metadata.gz: 0e7d7e77045f4130fe9e9863f3b6fddcbf5e847e4d461d2d40b13c472fecb40c
4
+ data.tar.gz: 55d594ce2ad63d13908e8d6cb97c4ae258b877c5b89d1ef52b64b345e4a83bf8
5
5
  SHA512:
6
- metadata.gz: 49b290cbf2a7fdfc4b938fbdad9d4bd54469867fa46be22dc1a2158c32fe68266d1cce136d824a263689b9fdd9797ddfd601c7fa9fafbd37bb9adf3634f126ba
7
- data.tar.gz: 73241d7daed75ad77a230904821749dcb0e954f8c3c858bc207d9487dd4b7cffbe806ea7a64dffc28010e6dc27281133d5191129781ad2c462dbe2f395b76278
6
+ metadata.gz: 5ea7238bd691e8ab2af17ae36e24fd10e3ad5033c2a70fd904ec253e6b57b4ab2b9a819781a5403a2e1d93bc138e2de82d8c42fe0ed730c10a2597fe2df85a3f
7
+ data.tar.gz: 84cff469cae0b9eefa9ce2a6675d301cc381082e694352818f5852a74296338957b172cf39e9420b49924696c6eab16f600388c89c7b816b187dfb22c3d5843d
@@ -12,7 +12,7 @@ jobs:
12
12
  strategy:
13
13
  fail-fast: false
14
14
  matrix:
15
- ruby-version: ['2.6.2', '2.7.1', '3.0.1', '3.1.0', '3.2.0']
15
+ ruby-version: ['2.6.2', '2.7.1', '3.0.1', '3.1.0', '3.2.0', '3.3.0']
16
16
 
17
17
  steps:
18
18
  - uses: actions/checkout@v2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change log
2
2
 
3
+ ## 3.0.29 (2024-06-06)
4
+
5
+ * Add option `parse_json` to `hash` and `array` V3 nodes
6
+
7
+ Internal reference: `#125211`.
8
+
3
9
  ## 3.0.28 (2023-12-20)
4
10
 
5
11
  * Fix `mailbox` format allowing incorrectly formatted strings
data/Gemfile CHANGED
@@ -2,3 +2,13 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify gem dependencies in the .gemspec file
4
4
  gemspec
5
+
6
+ # Development dependencies
7
+ gem 'bundler'
8
+ gem 'colorize'
9
+ gem 'minitest'
10
+ gem 'minitest-reporters'
11
+ gem 'pry'
12
+ gem 'rake'
13
+ gem 'rubocop', '1.24.1'
14
+ gem 'simplecov', '0.21.2'
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright © 2016 - 2023 Sitrox
3
+ Copyright © 2016 - 2024 Sitrox
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -16,6 +16,8 @@ Schemacop is tested with the following ruby versions:
16
16
  * 2.7.1
17
17
  * 3.0.1
18
18
  * 3.1.0
19
+ * 3.2.0
20
+ * 3.3.0
19
21
 
20
22
  Other ruby versions might work but are not covered by our automated tests.
21
23
 
@@ -119,4 +121,4 @@ To run tests:
119
121
 
120
122
  ## Copyright
121
123
 
122
- Copyright © 2016 - 2023 Sitrox. See `LICENSE` for further details.
124
+ Copyright © 2016 - 2024 Sitrox. See `LICENSE` for further details.
data/README_V2.md CHANGED
@@ -144,11 +144,11 @@ s = Schema.new do
144
144
  req :foo, default: 42
145
145
  end
146
146
 
147
- collector = s.validate({})
147
+ collector = s.validate({})
148
148
  collector.valid? # true
149
149
  collector.data # => { foo: 42 }
150
150
 
151
- collector = s.validate({ foo: 'invalid' })
151
+ collector = s.validate({ foo: 'invalid' })
152
152
  collector.valid? # false
153
153
  collector.data # => nil
154
154
  collector.exceptions # => Validation error
@@ -772,4 +772,4 @@ To run tests:
772
772
 
773
773
  ## Copyright
774
774
 
775
- Copyright (c) 2020 Sitrox. See `LICENSE` for further details.
775
+ Copyright (c) 2024 Sitrox. See `LICENSE` for further details.
data/README_V3.md CHANGED
@@ -632,6 +632,14 @@ It consists of one or multiple values, which can be validated using arbitrary no
632
632
 
633
633
  This is the inverse of option `filter`.
634
634
 
635
+ * `parse_json`
636
+ Specifies whether JSON is accepted instead of an array. If this is set to
637
+ `true` and the given value is a string, Schemacop will attempt to parse the
638
+ string as JSON. If the JSON yields a valid array, it will cast the JSON to a
639
+ array and validate it using the given schema.
640
+
641
+ Defaults to `false`.
642
+
635
643
  #### Contains
636
644
 
637
645
  The `array` node features the *contains* node, which you can use with the DSL
@@ -855,6 +863,21 @@ end
855
863
  schema.validate!(['foo', 42, 0]) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, got type "String", expected "integer".
856
864
  ```
857
865
 
866
+ ##### Parsing JSON
867
+
868
+ By enabling `parse_json`, the given value will be parsed as JSON if it is a
869
+ string instead of an array:
870
+
871
+ ```ruby
872
+ # This schema will accept any additional properties, but remove them from the result
873
+ schema = Schemacop::Schema3.new :array, parse_json: true do
874
+ list :integer
875
+ end
876
+
877
+ schema.validate!([1, 2, 3]) # => [1, 2, 3]
878
+ schema.validate!('[1, 2, 3]') # => [1, 2, 3]
879
+ ```
880
+
858
881
  ### Hash
859
882
 
860
883
  Type: `:hash`\
@@ -892,6 +915,14 @@ It consists of key-value-pairs that can be validated using arbitrary nodes.
892
915
  If it is set to an enumerable (e.g. `Set` or `Array`), it functions as a
893
916
  white-list and only the given additional properties are allowed.
894
917
 
918
+ * `parse_json`
919
+ Specifies whether JSON is accepted instead of a hash. If this is set to
920
+ `true` and the given value is a string, Schemacop will attempt to parse the
921
+ string as JSON. If the JSON yields a valid hash, it will cast the JSON to a
922
+ hash and validate it using the given schema.
923
+
924
+ Defaults to `false`.
925
+
895
926
  #### Specifying properties
896
927
 
897
928
  Hash nodes support a block in which you can specify the required hash contents.
@@ -1115,6 +1146,27 @@ schema.validate!({foo: :bar}) # => {"foo"=>:bar}
1115
1146
  schema.validate!({foo: :bar, baz: 42}) # => {"foo"=>:bar}
1116
1147
  ```
1117
1148
 
1149
+ ##### Parsing JSON
1150
+
1151
+ By enabling `parse_json`, the given value will be parsed as JSON if it is a
1152
+ string instead of a hash:
1153
+
1154
+ ```ruby
1155
+ # This schema will accept any additional properties, but remove them from the result
1156
+ schema = Schemacop::Schema3.new :hash, parse_json: true do
1157
+ int! :id
1158
+ str! :name
1159
+ end
1160
+
1161
+ schema.validate!({
1162
+ id: 42,
1163
+ name: 'Jane Doe'
1164
+ }) # => { id: 42, name: 'Jane Doe' }
1165
+ schema.validate!('{ "id": 42, name: "Jane Doe" }') # => { "id" => 42, "name" => 'Jane Doe' }
1166
+ ```
1167
+
1168
+ Note that the parsed JSON will always result in string hash keys, not symbols.
1169
+
1118
1170
  ##### Dependencies
1119
1171
 
1120
1172
  Using the DSL method `dep`, you can specifiy (non-nested) property dependencies:
data/Rakefile CHANGED
@@ -19,15 +19,6 @@ task :gemspec do
19
19
  # behavior of that as in version 5 of ActiveSupport.
20
20
  spec.add_dependency 'activesupport', '>= 4.0'
21
21
  spec.add_dependency 'ruby2_keywords', '>= 0.0.4'
22
- spec.add_development_dependency 'bundler'
23
- spec.add_development_dependency 'rake'
24
- spec.add_development_dependency 'minitest'
25
- spec.add_development_dependency 'minitest-reporters'
26
- spec.add_development_dependency 'colorize'
27
- spec.add_development_dependency 'rubocop', '1.24.1'
28
- spec.add_development_dependency 'pry'
29
- spec.add_development_dependency 'byebug'
30
- spec.add_development_dependency 'simplecov', '0.21.2'
31
22
  end
32
23
 
33
24
  File.write('schemacop.gemspec', gemspec.to_ruby.strip)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.28
1
+ 3.0.29
@@ -10,7 +10,7 @@ module Schemacop
10
10
  supports_children
11
11
 
12
12
  def self.allowed_options
13
- super + ATTRIBUTES + %i[additional_items reject filter]
13
+ super + ATTRIBUTES + %i[additional_items reject filter parse_json]
14
14
  end
15
15
 
16
16
  def self.dsl_methods
@@ -73,13 +73,21 @@ module Schemacop
73
73
  end
74
74
 
75
75
  def allowed_types
76
- { Array => :array }
76
+ if options[:parse_json]
77
+ { Array => :array, String => :array }
78
+ else
79
+ { Array => :array }
80
+ end
77
81
  end
78
82
 
79
83
  def _validate(data, result:)
80
84
  super_data = super
81
85
  return if super_data.nil?
82
86
 
87
+ # Handle JSON
88
+ super_data = parse_if_json(super_data, result: result, allowed_types: { Array => :array })
89
+ return if super_data.nil?
90
+
83
91
  # Preprocess
84
92
  super_data = preprocess_array(super_data)
85
93
 
@@ -143,6 +151,8 @@ module Schemacop
143
151
  def cast(value)
144
152
  return default unless value
145
153
 
154
+ value = parse_if_json(value, allowed_types: { Array => :array })
155
+
146
156
  result = []
147
157
 
148
158
  value.each_with_index do |value_item, index|
@@ -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 ignore_obsolete_properties]
17
+ super + ATTRIBUTES - %i[dependencies] + %i[additional_properties ignore_obsolete_properties parse_json]
18
18
  end
19
19
 
20
20
  def self.dsl_methods
@@ -78,13 +78,21 @@ module Schemacop
78
78
  end
79
79
 
80
80
  def allowed_types
81
- { Hash => :object }
81
+ if options[:parse_json]
82
+ { Hash => :object, String => :string }
83
+ else
84
+ { Hash => :object }
85
+ end
82
86
  end
83
87
 
84
88
  def _validate(data, result: Result.new)
85
89
  super_data = super
86
90
  return if super_data.nil?
87
91
 
92
+ # Handle JSON
93
+ super_data = parse_if_json(super_data, result: result, allowed_types: { Hash => :object })
94
+ return if super_data.nil?
95
+
88
96
  original_data_hash = super_data.dup
89
97
  data_hash = super_data.with_indifferent_access
90
98
 
@@ -169,6 +177,7 @@ module Schemacop
169
177
  end
170
178
 
171
179
  def cast(data)
180
+ data = parse_if_json(data, allowed_types: { Hash => :object })
172
181
  result = {}.with_indifferent_access
173
182
  data ||= default
174
183
  return nil if data.nil?
@@ -192,6 +192,21 @@ module Schemacop
192
192
  return json.as_json
193
193
  end
194
194
 
195
+ def parse_if_json(data, allowed_types:, result: nil)
196
+ if data.is_a?(String)
197
+ data = JSON.parse(data)
198
+
199
+ if result && !validate_type(data, result, allowed_types: allowed_types)
200
+ return nil
201
+ end
202
+ end
203
+
204
+ return data
205
+ rescue JSON::ParserError => e
206
+ result&.error "JSON parse error: #{e.message.inspect}."
207
+ return nil
208
+ end
209
+
195
210
  def type_assertion_method
196
211
  :is_a?
197
212
  end
@@ -213,11 +228,7 @@ module Schemacop
213
228
  end
214
229
 
215
230
  # Validate type #
216
- if allowed_types.any? && allowed_types.keys.none? { |c| data.send(type_assertion_method, c) }
217
- collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
218
- result.error "Invalid type, got type \"#{data.class}\", expected #{collection}."
219
- return nil
220
- end
231
+ return nil unless validate_type(data, result)
221
232
 
222
233
  # Validate enums #
223
234
  if @enum && !@enum.include?(data)
@@ -227,6 +238,16 @@ module Schemacop
227
238
  return data
228
239
  end
229
240
 
241
+ def validate_type(data, result, allowed_types: self.allowed_types)
242
+ if allowed_types.any? && allowed_types.keys.none? { |c| data.send(type_assertion_method, c) }
243
+ collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
244
+ result.error "Invalid type, got type \"#{data.class}\", expected #{collection}."
245
+ return false
246
+ end
247
+
248
+ return true
249
+ end
250
+
230
251
  def validate_self; end
231
252
  end
232
253
  end
data/schemacop.gemspec CHANGED
@@ -1,32 +1,33 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 3.0.28 ruby lib
2
+ # stub: schemacop 3.0.29 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "3.0.28".freeze
6
+ s.version = "3.0.29"
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 = "2023-12-20"
11
+ s.date = "2024-06-06"
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.4.22".freeze
15
+ s.rubygems_version = "3.0.3".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
- s.specification_version = 4
19
+ if s.respond_to? :specification_version then
20
+ s.specification_version = 4
20
21
 
21
- s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.0".freeze])
22
- s.add_runtime_dependency(%q<ruby2_keywords>.freeze, [">= 0.0.4".freeze])
23
- s.add_development_dependency(%q<bundler>.freeze, [">= 0".freeze])
24
- s.add_development_dependency(%q<rake>.freeze, [">= 0".freeze])
25
- s.add_development_dependency(%q<minitest>.freeze, [">= 0".freeze])
26
- s.add_development_dependency(%q<minitest-reporters>.freeze, [">= 0".freeze])
27
- s.add_development_dependency(%q<colorize>.freeze, [">= 0".freeze])
28
- s.add_development_dependency(%q<rubocop>.freeze, ["= 1.24.1".freeze])
29
- s.add_development_dependency(%q<pry>.freeze, [">= 0".freeze])
30
- s.add_development_dependency(%q<byebug>.freeze, [">= 0".freeze])
31
- s.add_development_dependency(%q<simplecov>.freeze, ["= 0.21.2".freeze])
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
+ else
26
+ s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
27
+ s.add_dependency(%q<ruby2_keywords>.freeze, [">= 0.0.4"])
28
+ end
29
+ else
30
+ s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
31
+ s.add_dependency(%q<ruby2_keywords>.freeze, [">= 0.0.4"])
32
+ end
32
33
  end
@@ -28,7 +28,6 @@ require 'minitest/reporters'
28
28
  require 'schemacop'
29
29
  require 'pry'
30
30
  require 'colorize'
31
- require 'byebug'
32
31
 
33
32
  Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]
34
33
 
@@ -131,7 +130,7 @@ class V3Test < SchemacopTest
131
130
 
132
131
  Expected
133
132
  --------
134
- #{expected_error.blue}
133
+ #{expected_error.to_s.blue}
135
134
 
136
135
  #{'Actual'.red}
137
136
  #{'------'.red}
@@ -930,6 +930,31 @@ module Schemacop
930
930
  end
931
931
  end
932
932
  end
933
+
934
+ def test_parse_json
935
+ schema :array, parse_json: true do
936
+ list :integer
937
+ end
938
+ assert_validation([1, 2, 3])
939
+ assert_validation('[1,2,3]')
940
+ assert_cast('[1,2,3]', [1, 2, 3])
941
+
942
+ assert_validation('[1,2,"3"]') do
943
+ error '/[2]', 'Invalid type, got type "String", expected "integer".'
944
+ end
945
+
946
+ assert_validation('{ "id": 42 }') do
947
+ error '/', 'Invalid type, got type "Hash", expected "array".'
948
+ end
949
+
950
+ assert_validation('{42]') do
951
+ error '/', /JSON parse error: "(\d+: )?unexpected token at '{42]'"\./
952
+ end
953
+
954
+ assert_validation('"foo"') do
955
+ error '/', 'Invalid type, got type "String", expected "array".'
956
+ end
957
+ end
933
958
  end
934
959
  end
935
960
  end
@@ -14,6 +14,41 @@ module Schemacop
14
14
  assert_json(type: :object, additionalProperties: false)
15
15
  end
16
16
 
17
+ def test_parse_json
18
+ schema :hash, parse_json: true do
19
+ int! :id
20
+ str! :name
21
+ end
22
+ assert_validation({ id: 42, name: 'Jane Doe' })
23
+ assert_validation('{"id":42,"name":"Jane Doe"}')
24
+ assert_cast('{"id":42,"name":"Jane Doe"}', { id: 42, name: 'Jane Doe' }.stringify_keys)
25
+
26
+ assert_validation('{"id":42,"name":42}') do
27
+ error '/name', 'Invalid type, got type "Integer", expected "string".'
28
+ end
29
+
30
+ assert_validation('[42]') do
31
+ error '/', 'Invalid type, got type "Array", expected "object".'
32
+ end
33
+
34
+ assert_validation('{42]') do
35
+ error '/', /JSON parse error: "(\d+: )?unexpected token at '{42]'"\./
36
+ end
37
+
38
+ assert_validation('"foo"') do
39
+ error '/', 'Invalid type, got type "String", expected "object".'
40
+ end
41
+
42
+ schema :hash do
43
+ int! :id
44
+ end
45
+
46
+ assert_validation({ id: 42 })
47
+ assert_validation('{"id":42}') do
48
+ error '/', 'Invalid type, got type "String", expected "object".'
49
+ end
50
+ end
51
+
17
52
  def test_additional_properties_false_by_default
18
53
  schema
19
54
  assert_validation({})
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.28
4
+ version: 3.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-20 00:00:00.000000000 Z
11
+ date: 2024-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,134 +38,8 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.0.4
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: minitest
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: minitest-reporters
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: colorize
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: rubocop
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - '='
116
- - !ruby/object:Gem::Version
117
- version: 1.24.1
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - '='
123
- - !ruby/object:Gem::Version
124
- version: 1.24.1
125
- - !ruby/object:Gem::Dependency
126
- name: pry
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: byebug
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: simplecov
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - '='
158
- - !ruby/object:Gem::Version
159
- version: 0.21.2
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - '='
165
- - !ruby/object:Gem::Version
166
- version: 0.21.2
167
- description:
168
- email:
41
+ description:
42
+ email:
169
43
  executables: []
170
44
  extensions: []
171
45
  extra_rdoc_files: []
@@ -277,7 +151,7 @@ homepage: https://github.com/sitrox/schemacop
277
151
  licenses:
278
152
  - MIT
279
153
  metadata: {}
280
- post_install_message:
154
+ post_install_message:
281
155
  rdoc_options: []
282
156
  require_paths:
283
157
  - lib
@@ -292,8 +166,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
292
166
  - !ruby/object:Gem::Version
293
167
  version: '0'
294
168
  requirements: []
295
- rubygems_version: 3.4.22
296
- signing_key:
169
+ rubygems_version: 3.5.4
170
+ signing_key:
297
171
  specification_version: 4
298
172
  summary: Schemacop validates ruby structures consisting of nested hashes and arrays
299
173
  against simple schema definitions.