schemacop 2.1.0 → 2.2.0

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
  SHA1:
3
- metadata.gz: bb096c541a2c7681a8e46331b266f0e6d93e8946
4
- data.tar.gz: 2aead692c31d0eea58cc0bcc5f239cb03edcf322
3
+ metadata.gz: 553c564f36cf2b16bd1bc72f48f75abcaef6d66e
4
+ data.tar.gz: 9546f609f7e8e58db4012efd82ca4dee1ae21788
5
5
  SHA512:
6
- metadata.gz: 156d68d30297a571819a9bc9c4e2ca2fe7e10bfcbe01c79d13393de506a4b27e4e6d1faf07021cda33a05ebcf7b8cefb046efcbf325e3fd248ad272271d5ce6a
7
- data.tar.gz: 7057102772faed1465753810c013b3a8ea29f0c78d03128af0f6920dce56492de7835571716ba083a4bb48c9edece13ef0f3e364b25575753937855c2209e6b1
6
+ metadata.gz: c033fe31d11708a6f499869d9736b4b42081c02860270e48776ac86e9181f0522668dd3fa18ab80f31f2621453a3ac32493fab6e2da1502d61d028a58a621f25
7
+ data.tar.gz: 219c873fbeac36c803011553d34a88d073da8c30b61119cf0647f8fc3347cc736f8ad4306411a23173a79043592c4a697b3f984134cc91a0ac5f7c02b37817f1
data/CHANGELOG.md CHANGED
@@ -10,6 +10,19 @@
10
10
  ### Changes
11
11
  -->
12
12
 
13
+ ## 2.2.0 (2017-05-17)
14
+
15
+ ### Changes
16
+
17
+ * Handle `ActiveSupport::HashWithIndifferentAccess` objects gracefully when
18
+ performing the validation. This allows the user to specify the schema using
19
+ a mixture of symbols and strings, but during the validation of a
20
+ `HashWithIndifferentAccess` it transparently converts the keys, both in the
21
+ schema and in the hash, to symbols.
22
+
23
+ In the event that a key is defined both in the string and symbol version,
24
+ Schemacop expects a Ruby hash and will throw a ValidationError otherwise.
25
+
13
26
  ## 2.1.0 (2017-05-16)
14
27
 
15
28
  ### New features
data/README.md CHANGED
@@ -226,8 +226,8 @@ option `min` is supported by the `:string` validator (covered later).
226
226
 
227
227
  ### Field Line
228
228
 
229
- Inside a Type Line of type `:hash` or `Hash`, you may specify an arbitrary
230
- number of field lines (one for each key-value pair you want to be in the hash).
229
+ Inside a Type Line of type `:hash`, you may specify an arbitrary number of field
230
+ lines (one for each key-value pair you want to be in the hash).
231
231
 
232
232
  Field Lines start with one of the following six identifiers: `req`, `req?`,
233
233
  `req!`, `opt`, `opt?` or `opt!`:
@@ -273,6 +273,36 @@ end
273
273
  You might find the notation cumbersome, and you'd be right to say so. Luckily
274
274
  there are plenty of short forms available which we will see below.
275
275
 
276
+ #### Handling hashes with indifferent access
277
+
278
+ Schemacop has special handling for objects of the class
279
+ `ActiveSupport::HashWithIndifferentAccess`: You may specify the keys as symbols
280
+ or strings, and Schemacop will handle the conversion necessary for proper
281
+ validation internally. Note that if you define the same key as string and
282
+ symbol, it will throw a `ValidationError` [exception](#exceptions) when asked to
283
+ validate a hash with indifferent access.
284
+
285
+ Thus, the following two schema definitions are equivalent when validating a hash
286
+ with indifferent access:
287
+
288
+ ```ruby
289
+ Schema.new do
290
+ type :hash do
291
+ req :name do
292
+ type :string
293
+ end
294
+ end
295
+ end
296
+
297
+ Schema.new do
298
+ type :hash do
299
+ req 'name' do
300
+ type :string
301
+ end
302
+ end
303
+ end
304
+ ```
305
+
276
306
  ## Types
277
307
 
278
308
  Types are defined via their validators, which is a class under `validator/`.
@@ -317,7 +347,7 @@ The following types are supported by Schemacop by default:
317
347
  - TODO no lookahead for different arrays, see
318
348
  validator_array_test#test_multiple_arrays
319
349
 
320
- * `:hash` accepts a Ruby Hash.
350
+ * `:hash` accepts a Ruby Hash or an `ActiveSupport::HashWithIndifferentAccess`.
321
351
 
322
352
  - accepts a block with an arbitrary number of Field Lines.
323
353
 
data/Rakefile CHANGED
@@ -18,11 +18,15 @@ task :gemspec do
18
18
  spec.add_development_dependency 'rake'
19
19
  spec.add_development_dependency 'ci_reporter', '~> 2.0'
20
20
  spec.add_development_dependency 'ci_reporter_minitest'
21
- spec.add_development_dependency 'activesupport'
22
21
  spec.add_development_dependency 'haml'
23
22
  spec.add_development_dependency 'yard'
24
23
  spec.add_development_dependency 'rubocop', '0.35.1'
25
24
  spec.add_development_dependency 'redcarpet'
25
+
26
+ # This lower bound for ActiveSupport is not necessarily true. Schemacop
27
+ # needs access to ActiveSupport::HashWithIndifferentAccess and expects
28
+ # behavior of that as in version 5 of ActiveSupport.
29
+ spec.add_dependency 'activesupport', '>= 4.0', '< 6'
26
30
  end
27
31
 
28
32
  File.open('schemacop.gemspec', 'w') { |f| f.write(gemspec.to_ruby.strip) }
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.2.0
data/doc/Schemacop.html CHANGED
@@ -109,7 +109,7 @@
109
109
  </div>
110
110
 
111
111
  <div id="footer">
112
- Generated on Tue May 16 11:56:24 2017 by
112
+ Generated on Wed May 17 10:53:58 2017 by
113
113
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
114
114
  0.9.9 (ruby-2.3.1).
115
115
  </div>
@@ -319,7 +319,7 @@
319
319
  </div>
320
320
 
321
321
  <div id="footer">
322
- Generated on Tue May 16 11:56:25 2017 by
322
+ Generated on Wed May 17 10:53:58 2017 by
323
323
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
324
324
  0.9.9 (ruby-2.3.1).
325
325
  </div>
@@ -135,7 +135,7 @@
135
135
  </div>
136
136
 
137
137
  <div id="footer">
138
- Generated on Tue May 16 11:56:25 2017 by
138
+ Generated on Wed May 17 10:53:58 2017 by
139
139
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
140
140
  0.9.9 (ruby-2.3.1).
141
141
  </div>
@@ -525,7 +525,7 @@
525
525
  </div>
526
526
 
527
527
  <div id="footer">
528
- Generated on Tue May 16 11:56:24 2017 by
528
+ Generated on Wed May 17 10:53:58 2017 by
529
529
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
530
530
  0.9.9 (ruby-2.3.1).
531
531
  </div>
@@ -105,7 +105,7 @@
105
105
  </div>
106
106
 
107
107
  <div id="footer">
108
- Generated on Tue May 16 11:56:24 2017 by
108
+ Generated on Wed May 17 10:53:58 2017 by
109
109
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
110
110
  0.9.9 (ruby-2.3.1).
111
111
  </div>
@@ -114,7 +114,7 @@
114
114
  </div>
115
115
 
116
116
  <div id="footer">
117
- Generated on Tue May 16 11:56:24 2017 by
117
+ Generated on Wed May 17 10:53:58 2017 by
118
118
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
119
119
  0.9.9 (ruby-2.3.1).
120
120
  </div>
@@ -114,7 +114,7 @@
114
114
  </div>
115
115
 
116
116
  <div id="footer">
117
- Generated on Tue May 16 11:56:24 2017 by
117
+ Generated on Wed May 17 10:53:58 2017 by
118
118
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
119
119
  0.9.9 (ruby-2.3.1).
120
120
  </div>
@@ -399,7 +399,7 @@
399
399
  </div>
400
400
 
401
401
  <div id="footer">
402
- Generated on Tue May 16 11:56:24 2017 by
402
+ Generated on Wed May 17 10:53:58 2017 by
403
403
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
404
404
  0.9.9 (ruby-2.3.1).
405
405
  </div>
@@ -148,7 +148,7 @@
148
148
  </div>
149
149
 
150
150
  <div id="footer">
151
- Generated on Tue May 16 11:56:25 2017 by
151
+ Generated on Wed May 17 10:53:58 2017 by
152
152
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
153
153
  0.9.9 (ruby-2.3.1).
154
154
  </div>
@@ -226,7 +226,20 @@
226
226
  13
227
227
  14
228
228
  15
229
- 16</pre>
229
+ 16
230
+ 17
231
+ 18
232
+ 19
233
+ 20
234
+ 21
235
+ 22
236
+ 23
237
+ 24
238
+ 25
239
+ 26
240
+ 27
241
+ 28
242
+ 29</pre>
230
243
  </td>
231
244
  <td>
232
245
  <pre class="code"><span class="info file"># File 'lib/schemacop/validator/hash_validator.rb', line 5</span>
@@ -234,8 +247,21 @@
234
247
  <span class='kw'>def</span> <span class='id identifier rubyid_validate'>validate</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='comma'>,</span> <span class='id identifier rubyid_collector'>collector</span><span class='rparen'>)</span>
235
248
  <span class='kw'>super</span>
236
249
 
237
- <span class='id identifier rubyid_allowed_fields'>allowed_fields</span> <span class='op'>=</span> <span class='ivar'>@fields</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span>
238
- <span class='id identifier rubyid_obsolete_keys'>obsolete_keys</span> <span class='op'>=</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span> <span class='op'>-</span> <span class='id identifier rubyid_allowed_fields'>allowed_fields</span>
250
+ <span class='kw'>if</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>ActiveSupport</span><span class='op'>::</span><span class='const'>HashWithIndifferentAccess</span>
251
+ <span class='id identifier rubyid_allowed_fields'>allowed_fields</span> <span class='op'>=</span> <span class='ivar'>@fields</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span> <span class='id identifier rubyid_k'>k</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_k'>k</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span> <span class='op'>:</span> <span class='id identifier rubyid_k'>k</span> <span class='rbrace'>}</span>
252
+ <span class='id identifier rubyid_data_keys'>data_keys</span> <span class='op'>=</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span> <span class='id identifier rubyid_k'>k</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_k'>k</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span> <span class='op'>:</span> <span class='id identifier rubyid_k'>k</span> <span class='rbrace'>}</span>
253
+
254
+ <span class='comment'># If the same key is specified in the schema as string and symbol, we
255
+ </span> <span class='comment'># definitely expect a Ruby hash and not one with indifferent access
256
+ </span> <span class='kw'>if</span> <span class='ivar'>@fields</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span> <span class='op'>!=</span> <span class='const'>Set</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_allowed_fields'>allowed_fields</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span>
257
+ <span class='id identifier rubyid_fail'>fail</span> <span class='const'><span class='object_link'><a href="Exceptions.html" title="Schemacop::Exceptions (module)">Exceptions</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Exceptions/ValidationError.html" title="Schemacop::Exceptions::ValidationError (class)">ValidationError</a></span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Hash expected, but got ActiveSupport::HashWithIndifferentAccess.</span><span class='tstring_end'>&#39;</span></span>
258
+ <span class='kw'>end</span>
259
+ <span class='kw'>else</span>
260
+ <span class='id identifier rubyid_allowed_fields'>allowed_fields</span> <span class='op'>=</span> <span class='ivar'>@fields</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span>
261
+ <span class='id identifier rubyid_data_keys'>data_keys</span> <span class='op'>=</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span>
262
+ <span class='kw'>end</span>
263
+
264
+ <span class='id identifier rubyid_obsolete_keys'>obsolete_keys</span> <span class='op'>=</span> <span class='id identifier rubyid_data_keys'>data_keys</span> <span class='op'>-</span> <span class='id identifier rubyid_allowed_fields'>allowed_fields</span>
239
265
 
240
266
  <span class='id identifier rubyid_collector'>collector</span><span class='period'>.</span><span class='id identifier rubyid_error'>error</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Obsolete keys: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_obsolete_keys'>obsolete_keys</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_content'>.</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_obsolete_keys'>obsolete_keys</span><span class='period'>.</span><span class='id identifier rubyid_any?'>any?</span>
241
267
 
@@ -253,7 +279,7 @@
253
279
  </div>
254
280
 
255
281
  <div id="footer">
256
- Generated on Tue May 16 11:56:25 2017 by
282
+ Generated on Wed May 17 10:53:58 2017 by
257
283
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
258
284
  0.9.9 (ruby-2.3.1).
259
285
  </div>
@@ -148,7 +148,7 @@
148
148
  </div>
149
149
 
150
150
  <div id="footer">
151
- Generated on Tue May 16 11:56:25 2017 by
151
+ Generated on Wed May 17 10:53:58 2017 by
152
152
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
153
153
  0.9.9 (ruby-2.3.1).
154
154
  </div>
@@ -135,7 +135,7 @@
135
135
  </div>
136
136
 
137
137
  <div id="footer">
138
- Generated on Tue May 16 11:56:24 2017 by
138
+ Generated on Wed May 17 10:53:58 2017 by
139
139
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
140
140
  0.9.9 (ruby-2.3.1).
141
141
  </div>
@@ -1416,7 +1416,7 @@
1416
1416
  </div>
1417
1417
 
1418
1418
  <div id="footer">
1419
- Generated on Tue May 16 11:56:24 2017 by
1419
+ Generated on Wed May 17 10:53:58 2017 by
1420
1420
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
1421
1421
  0.9.9 (ruby-2.3.1).
1422
1422
  </div>
@@ -232,7 +232,7 @@
232
232
  </div>
233
233
 
234
234
  <div id="footer">
235
- Generated on Tue May 16 11:56:24 2017 by
235
+ Generated on Wed May 17 10:53:58 2017 by
236
236
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
237
237
  0.9.9 (ruby-2.3.1).
238
238
  </div>
@@ -580,7 +580,7 @@
580
580
  </div>
581
581
 
582
582
  <div id="footer">
583
- Generated on Tue May 16 11:56:24 2017 by
583
+ Generated on Wed May 17 10:53:58 2017 by
584
584
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
585
585
  0.9.9 (ruby-2.3.1).
586
586
  </div>
@@ -604,7 +604,7 @@ as it formerly was the case in the constructor.</p>
604
604
  </div>
605
605
 
606
606
  <div id="footer">
607
- Generated on Tue May 16 11:56:24 2017 by
607
+ Generated on Wed May 17 10:53:58 2017 by
608
608
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
609
609
  0.9.9 (ruby-2.3.1).
610
610
  </div>
@@ -279,7 +279,7 @@
279
279
  </div>
280
280
 
281
281
  <div id="footer">
282
- Generated on Tue May 16 11:56:24 2017 by
282
+ Generated on Wed May 17 10:53:58 2017 by
283
283
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
284
284
  0.9.9 (ruby-2.3.1).
285
285
  </div>
@@ -222,7 +222,7 @@
222
222
  </div>
223
223
 
224
224
  <div id="footer">
225
- Generated on Tue May 16 11:56:25 2017 by
225
+ Generated on Wed May 17 10:53:58 2017 by
226
226
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
227
227
  0.9.9 (ruby-2.3.1).
228
228
  </div>
@@ -278,7 +278,7 @@
278
278
  </div>
279
279
 
280
280
  <div id="footer">
281
- Generated on Tue May 16 11:56:25 2017 by
281
+ Generated on Wed May 17 10:53:58 2017 by
282
282
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
283
283
  0.9.9 (ruby-2.3.1).
284
284
  </div>
@@ -161,7 +161,7 @@
161
161
  </div>
162
162
 
163
163
  <div id="footer">
164
- Generated on Tue May 16 11:56:24 2017 by
164
+ Generated on Wed May 17 10:53:58 2017 by
165
165
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
166
166
  0.9.9 (ruby-2.3.1).
167
167
  </div>
@@ -687,7 +687,7 @@ this exception is thrown.</p>
687
687
  </div>
688
688
 
689
689
  <div id="footer">
690
- Generated on Tue May 16 11:56:24 2017 by
690
+ Generated on Wed May 17 10:53:58 2017 by
691
691
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
692
692
  0.9.9 (ruby-2.3.1).
693
693
  </div>
@@ -285,7 +285,7 @@
285
285
  </div>
286
286
 
287
287
  <div id="footer">
288
- Generated on Tue May 16 11:56:25 2017 by
288
+ Generated on Wed May 17 10:53:58 2017 by
289
289
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
290
290
  0.9.9 (ruby-2.3.1).
291
291
  </div>
@@ -135,7 +135,7 @@
135
135
  </div>
136
136
 
137
137
  <div id="footer">
138
- Generated on Tue May 16 11:56:25 2017 by
138
+ Generated on Wed May 17 10:53:58 2017 by
139
139
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
140
140
  0.9.9 (ruby-2.3.1).
141
141
  </div>
data/doc/ScopedEnv.html CHANGED
@@ -341,7 +341,7 @@
341
341
  </div>
342
342
 
343
343
  <div id="footer">
344
- Generated on Tue May 16 11:56:24 2017 by
344
+ Generated on Wed May 17 10:53:58 2017 by
345
345
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
346
346
  0.9.9 (ruby-2.3.1).
347
347
  </div>
data/doc/_index.html CHANGED
@@ -347,7 +347,7 @@
347
347
  </div>
348
348
 
349
349
  <div id="footer">
350
- Generated on Tue May 16 11:56:24 2017 by
350
+ Generated on Wed May 17 10:53:58 2017 by
351
351
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
352
352
  0.9.9 (ruby-2.3.1).
353
353
  </div>
data/doc/file.README.html CHANGED
@@ -280,8 +280,8 @@ option <code>min</code> is supported by the <code>:string</code> validator (cove
280
280
 
281
281
  <h3>Field Line</h3>
282
282
 
283
- <p>Inside a Type Line of type <code>:hash</code> or <code>Hash</code>, you may specify an arbitrary
284
- number of field lines (one for each key-value pair you want to be in the hash).</p>
283
+ <p>Inside a Type Line of type <code>:hash</code>, you may specify an arbitrary number of field
284
+ lines (one for each key-value pair you want to be in the hash).</p>
285
285
 
286
286
  <p>Field Lines start with one of the following six identifiers: <code>req</code>, <code>req?</code>,
287
287
  <code>req!</code>, <code>opt</code>, <code>opt?</code> or <code>opt!</code>:</p>
@@ -327,6 +327,35 @@ non-nil field of type Integer or Date under the key <code>:age</code>.</p>
327
327
  <p>You might find the notation cumbersome, and you&#39;d be right to say so. Luckily
328
328
  there are plenty of short forms available which we will see below.</p>
329
329
 
330
+ <h4>Handling hashes with indifferent access</h4>
331
+
332
+ <p>Schemacop has special handling for objects of the class
333
+ <code>ActiveSupport::HashWithIndifferentAccess</code>: You may specify the keys as symbols
334
+ or strings, and Schemacop will handle the conversion necessary for proper
335
+ validation internally. Note that if you define the same key as string and
336
+ symbol, it will throw a <code>ValidationError</code> <a href="#exceptions">exception</a> when asked to
337
+ validate a hash with indifferent access.</p>
338
+
339
+ <p>Thus, the following two schema definitions are equivalent when validating a hash
340
+ with indifferent access:</p>
341
+
342
+ <pre class="code ruby"><code class="ruby"><span class='const'>Schema</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span>
343
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:hash</span> <span class='kw'>do</span>
344
+ <span class='id identifier rubyid_req'>req</span> <span class='symbol'>:name</span> <span class='kw'>do</span>
345
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:string</span>
346
+ <span class='kw'>end</span>
347
+ <span class='kw'>end</span>
348
+ <span class='kw'>end</span>
349
+
350
+ <span class='const'>Schema</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span>
351
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:hash</span> <span class='kw'>do</span>
352
+ <span class='id identifier rubyid_req'>req</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>name</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>do</span>
353
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:string</span>
354
+ <span class='kw'>end</span>
355
+ <span class='kw'>end</span>
356
+ <span class='kw'>end</span>
357
+ </code></pre>
358
+
330
359
  <h2>Types</h2>
331
360
 
332
361
  <p>Types are defined via their validators, which is a class under <code>validator/</code>.
@@ -374,7 +403,7 @@ own validation (e.g. through the option <code>check</code>).</li>
374
403
  <li>TODO no lookahead for different arrays, see
375
404
  validator_array_test#test_multiple_arrays</li>
376
405
  </ul></li>
377
- <li><p><code>:hash</code> accepts a Ruby Hash.</p>
406
+ <li><p><code>:hash</code> accepts a Ruby Hash or an <code>ActiveSupport::HashWithIndifferentAccess</code>.</p>
378
407
 
379
408
  <ul>
380
409
  <li>accepts a block with an arbitrary number of Field Lines.</li>
@@ -590,7 +619,7 @@ to <a href="http://www.subgit.com/">SubGit</a> for their great open source licen
590
619
  </div></div>
591
620
 
592
621
  <div id="footer">
593
- Generated on Tue May 16 11:56:24 2017 by
622
+ Generated on Wed May 17 10:53:58 2017 by
594
623
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
595
624
  0.9.9 (ruby-2.3.1).
596
625
  </div>
data/doc/index.html CHANGED
@@ -280,8 +280,8 @@ option <code>min</code> is supported by the <code>:string</code> validator (cove
280
280
 
281
281
  <h3>Field Line</h3>
282
282
 
283
- <p>Inside a Type Line of type <code>:hash</code> or <code>Hash</code>, you may specify an arbitrary
284
- number of field lines (one for each key-value pair you want to be in the hash).</p>
283
+ <p>Inside a Type Line of type <code>:hash</code>, you may specify an arbitrary number of field
284
+ lines (one for each key-value pair you want to be in the hash).</p>
285
285
 
286
286
  <p>Field Lines start with one of the following six identifiers: <code>req</code>, <code>req?</code>,
287
287
  <code>req!</code>, <code>opt</code>, <code>opt?</code> or <code>opt!</code>:</p>
@@ -327,6 +327,35 @@ non-nil field of type Integer or Date under the key <code>:age</code>.</p>
327
327
  <p>You might find the notation cumbersome, and you&#39;d be right to say so. Luckily
328
328
  there are plenty of short forms available which we will see below.</p>
329
329
 
330
+ <h4>Handling hashes with indifferent access</h4>
331
+
332
+ <p>Schemacop has special handling for objects of the class
333
+ <code>ActiveSupport::HashWithIndifferentAccess</code>: You may specify the keys as symbols
334
+ or strings, and Schemacop will handle the conversion necessary for proper
335
+ validation internally. Note that if you define the same key as string and
336
+ symbol, it will throw a <code>ValidationError</code> <a href="#exceptions">exception</a> when asked to
337
+ validate a hash with indifferent access.</p>
338
+
339
+ <p>Thus, the following two schema definitions are equivalent when validating a hash
340
+ with indifferent access:</p>
341
+
342
+ <pre class="code ruby"><code class="ruby"><span class='const'>Schema</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span>
343
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:hash</span> <span class='kw'>do</span>
344
+ <span class='id identifier rubyid_req'>req</span> <span class='symbol'>:name</span> <span class='kw'>do</span>
345
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:string</span>
346
+ <span class='kw'>end</span>
347
+ <span class='kw'>end</span>
348
+ <span class='kw'>end</span>
349
+
350
+ <span class='const'>Schema</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span>
351
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:hash</span> <span class='kw'>do</span>
352
+ <span class='id identifier rubyid_req'>req</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>name</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>do</span>
353
+ <span class='id identifier rubyid_type'>type</span> <span class='symbol'>:string</span>
354
+ <span class='kw'>end</span>
355
+ <span class='kw'>end</span>
356
+ <span class='kw'>end</span>
357
+ </code></pre>
358
+
330
359
  <h2>Types</h2>
331
360
 
332
361
  <p>Types are defined via their validators, which is a class under <code>validator/</code>.
@@ -374,7 +403,7 @@ own validation (e.g. through the option <code>check</code>).</li>
374
403
  <li>TODO no lookahead for different arrays, see
375
404
  validator_array_test#test_multiple_arrays</li>
376
405
  </ul></li>
377
- <li><p><code>:hash</code> accepts a Ruby Hash.</p>
406
+ <li><p><code>:hash</code> accepts a Ruby Hash or an <code>ActiveSupport::HashWithIndifferentAccess</code>.</p>
378
407
 
379
408
  <ul>
380
409
  <li>accepts a block with an arbitrary number of Field Lines.</li>
@@ -590,7 +619,7 @@ to <a href="http://www.subgit.com/">SubGit</a> for their great open source licen
590
619
  </div></div>
591
620
 
592
621
  <div id="footer">
593
- Generated on Tue May 16 11:56:24 2017 by
622
+ Generated on Wed May 17 10:53:58 2017 by
594
623
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
595
624
  0.9.9 (ruby-2.3.1).
596
625
  </div>
@@ -102,7 +102,7 @@
102
102
  </div>
103
103
 
104
104
  <div id="footer">
105
- Generated on Tue May 16 11:56:24 2017 by
105
+ Generated on Wed May 17 10:53:58 2017 by
106
106
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
107
  0.9.9 (ruby-2.3.1).
108
108
  </div>
data/lib/schemacop.rb CHANGED
@@ -3,6 +3,7 @@ end
3
3
 
4
4
  require 'set'
5
5
  require 'active_support/core_ext/class/attribute'
6
+ require 'active_support/hash_with_indifferent_access'
6
7
 
7
8
  require 'schemacop/scoped_env'
8
9
  require 'schemacop/exceptions'
@@ -5,8 +5,21 @@ module Schemacop
5
5
  def validate(data, collector)
6
6
  super
7
7
 
8
- allowed_fields = @fields.keys
9
- obsolete_keys = data.keys - allowed_fields
8
+ if data.is_a? ActiveSupport::HashWithIndifferentAccess
9
+ allowed_fields = @fields.keys.map { |k| k.is_a?(String) ? k.to_sym : k }
10
+ data_keys = data.keys.map { |k| k.is_a?(String) ? k.to_sym : k }
11
+
12
+ # If the same key is specified in the schema as string and symbol, we
13
+ # definitely expect a Ruby hash and not one with indifferent access
14
+ if @fields.keys.length != Set.new(allowed_fields).length
15
+ fail Exceptions::ValidationError, 'Hash expected, but got ActiveSupport::HashWithIndifferentAccess.'
16
+ end
17
+ else
18
+ allowed_fields = @fields.keys
19
+ data_keys = data.keys
20
+ end
21
+
22
+ obsolete_keys = data_keys - allowed_fields
10
23
 
11
24
  collector.error "Obsolete keys: #{obsolete_keys.inspect}." if obsolete_keys.any?
12
25
 
data/schemacop.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 2.1.0 ruby lib
2
+ # stub: schemacop 2.2.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "2.1.0"
6
+ s.version = "2.2.0"
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 = "2017-05-16"
11
+ s.date = "2017-05-17"
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, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "doc/Schemacop.html".freeze, "doc/Schemacop/ArrayValidator.html".freeze, "doc/Schemacop/BooleanValidator.html".freeze, "doc/Schemacop/Collector.html".freeze, "doc/Schemacop/Exceptions.html".freeze, "doc/Schemacop/Exceptions/InvalidSchemaError.html".freeze, "doc/Schemacop/Exceptions/ValidationError.html".freeze, "doc/Schemacop/FieldNode.html".freeze, "doc/Schemacop/FloatValidator.html".freeze, "doc/Schemacop/HashValidator.html".freeze, "doc/Schemacop/IntegerValidator.html".freeze, "doc/Schemacop/NilValidator.html".freeze, "doc/Schemacop/Node.html".freeze, "doc/Schemacop/NodeResolver.html".freeze, "doc/Schemacop/NodeSupportingField.html".freeze, "doc/Schemacop/NodeSupportingType.html".freeze, "doc/Schemacop/NodeWithBlock.html".freeze, "doc/Schemacop/NumberValidator.html".freeze, "doc/Schemacop/ObjectValidator.html".freeze, "doc/Schemacop/RootNode.html".freeze, "doc/Schemacop/Schema.html".freeze, "doc/Schemacop/StringValidator.html".freeze, "doc/Schemacop/SymbolValidator.html".freeze, "doc/ScopedEnv.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/inheritance.graphml".freeze, "doc/inheritance.pdf".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/collector.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/field_node.rb".freeze, "lib/schemacop/node.rb".freeze, "lib/schemacop/node_resolver.rb".freeze, "lib/schemacop/node_supporting_field.rb".freeze, "lib/schemacop/node_supporting_type.rb".freeze, "lib/schemacop/node_with_block.rb".freeze, "lib/schemacop/root_node.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/validator/array_validator.rb".freeze, "lib/schemacop/validator/boolean_validator.rb".freeze, "lib/schemacop/validator/float_validator.rb".freeze, "lib/schemacop/validator/hash_validator.rb".freeze, "lib/schemacop/validator/integer_validator.rb".freeze, "lib/schemacop/validator/nil_validator.rb".freeze, "lib/schemacop/validator/number_validator.rb".freeze, "lib/schemacop/validator/object_validator.rb".freeze, "lib/schemacop/validator/string_validator.rb".freeze, "lib/schemacop/validator/symbol_validator.rb".freeze, "schemacop.gemspec".freeze, "test/custom_check_test.rb".freeze, "test/custom_if_test.rb".freeze, "test/nil_dis_allow_test.rb".freeze, "test/short_forms_test.rb".freeze, "test/test_helper.rb".freeze, "test/types_test.rb".freeze, "test/validator_array_test.rb".freeze, "test/validator_boolean_test.rb".freeze, "test/validator_float_test.rb".freeze, "test/validator_hash_test.rb".freeze, "test/validator_integer_test.rb".freeze, "test/validator_nil_test.rb".freeze, "test/validator_number_test.rb".freeze, "test/validator_object_test.rb".freeze, "test/validator_string_test.rb".freeze, "test/validator_symbol_test.rb".freeze]
13
13
  s.homepage = "https://github.com/sitrox/schemacop".freeze
14
14
  s.licenses = ["MIT".freeze]
@@ -24,31 +24,31 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency(%q<rake>.freeze, [">= 0"])
25
25
  s.add_development_dependency(%q<ci_reporter>.freeze, ["~> 2.0"])
26
26
  s.add_development_dependency(%q<ci_reporter_minitest>.freeze, [">= 0"])
27
- s.add_development_dependency(%q<activesupport>.freeze, [">= 0"])
28
27
  s.add_development_dependency(%q<haml>.freeze, [">= 0"])
29
28
  s.add_development_dependency(%q<yard>.freeze, [">= 0"])
30
29
  s.add_development_dependency(%q<rubocop>.freeze, ["= 0.35.1"])
31
30
  s.add_development_dependency(%q<redcarpet>.freeze, [">= 0"])
31
+ s.add_runtime_dependency(%q<activesupport>.freeze, ["< 6", ">= 4.0"])
32
32
  else
33
33
  s.add_dependency(%q<bundler>.freeze, ["~> 1.3"])
34
34
  s.add_dependency(%q<rake>.freeze, [">= 0"])
35
35
  s.add_dependency(%q<ci_reporter>.freeze, ["~> 2.0"])
36
36
  s.add_dependency(%q<ci_reporter_minitest>.freeze, [">= 0"])
37
- s.add_dependency(%q<activesupport>.freeze, [">= 0"])
38
37
  s.add_dependency(%q<haml>.freeze, [">= 0"])
39
38
  s.add_dependency(%q<yard>.freeze, [">= 0"])
40
39
  s.add_dependency(%q<rubocop>.freeze, ["= 0.35.1"])
41
40
  s.add_dependency(%q<redcarpet>.freeze, [">= 0"])
41
+ s.add_dependency(%q<activesupport>.freeze, ["< 6", ">= 4.0"])
42
42
  end
43
43
  else
44
44
  s.add_dependency(%q<bundler>.freeze, ["~> 1.3"])
45
45
  s.add_dependency(%q<rake>.freeze, [">= 0"])
46
46
  s.add_dependency(%q<ci_reporter>.freeze, ["~> 2.0"])
47
47
  s.add_dependency(%q<ci_reporter_minitest>.freeze, [">= 0"])
48
- s.add_dependency(%q<activesupport>.freeze, [">= 0"])
49
48
  s.add_dependency(%q<haml>.freeze, [">= 0"])
50
49
  s.add_dependency(%q<yard>.freeze, [">= 0"])
51
50
  s.add_dependency(%q<rubocop>.freeze, ["= 0.35.1"])
52
51
  s.add_dependency(%q<redcarpet>.freeze, [">= 0"])
52
+ s.add_dependency(%q<activesupport>.freeze, ["< 6", ">= 4.0"])
53
53
  end
54
54
  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: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-16 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: activesupport
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
69
  - !ruby/object:Gem::Dependency
84
70
  name: haml
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +122,26 @@ dependencies:
136
122
  - - ">="
137
123
  - !ruby/object:Gem::Version
138
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "<"
130
+ - !ruby/object:Gem::Version
131
+ version: '6'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '4.0'
135
+ type: :runtime
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "<"
140
+ - !ruby/object:Gem::Version
141
+ version: '6'
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '4.0'
139
145
  description:
140
146
  email:
141
147
  executables: []