strelka 0.0.1.pre177 → 0.0.1.pre184

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.
Files changed (38) hide show
  1. data/ChangeLog +111 -16
  2. data/Manifest.txt +8 -8
  3. data/Rakefile +3 -3
  4. data/bin/leash +51 -28
  5. data/examples/{auth-demo.rb → apps/auth-demo} +3 -3
  6. data/examples/{auth-demo2.rb → apps/auth-demo2} +0 -0
  7. data/examples/{sessions-demo.rb → apps/sessions-demo} +0 -0
  8. data/examples/config.yml +5 -1
  9. data/examples/{examples.css → static/examples.css} +0 -0
  10. data/examples/{examples.html → static/examples.html} +0 -0
  11. data/examples/{auth-form.tmpl → templates/auth-form.tmpl} +0 -0
  12. data/examples/{auth-success.tmpl → templates/auth-success.tmpl} +0 -0
  13. data/examples/{layout.tmpl → templates/layout.tmpl} +0 -0
  14. data/lib/strelka/app/auth.rb +18 -8
  15. data/lib/strelka/app/errors.rb +3 -2
  16. data/lib/strelka/app/filters.rb +2 -0
  17. data/lib/strelka/app/negotiation.rb +2 -0
  18. data/lib/strelka/app/parameters.rb +18 -140
  19. data/lib/strelka/app/plugins.rb +84 -26
  20. data/lib/strelka/app/restresources.rb +26 -18
  21. data/lib/strelka/app/routing.rb +8 -2
  22. data/lib/strelka/app/sessions.rb +7 -0
  23. data/lib/strelka/app/templating.rb +1 -1
  24. data/lib/strelka/app.rb +25 -1
  25. data/lib/strelka/constants.rb +3 -1
  26. data/lib/strelka/paramvalidator.rb +251 -74
  27. data/lib/strelka/session/default.rb +1 -1
  28. data/spec/strelka/app/auth_spec.rb +37 -0
  29. data/spec/strelka/app/errors_spec.rb +0 -2
  30. data/spec/strelka/app/filters_spec.rb +1 -1
  31. data/spec/strelka/app/parameters_spec.rb +4 -92
  32. data/spec/strelka/app/plugins_spec.rb +64 -2
  33. data/spec/strelka/app/restresources_spec.rb +3 -0
  34. data/spec/strelka/app/routing_spec.rb +5 -5
  35. data/spec/strelka/paramvalidator_spec.rb +294 -385
  36. data.tar.gz.sig +0 -0
  37. metadata +126 -46
  38. metadata.gz.sig +0 -0
@@ -3,7 +3,7 @@
3
3
 
4
4
  BEGIN {
5
5
  require 'pathname'
6
- basedir = Pathname.new( __FILE__ ).dirname.parent.parent.parent
6
+ basedir = Pathname.new( __FILE__ ).dirname.parent.parent
7
7
  $LOAD_PATH.unshift( basedir ) unless $LOAD_PATH.include?( basedir )
8
8
  }
9
9
 
@@ -21,112 +21,96 @@ require 'strelka/paramvalidator'
21
21
  #####################################################################
22
22
  describe Strelka::ParamValidator do
23
23
 
24
- TEST_PROFILE = {
25
- :required => [ :required ],
26
- :optional => %w{
27
- optional number int_constraint float_constraint bool_constraint email_constraint
28
- host_constraint regexp_w_captures regexp_w_one_capture regexp_w_named_captures
29
- alpha_constraint alphanumeric_constraint printable_constraint proc_constraint
30
- uri_constraint word_constraint date_constraint
31
- },
32
- :constraints => {
33
- :number => /^\d+$/,
34
- :regexp_w_captures => /(\w+)(\S+)?/,
35
- :regexp_w_one_capture => /(\w+)/,
36
- :regexp_w_named_captures => /(?<category>[[:upper:]]{3})-(?<sku>\d{12})/,
37
- :int_constraint => :integer,
38
- :float_constraint => :float,
39
- :bool_constraint => :boolean,
40
- :email_constraint => :email,
41
- :uri_constraint => :uri,
42
- :host_constraint => :hostname,
43
- :alpha_constraint => :alpha,
44
- :alphanumeric_constraint => :alphanumeric,
45
- :printable_constraint => :printable,
46
- :word_constraint => :word,
47
- :proc_constraint => Date.method( :parse ),
48
- :date_constraint => :date,
49
- },
50
- }
51
-
52
-
53
24
  before( :all ) do
54
25
  setup_logging( :fatal )
55
26
  end
56
27
 
57
- before(:each) do
58
- @validator = Strelka::ParamValidator.new( TEST_PROFILE )
59
- end
60
-
61
28
  after( :all ) do
62
29
  reset_logging()
63
30
  end
64
31
 
65
32
 
33
+ before(:each) do
34
+ @validator = Strelka::ParamValidator.new
35
+ end
36
+
37
+
66
38
  it "starts out empty" do
67
39
  @validator.should be_empty()
68
40
  @validator.should_not have_args()
69
41
  end
70
42
 
71
43
  it "is no longer empty if at least one set of parameters has been validated" do
72
- @validator.validate( {'required' => "1"} )
44
+ @validator.add( :foo, :integer )
45
+
46
+ @validator.validate( {'foo' => "1"} )
73
47
 
74
48
  @validator.should_not be_empty()
75
49
  @validator.should have_args()
76
50
  end
77
51
 
78
- it "raises an exception on an unknown constraint type" do
79
- pending "figure out why this isn't working" do
80
- profile = {
81
- required: [:required],
82
- constraints: {
83
- required: $stderr,
84
- }
85
- }
86
- val = Strelka::ParamValidator.new( profile )
87
-
88
- expect {
89
- val.validate( required: '1' )
90
- }.to raise_error( /unknown constraint type IO/ )
91
- end
52
+ it "allows constraints to be added" do
53
+ @validator.add( :a_field, :string )
54
+ @validator.param_names.should include( :a_field )
92
55
  end
93
56
 
94
- # Test index operator interface
95
- it "provides read and write access to valid args via the index operator" do
96
- rval = nil
57
+ it "doesn't allow a parameter to be added twice" do
58
+ @validator.add( :a_field, :string )
59
+ expect {
60
+ @validator.add( :a_field, :string )
61
+ }.to raise_error( /parameter :a_field is already defined/i )
62
+ @validator.param_names.should include( :a_field )
63
+ end
97
64
 
98
- @validator.validate( {'required' => "1"} )
99
- @validator[:required].should == "1"
65
+ it "allows an existing constraint to be overridden" do
66
+ @validator.add( :a_field, :string )
67
+ @validator.override( :a_field, :integer )
68
+ @validator.param_names.should include( :a_field )
69
+ @validator.validate( 'a_field' => 'a string!' )
70
+ @validator.should have_errors()
71
+ @validator.should_not be_okay()
72
+ @validator.error_messages.should include( "Invalid value for 'A Field'" )
73
+ end
100
74
 
101
- @validator[:required] = "bar"
102
- @validator["required"].should == "bar"
75
+ it "doesn't allow a non-existant parameter to be overridden" do
76
+ expect {
77
+ @validator.override( :a_field, :string )
78
+ }.to raise_error( /no parameter :a_field defined/i )
79
+ @validator.param_names.should_not include( :a_field )
103
80
  end
104
81
 
82
+ it "raises an exception on an unknown constraint type" do
83
+ expect {
84
+ @validator.add( :foo, $stderr )
85
+ }.to raise_error( /no builtin :foo validator/ )
86
+ end
105
87
 
106
- it "untaints valid args if told to do so" do
107
- rval = nil
108
- tainted_one = "1"
109
- tainted_one.taint
88
+ it "retains its parameters through a copy" do
89
+ @validator.add( :foo, :string, :required )
90
+ dup = @validator.dup
91
+ @validator.validate( {} )
92
+ @validator.should_not be_okay()
93
+ @validator.should have_errors()
94
+ @validator.error_messages.should == ["Missing value for 'Foo'"]
95
+ end
110
96
 
111
- @validator.validate( {'required' => 1, 'number' => tainted_one},
112
- :untaint_all_constraints => true )
97
+ it "provides read and write access to valid args via the index operator" do
98
+ @validator.add( :foo, /^\d+$/ )
113
99
 
114
- Strelka.log.debug "Validator: %p" % [@validator]
100
+ @validator.validate( {'foo' => "1"} )
101
+ @validator[:foo].should == "1"
115
102
 
116
- @validator[:number].should == "1"
117
- @validator[:number].tainted?.should be_false()
103
+ @validator[:foo] = "bar"
104
+ @validator["foo"].should == "bar"
118
105
  end
119
106
 
120
107
 
121
- it "untaints field names" do
122
- rval = nil
108
+ it "untaints valid args if told to do so" do
123
109
  tainted_one = "1"
124
110
  tainted_one.taint
125
111
 
126
- @validator.validate( {'required' => 1, 'number' => tainted_one},
127
- :untaint_all_constraints => true )
128
-
129
- Strelka.log.debug "Validator: %p" % [@validator]
112
+ @validator.add( :number, /^\d+$/, :untaint )
113
+ @validator.validate( 'number' => tainted_one )
130
114
 
131
115
  @validator[:number].should == "1"
132
116
  @validator[:number].tainted?.should be_false()
@@ -134,65 +118,48 @@ describe Strelka::ParamValidator do
134
118
 
135
119
 
136
120
  it "returns the capture from a regexp constraint if it has only one" do
137
- rval = nil
138
- params = { 'required' => 1, 'regexp_w_one_capture' => " ygdrassil " }
139
-
140
- @validator.validate( params, :untaint_all_constraints => true )
141
-
142
- Strelka.log.debug "Validator: %p" % [@validator]
143
-
144
- @validator[:regexp_w_one_capture].should == 'ygdrassil'
121
+ @validator.add( :treename, /(\w+)/ )
122
+ @validator.validate( 'treename' => " ygdrassil " )
123
+ @validator[:treename].should == 'ygdrassil'
145
124
  end
146
125
 
147
126
  it "returns the captures from a regexp constraint as an array if it has more than one" do
148
- rval = nil
149
- params = { 'required' => 1, 'regexp_w_captures' => " the1tree(!) " }
150
-
151
- @validator.validate( params, :untaint_all_constraints => true )
152
-
153
- Strelka.log.debug "Validator: %p" % [@validator]
154
-
155
- @validator[:regexp_w_captures].should == ['the1tree', '(!)']
127
+ @validator.add( :stuff, /(\w+)(\S+)?/ )
128
+ @validator.validate( 'stuff' => " the1tree(!) " )
129
+ @validator[:stuff].should == ['the1tree', '(!)']
156
130
  end
157
131
 
158
132
  it "returns the captures from a regexp constraint with named captures as a Hash" do
159
- rval = nil
160
- params = { 'required' => 1, 'regexp_w_named_captures' => " JVV-886451300133 ".taint }
161
-
162
- @validator.validate( params, :untaint_all_constraints => true )
133
+ @validator.add( :order_number, /(?<category>[[:upper:]]{3})-(?<sku>\d{12})/, :untaint )
134
+ @validator.validate( 'order_number' => " JVV-886451300133 ".taint )
163
135
 
164
- Strelka.log.debug "Validator: %p" % [@validator]
165
-
166
- @validator[:regexp_w_named_captures].should == {:category => 'JVV', :sku => '886451300133'}
167
- @validator[:regexp_w_named_captures][:category].should_not be_tainted()
168
- @validator[:regexp_w_named_captures][:sku].should_not be_tainted()
136
+ @validator[:order_number].should == {:category => 'JVV', :sku => '886451300133'}
137
+ @validator[:order_number][:category].should_not be_tainted()
138
+ @validator[:order_number][:sku].should_not be_tainted()
169
139
  end
170
140
 
171
141
  it "returns the captures from a regexp constraint as an array " +
172
142
  "even if an optional capture doesn't match anything" do
173
- rval = nil
174
- params = { 'required' => 1, 'regexp_w_captures' => " the1tree " }
175
-
176
- @validator.validate( params, :untaint_all_constraints => true )
177
-
178
- Strelka.log.debug "Validator: %p" % [@validator]
143
+ @validator.add( :amount, /^([\-+])?(\d+(?:\.\d+)?)/ )
144
+ @validator.validate( 'amount' => '2.28' )
179
145
 
180
- @validator[:regexp_w_captures].should == ['the1tree', nil]
146
+ @validator[:amount].should == [ nil, '2.28' ]
181
147
  end
182
148
 
183
149
  it "knows the names of fields that were required but missing from the parameters" do
150
+ @validator.add( :id, :integer, :required )
184
151
  @validator.validate( {} )
185
152
 
186
153
  @validator.should have_errors()
187
154
  @validator.should_not be_okay()
188
155
 
189
156
  @validator.missing.should have(1).members
190
- @validator.missing.should == ['required']
157
+ @validator.missing.should == ['id']
191
158
  end
192
159
 
193
160
  it "knows the names of fields that did not meet their constraints" do
194
- params = {'number' => 'rhinoceros'}
195
- @validator.validate( params )
161
+ @validator.add( :number, :integer, :required )
162
+ @validator.validate( 'number' => 'rhinoceros' )
196
163
 
197
164
  @validator.should have_errors()
198
165
  @validator.should_not be_okay()
@@ -203,44 +170,45 @@ describe Strelka::ParamValidator do
203
170
 
204
171
  it "can return a combined list of all problem parameters, which includes " +
205
172
  " both missing and invalid fields" do
206
- params = {'number' => 'rhinoceros'}
207
- @validator.validate( params )
173
+ @validator.add( :number, :integer )
174
+ @validator.add( :id, /^(\w{20})$/, :required )
175
+
176
+ @validator.validate( 'number' => 'rhinoceros' )
208
177
 
209
178
  @validator.should have_errors()
210
179
  @validator.should_not be_okay()
211
180
 
212
181
  @validator.error_fields.should have(2).members
213
182
  @validator.error_fields.should include('number')
214
- @validator.error_fields.should include('required')
183
+ @validator.error_fields.should include('id')
215
184
  end
216
185
 
217
186
  it "can return human descriptions of validation errors" do
218
- params = {'number' => 'rhinoceros', 'unknown' => "1"}
219
- @validator.validate( params )
187
+ @validator.add( :number, :integer )
188
+ @validator.add( :id, /^(\w{20})$/, :required )
189
+ @validator.validate( 'number' => 'rhinoceros', 'unknown' => "1" )
220
190
 
221
191
  @validator.error_messages.should have(2).members
222
- @validator.error_messages.should include("Missing value for 'Required'")
192
+ @validator.error_messages.should include("Missing value for 'Id'")
223
193
  @validator.error_messages.should include("Invalid value for 'Number'")
224
194
  end
225
195
 
226
196
  it "can include unknown fields in its human descriptions of validation errors" do
227
- params = {'number' => 'rhinoceros', 'unknown' => "1"}
228
- @validator.validate( params )
197
+ @validator.add( :number, :integer )
198
+ @validator.add( :id, /^(\w{20})$/, :required )
199
+ @validator.validate( 'number' => 'rhinoceros', 'unknown' => "1" )
229
200
 
230
201
  @validator.error_messages(true).should have(3).members
231
- @validator.error_messages(true).should include("Missing value for 'Required'")
202
+ @validator.error_messages(true).should include("Missing value for 'Id'")
232
203
  @validator.error_messages(true).should include("Invalid value for 'Number'")
233
204
  @validator.error_messages(true).should include("Unknown parameter 'Unknown'")
234
205
  end
235
206
 
236
207
  it "can use provided descriptions of parameters when constructing human " +
237
208
  "validation error messages" do
238
- descs = {
239
- :number => "Numeral",
240
- :required => "Test Name",
241
- }
242
- params = {'number' => 'rhinoceros', 'unknown' => "1"}
243
- @validator.validate( params, :descriptions => descs )
209
+ @validator.add( :number, :integer, "Numeral" )
210
+ @validator.add( :id, /^(\w{20})$/, "Test Name", :required )
211
+ @validator.validate( 'number' => 'rhinoceros', 'unknown' => "1" )
244
212
 
245
213
  @validator.error_messages.should have(2).members
246
214
  @validator.error_messages.should include("Missing value for 'Test Name'")
@@ -248,13 +216,14 @@ describe Strelka::ParamValidator do
248
216
  end
249
217
 
250
218
  it "can get and set the profile's descriptions directly" do
251
- params = {'number' => 'rhinoceros', 'unknown' => "1"}
219
+ @validator.add( :number, :integer )
220
+ @validator.add( :id, /^(\w{20})$/, :required )
252
221
 
253
222
  @validator.descriptions = {
254
223
  number: 'Numeral',
255
- required: 'Test Name'
224
+ id: 'Test Name'
256
225
  }
257
- @validator.validate( params )
226
+ @validator.validate( 'number' => 'rhinoceros', 'unknown' => "1" )
258
227
 
259
228
  @validator.descriptions.should have( 2 ).members
260
229
  @validator.error_messages.should have( 2 ).members
@@ -279,26 +248,24 @@ describe Strelka::ParamValidator do
279
248
  end
280
249
 
281
250
  it "coalesces simple hash fields into a hash of validated values" do
282
- @validator.validate( {'rodent[size]' => 'unusual'}, :optional => ['rodent[size]'] )
251
+ @validator.add( 'rodent[size]', :string )
252
+ @validator.validate( 'rodent[size]' => 'unusual' )
283
253
 
284
254
  @validator.valid.should == {'rodent' => {'size' => 'unusual'}}
285
255
  end
286
256
 
287
257
  it "coalesces complex hash fields into a nested hash of validated values" do
288
- profile = {
289
- :optional => [
290
- 'recipe[ingredient][name]',
291
- 'recipe[ingredient][cost]',
292
- 'recipe[yield]'
293
- ]
294
- }
258
+ @validator.add( 'recipe[ingredient][name]', :string )
259
+ @validator.add( 'recipe[ingredient][cost]', :string )
260
+ @validator.add( 'recipe[yield]', :string )
261
+
295
262
  args = {
296
263
  'recipe[ingredient][name]' => 'nutmeg',
297
264
  'recipe[ingredient][cost]' => '$0.18',
298
265
  'recipe[yield]' => '2 loaves',
299
266
  }
267
+ @validator.validate( args )
300
268
 
301
- @validator.validate( args, profile )
302
269
  @validator.valid.should == {
303
270
  'recipe' => {
304
271
  'ingredient' => { 'name' => 'nutmeg', 'cost' => '$0.18' },
@@ -308,28 +275,19 @@ describe Strelka::ParamValidator do
308
275
  end
309
276
 
310
277
  it "untaints both keys and values in complex hash fields if untainting is turned on" do
311
- profile = {
312
- :required => [
313
- 'recipe[ingredient][rarity]',
314
- ],
315
- :optional => [
316
- 'recipe[ingredient][name]',
317
- 'recipe[ingredient][cost]',
318
- 'recipe[yield]'
319
- ],
320
- :constraints => {
321
- 'recipe[ingredient][rarity]' => /^([\w\-]+)$/,
322
- },
323
- :untaint_all_constraints => true,
324
- }
278
+ @validator.add( 'recipe[ingredient][rarity]', /^([\w\-]+)$/, :required )
279
+ @validator.add( 'recipe[ingredient][name]', :string )
280
+ @validator.add( 'recipe[ingredient][cost]', :string )
281
+ @validator.add( 'recipe[yield]', :string )
282
+ @validator.untaint_all_constraints
283
+
325
284
  args = {
326
285
  'recipe[ingredient][rarity]'.taint => 'super-rare'.taint,
327
286
  'recipe[ingredient][name]'.taint => 'nutmeg'.taint,
328
287
  'recipe[ingredient][cost]'.taint => '$0.18'.taint,
329
288
  'recipe[yield]'.taint => '2 loaves'.taint,
330
289
  }
331
-
332
- @validator.validate( args, profile )
290
+ @validator.validate( args )
333
291
 
334
292
  @validator.valid.should == {
335
293
  'recipe' => {
@@ -349,335 +307,306 @@ describe Strelka::ParamValidator do
349
307
  end
350
308
 
351
309
  it "accepts the value 'true' for fields with boolean constraints" do
352
- params = {'required' => '1', 'bool_constraint' => 'true'}
353
-
354
- @validator.validate( params )
310
+ @validator.add( :enabled, :boolean )
311
+ @validator.validate( 'enabled' => 'true' )
355
312
 
356
313
  @validator.should be_okay()
357
314
  @validator.should_not have_errors()
358
315
 
359
- @validator[:bool_constraint].should be_true()
316
+ @validator[:enabled].should be_true()
360
317
  end
361
318
 
362
319
  it "accepts the value 't' for fields with boolean constraints" do
363
- params = {'required' => '1', 'bool_constraint' => 't'}
364
-
365
- @validator.validate( params )
320
+ @validator.add( :enabled, :boolean )
321
+ @validator.validate( 'enabled' => 't' )
366
322
 
367
323
  @validator.should be_okay()
368
324
  @validator.should_not have_errors()
369
325
 
370
- @validator[:bool_constraint].should be_true()
326
+ @validator[:enabled].should be_true()
371
327
  end
372
328
 
373
329
  it "accepts the value 'yes' for fields with boolean constraints" do
374
- params = {'required' => '1', 'bool_constraint' => 'yes'}
375
-
376
- @validator.validate( params )
330
+ @validator.add( :enabled, :boolean )
331
+ @validator.validate( 'enabled' => 'yes' )
377
332
 
378
333
  @validator.should be_okay()
379
334
  @validator.should_not have_errors()
380
335
 
381
- @validator[:bool_constraint].should be_true()
336
+ @validator[:enabled].should be_true()
382
337
  end
383
338
 
384
339
  it "accepts the value 'y' for fields with boolean constraints" do
385
- params = {'required' => '1', 'bool_constraint' => 'y'}
386
-
387
- @validator.validate( params )
340
+ @validator.add( :enabled, :boolean )
341
+ @validator.validate( 'enabled' => 'y' )
388
342
 
389
343
  @validator.should be_okay()
390
344
  @validator.should_not have_errors()
391
345
 
392
- @validator[:bool_constraint].should be_true()
346
+ @validator[:enabled].should be_true()
393
347
  end
394
348
 
395
349
  it "accepts the value '1' for fields with boolean constraints" do
396
- params = {'required' => '1', 'bool_constraint' => '1'}
397
-
398
- @validator.validate( params )
350
+ @validator.add( :enabled, :boolean )
351
+ @validator.validate( 'enabled' => '1' )
399
352
 
400
353
  @validator.should be_okay()
401
354
  @validator.should_not have_errors()
402
355
 
403
- @validator[:bool_constraint].should be_true()
356
+ @validator[:enabled].should be_true()
404
357
  end
405
358
 
406
359
  it "accepts the value 'false' for fields with boolean constraints" do
407
- params = {'required' => '1', 'bool_constraint' => 'false'}
408
-
409
- @validator.validate( params )
360
+ @validator.add( :enabled, :boolean )
361
+ @validator.validate( 'enabled' => 'false' )
410
362
 
411
363
  @validator.should be_okay()
412
364
  @validator.should_not have_errors()
413
365
 
414
- @validator[:bool_constraint].should be_false()
366
+ @validator[:enabled].should be_false()
415
367
  end
416
368
 
417
369
  it "accepts the value 'f' for fields with boolean constraints" do
418
- params = {'required' => '1', 'bool_constraint' => 'f'}
419
-
420
- @validator.validate( params )
370
+ @validator.add( :enabled, :boolean )
371
+ @validator.validate( 'enabled' => 'f' )
421
372
 
422
373
  @validator.should be_okay()
423
374
  @validator.should_not have_errors()
424
375
 
425
- @validator[:bool_constraint].should be_false()
376
+ @validator[:enabled].should be_false()
426
377
  end
427
378
 
428
379
  it "accepts the value 'no' for fields with boolean constraints" do
429
- params = {'required' => '1', 'bool_constraint' => 'no'}
430
-
431
- @validator.validate( params )
380
+ @validator.add( :enabled, :boolean )
381
+ @validator.validate( 'enabled' => 'no' )
432
382
 
433
383
  @validator.should be_okay()
434
384
  @validator.should_not have_errors()
435
385
 
436
- @validator[:bool_constraint].should be_false()
386
+ @validator[:enabled].should be_false()
437
387
  end
438
388
 
439
389
  it "accepts the value 'n' for fields with boolean constraints" do
440
- params = {'required' => '1', 'bool_constraint' => 'n'}
441
-
442
- @validator.validate( params )
390
+ @validator.add( :enabled, :boolean )
391
+ @validator.validate( 'enabled' => 'n' )
443
392
 
444
393
  @validator.should be_okay()
445
394
  @validator.should_not have_errors()
446
395
 
447
- @validator[:bool_constraint].should be_false()
396
+ @validator[:enabled].should be_false()
448
397
  end
449
398
 
450
399
  it "accepts the value '0' for fields with boolean constraints" do
451
- params = {'required' => '1', 'bool_constraint' => '0'}
452
-
453
- @validator.validate( params )
400
+ @validator.add( :enabled, :boolean )
401
+ @validator.validate( 'enabled' => '0' )
454
402
 
455
403
  @validator.should be_okay()
456
404
  @validator.should_not have_errors()
457
405
 
458
- @validator[:bool_constraint].should be_false()
406
+ @validator[:enabled].should be_false()
459
407
  end
460
408
 
461
409
  it "rejects non-boolean parameters for fields with boolean constraints" do
462
- params = {'required' => '1', 'bool_constraint' => 'peanut'}
463
-
464
- @validator.validate( params )
410
+ @validator.add( :enabled, :boolean )
411
+ @validator.validate( 'enabled' => 'peanut' )
465
412
 
466
413
  @validator.should_not be_okay()
467
414
  @validator.should have_errors()
468
415
 
469
- @validator[:bool_constraint].should be_nil()
416
+ @validator[:enabled].should be_nil()
470
417
  end
471
418
 
472
419
  it "accepts simple integers for fields with integer constraints" do
473
- params = {'required' => '1', 'int_constraint' => '11'}
474
-
475
- @validator.validate( params )
420
+ @validator.add( :count, :integer )
421
+ @validator.validate( 'count' => '11' )
476
422
 
477
423
  @validator.should be_okay()
478
424
  @validator.should_not have_errors()
479
425
 
480
- @validator[:int_constraint].should == 11
426
+ @validator[:count].should == 11
481
427
  end
482
428
 
483
429
  it "accepts '0' for fields with integer constraints" do
484
- params = {'required' => '1', 'int_constraint' => '0'}
485
-
486
- @validator.validate( params )
430
+ @validator.add( :count, :integer )
431
+ @validator.validate( 'count' => '0' )
487
432
 
488
433
  @validator.should be_okay()
489
434
  @validator.should_not have_errors()
490
435
 
491
- @validator[:int_constraint].should == 0
436
+ @validator[:count].should == 0
492
437
  end
493
438
 
494
439
  it "accepts negative integers for fields with integer constraints" do
495
- params = {'required' => '1', 'int_constraint' => '-407'}
496
-
497
- @validator.validate( params )
440
+ @validator.add( :count, :integer )
441
+ @validator.validate( 'count' => '-407' )
498
442
 
499
443
  @validator.should be_okay()
500
444
  @validator.should_not have_errors()
501
445
 
502
- @validator[:int_constraint].should == -407
446
+ @validator[:count].should == -407
503
447
  end
504
448
 
505
449
  it "rejects non-integers for fields with integer constraints" do
506
- params = {'required' => '1', 'int_constraint' => '11.1'}
507
-
508
- @validator.validate( params )
450
+ @validator.add( :count, :integer )
451
+ @validator.validate( 'count' => '11.1' )
509
452
 
510
453
  @validator.should_not be_okay()
511
454
  @validator.should have_errors()
512
455
 
513
- @validator[:int_constraint].should be_nil()
456
+ @validator[:count].should be_nil()
514
457
  end
515
458
 
516
459
  it "rejects integer values with other cruft in them for fields with integer constraints" do
517
- params = {'required' => '1', 'int_constraint' => '88licks'}
518
-
519
- @validator.validate( params )
460
+ @validator.add( :count, :integer )
461
+ @validator.validate( 'count' => '88licks' )
520
462
 
521
463
  @validator.should_not be_okay()
522
464
  @validator.should have_errors()
523
465
 
524
- @validator[:int_constraint].should be_nil()
466
+ @validator[:count].should be_nil()
525
467
  end
526
468
 
527
469
  it "accepts simple floats for fields with float constraints" do
528
- params = {'required' => '1', 'float_constraint' => '3.14'}
529
-
530
- @validator.validate( params )
470
+ @validator.add( :amount, :float )
471
+ @validator.validate( 'amount' => '3.14' )
531
472
 
532
473
  @validator.should be_okay()
533
474
  @validator.should_not have_errors()
534
475
 
535
- @validator[:float_constraint].should == 3.14
476
+ @validator[:amount].should == 3.14
536
477
  end
537
478
 
538
479
  it "accepts negative floats for fields with float constraints" do
539
- params = {'required' => '1', 'float_constraint' => '-3.14'}
540
-
541
- @validator.validate( params )
480
+ @validator.add( :amount, :float )
481
+ @validator.validate( 'amount' => '-3.14' )
542
482
 
543
483
  @validator.should be_okay()
544
484
  @validator.should_not have_errors()
545
485
 
546
- @validator[:float_constraint].should == -3.14
486
+ @validator[:amount].should == -3.14
547
487
  end
548
488
 
549
489
  it "accepts positive floats for fields with float constraints" do
550
- params = {'required' => '1', 'float_constraint' => '+3.14'}
551
-
552
- @validator.validate( params )
490
+ @validator.add( :amount, :float )
491
+ @validator.validate( 'amount' => '+3.14' )
553
492
 
554
493
  @validator.should be_okay()
555
494
  @validator.should_not have_errors()
556
495
 
557
- @validator[:float_constraint].should == 3.14
496
+ @validator[:amount].should == 3.14
558
497
  end
559
498
 
560
499
  it "accepts floats that begin with '.' for fields with float constraints" do
561
- params = {'required' => '1', 'float_constraint' => '.1418'}
562
-
563
- @validator.validate( params )
500
+ @validator.add( :amount, :float )
501
+ @validator.validate( 'amount' => '.1418' )
564
502
 
565
503
  @validator.should be_okay()
566
504
  @validator.should_not have_errors()
567
505
 
568
- @validator[:float_constraint].should == 0.1418
506
+ @validator[:amount].should == 0.1418
569
507
  end
570
508
 
571
509
  it "accepts negative floats that begin with '.' for fields with float constraints" do
572
- params = {'required' => '1', 'float_constraint' => '-.171'}
573
-
574
- @validator.validate( params )
510
+ @validator.add( :amount, :float )
511
+ @validator.validate( 'amount' => '-.171' )
575
512
 
576
513
  @validator.should be_okay()
577
514
  @validator.should_not have_errors()
578
515
 
579
- @validator[:float_constraint].should == -0.171
516
+ @validator[:amount].should == -0.171
580
517
  end
581
518
 
582
519
  it "accepts positive floats that begin with '.' for fields with float constraints" do
583
- params = {'required' => '1', 'float_constraint' => '+.86668001'}
584
-
585
- @validator.validate( params )
520
+ @validator.add( :amount, :float )
521
+ @validator.validate( 'amount' => '+.86668001' )
586
522
 
587
523
  @validator.should be_okay()
588
524
  @validator.should_not have_errors()
589
525
 
590
- @validator[:float_constraint].should == 0.86668001
526
+ @validator[:amount].should == 0.86668001
591
527
  end
592
528
 
593
529
  it "accepts floats in exponential notation for fields with float constraints" do
594
- params = {'required' => '1', 'float_constraint' => '1756e-5'}
595
-
596
- @validator.validate( params )
530
+ @validator.add( :amount, :float )
531
+ @validator.validate( 'amount' => '1756e-5' )
597
532
 
598
533
  @validator.should be_okay()
599
534
  @validator.should_not have_errors()
600
535
 
601
- @validator[:float_constraint].should == 0.01756
536
+ @validator[:amount].should == 1756e-5
602
537
  end
603
538
 
604
539
  it "accepts negative floats in exponential notation for fields with float constraints" do
605
- params = {'required' => '1', 'float_constraint' => '-28e8'}
606
-
607
- @validator.validate( params )
540
+ @validator.add( :amount, :float )
541
+ @validator.validate( 'amount' => '-28e8' )
608
542
 
609
543
  @validator.should be_okay()
610
544
  @validator.should_not have_errors()
611
545
 
612
- @validator[:float_constraint].should == -28e8
546
+ @validator[:amount].should == -28e8
613
547
  end
614
548
 
615
549
  it "accepts floats that start with '.' in exponential notation for fields with float " +
616
550
  "constraints" do
617
- params = {'required' => '1', 'float_constraint' => '.5552e-10'}
618
-
619
- @validator.validate( params )
551
+ @validator.add( :amount, :float )
552
+ @validator.validate( 'amount' => '.5552e-10' )
620
553
 
621
554
  @validator.should be_okay()
622
555
  @validator.should_not have_errors()
623
556
 
624
- @validator[:float_constraint].should == 0.5552e-10
557
+ @validator[:amount].should == 0.5552e-10
625
558
  end
626
559
 
627
560
  it "accepts negative floats that start with '.' in exponential notation for fields with " +
628
561
  "float constraints" do
629
- params = {'required' => '1', 'float_constraint' => '-.288088e18'}
562
+ @validator.add( :amount, :float )
563
+ @validator.validate( 'amount' => '-.288088e18' )
630
564
 
631
- @validator.validate( params )
565
+ @validator.should be_okay()
566
+ @validator.should_not have_errors()
632
567
 
633
- @validator.should be_okay()
634
- @validator.should_not have_errors()
635
-
636
- @validator[:float_constraint].should == -0.288088e18
568
+ @validator[:amount].should == -0.288088e18
637
569
  end
638
570
 
639
571
  it "accepts integers for fields with float constraints" do
640
- params = {'required' => '1', 'float_constraint' => '288'}
641
-
642
- @validator.validate( params )
572
+ @validator.add( :amount, :float )
573
+ @validator.validate( 'amount' => '288' )
643
574
 
644
575
  @validator.should be_okay()
645
576
  @validator.should_not have_errors()
646
577
 
647
- @validator[:float_constraint].should == 288.0
578
+ @validator[:amount].should == 288.0
648
579
  end
649
580
 
650
581
  it "accepts negative integers for fields with float constraints" do
651
- params = {'required' => '1', 'float_constraint' => '-1606'}
652
-
653
- @validator.validate( params )
582
+ @validator.add( :amount, :float )
583
+ @validator.validate( 'amount' => '-1606' )
654
584
 
655
585
  @validator.should be_okay()
656
586
  @validator.should_not have_errors()
657
587
 
658
- @validator[:float_constraint].should == -1606.0
588
+ @validator[:amount].should == -1606.0
659
589
  end
660
590
 
661
591
  it "accepts positive integers for fields with float constraints" do
662
- params = {'required' => '1', 'float_constraint' => '+2600'}
663
-
664
- @validator.validate( params )
592
+ @validator.add( :amount, :float )
593
+ @validator.validate( 'amount' => '2600' )
665
594
 
666
595
  @validator.should be_okay()
667
596
  @validator.should_not have_errors()
668
597
 
669
- @validator[:float_constraint].should == 2600.0
598
+ @validator[:amount].should == 2600.0
670
599
  end
671
600
 
672
- it "accepts dates for fields with date constraints" do
673
- params = {'required' => '1', 'date_constraint' => '2008-11-18'}
674
601
 
675
- @validator.validate( params )
602
+ it "accepts dates for fields with date constraints" do
603
+ @validator.add( :expires, :date )
604
+ @validator.validate( 'expires' => '2008-11-18' )
676
605
 
677
606
  @validator.should be_okay()
678
607
  @validator.should_not have_errors()
679
608
 
680
- @validator[:date_constraint].should == Date.parse( '2008-11-18' )
609
+ @validator[:expires].should == Date.parse( '2008-11-18' )
681
610
  end
682
611
 
683
612
 
@@ -716,15 +645,14 @@ describe Strelka::ParamValidator do
716
645
 
717
646
  VALID_URIS.each do |uri_string|
718
647
  it "accepts #{uri_string} for fields with URI constraints" do
719
- params = {'required' => '1', 'uri_constraint' => uri_string}
720
-
721
- @validator.validate( params )
648
+ @validator.add( :homepage, :uri )
649
+ @validator.validate( 'homepage' => uri_string )
722
650
 
723
651
  @validator.should be_okay()
724
652
  @validator.should_not have_errors()
725
653
 
726
- @validator[:uri_constraint].should be_a_kind_of( URI::Generic )
727
- @validator[:uri_constraint].to_s.should == uri_string
654
+ @validator[:homepage].should be_a_kind_of( URI::Generic )
655
+ @validator[:homepage].to_s.should == uri_string
728
656
  end
729
657
  end
730
658
 
@@ -760,39 +688,34 @@ describe Strelka::ParamValidator do
760
688
 
761
689
  INVALID_URIS.each do |uri_string|
762
690
  it "rejects #{uri_string} for fields with URI constraints" do
763
- params = {'required' => '1', 'uri_constraint' => uri_string}
764
-
765
- # lambda {
766
- @validator.validate( params )
767
- # }.should_not raise_error()
691
+ @validator.add( :homepage, :uri )
692
+ @validator.validate( 'homepage' => uri_string )
768
693
 
769
694
  @validator.should_not be_okay()
770
695
  @validator.should have_errors()
771
696
 
772
- @validator[:uri_constraint].should be_nil()
697
+ @validator[:homepage].should be_nil()
773
698
  end
774
699
  end
775
700
 
776
701
  it "accepts simple RFC822 addresses for fields with email constraints" do
777
- params = {'required' => '1', 'email_constraint' => 'jrandom@hacker.ie'}
778
-
779
- @validator.validate( params )
702
+ @validator.add( :email )
703
+ @validator.validate( 'email' => 'jrandom@hacker.ie' )
780
704
 
781
705
  @validator.should be_okay()
782
706
  @validator.should_not have_errors()
783
707
 
784
- @validator[:email_constraint].should == 'jrandom@hacker.ie'
708
+ @validator[:email].should == 'jrandom@hacker.ie'
785
709
  end
786
710
 
787
711
  it "accepts hyphenated domains in RFC822 addresses for fields with email constraints" do
788
- params = {'required' => '1', 'email_constraint' => 'jrandom@just-another-hacquer.fr'}
789
-
790
- @validator.validate( params )
712
+ @validator.add( :email )
713
+ @validator.validate( 'email' => 'jrandom@just-another-hacquer.fr' )
791
714
 
792
715
  @validator.should be_okay()
793
716
  @validator.should_not have_errors()
794
717
 
795
- @validator[:email_constraint].should == 'jrandom@just-another-hacquer.fr'
718
+ @validator[:email].should == 'jrandom@just-another-hacquer.fr'
796
719
  end
797
720
 
798
721
  COMPLEX_ADDRESSES = [
@@ -803,14 +726,13 @@ describe Strelka::ParamValidator do
803
726
  ]
804
727
  COMPLEX_ADDRESSES.each do |addy|
805
728
  it "accepts #{addy} for fields with email constraints" do
806
- params = {'required' => '1', 'email_constraint' => addy}
807
-
808
- @validator.validate( params )
729
+ @validator.add( :mail, :email )
730
+ @validator.validate( 'mail' => addy )
809
731
 
810
732
  @validator.should be_okay()
811
733
  @validator.should_not have_errors()
812
734
 
813
- @validator[:email_constraint].should == addy
735
+ @validator[:mail].should == addy
814
736
  end
815
737
  end
816
738
 
@@ -824,37 +746,34 @@ describe Strelka::ParamValidator do
824
746
  ]
825
747
  BOGUS_ADDRESSES.each do |addy|
826
748
  it "rejects #{addy} for fields with email constraints" do
827
- params = {'required' => '1', 'email_constraint' => addy}
828
-
829
- @validator.validate( params )
749
+ @validator.add( :mail, :email )
750
+ @validator.validate( 'mail' => addy )
830
751
 
831
752
  @validator.should_not be_okay()
832
753
  @validator.should have_errors()
833
754
 
834
- @validator[:email_constraint].should be_nil()
755
+ @validator[:mail].should be_nil()
835
756
  end
836
757
  end
837
758
 
838
759
  it "accepts simple hosts for fields with host constraints" do
839
- params = {'required' => '1', 'host_constraint' => 'deveiate.org'}
840
-
841
- @validator.validate( params )
760
+ @validator.add( :host, :hostname )
761
+ @validator.validate( 'host' => 'deveiate.org' )
842
762
 
843
763
  @validator.should be_okay()
844
764
  @validator.should_not have_errors()
845
765
 
846
- @validator[:host_constraint].should == 'deveiate.org'
766
+ @validator[:host].should == 'deveiate.org'
847
767
  end
848
768
 
849
769
  it "accepts hyphenated hosts for fields with host constraints" do
850
- params = {'required' => '1', 'host_constraint' => 'your-characters-can-fly.kr'}
851
-
852
- @validator.validate( params )
770
+ @validator.add( :hostname )
771
+ @validator.validate( 'hostname' => 'your-characters-can-fly.kr' )
853
772
 
854
773
  @validator.should be_okay()
855
774
  @validator.should_not have_errors()
856
775
 
857
- @validator[:host_constraint].should == 'your-characters-can-fly.kr'
776
+ @validator[:hostname].should == 'your-characters-can-fly.kr'
858
777
  end
859
778
 
860
779
  BOGUS_HOSTS = [
@@ -867,60 +786,55 @@ describe Strelka::ParamValidator do
867
786
 
868
787
  BOGUS_HOSTS.each do |hostname|
869
788
  it "rejects #{hostname} for fields with host constraints" do
870
- params = {'required' => '1', 'host_constraint' => hostname}
871
-
872
- @validator.validate( params )
789
+ @validator.add( :hostname )
790
+ @validator.validate( 'hostname' => hostname )
873
791
 
874
792
  @validator.should_not be_okay()
875
793
  @validator.should have_errors()
876
794
 
877
- @validator[:host_constraint].should be_nil()
795
+ @validator[:hostname].should be_nil()
878
796
  end
879
797
  end
880
798
 
881
799
  it "accepts alpha characters for fields with alpha constraints" do
882
- params = {'required' => '1', 'alpha_constraint' => 'abelincoln'}
883
-
884
- @validator.validate( params )
800
+ @validator.add( :alpha )
801
+ @validator.validate( 'alpha' => 'abelincoln' )
885
802
 
886
803
  @validator.should be_okay()
887
804
  @validator.should_not have_errors()
888
805
 
889
- @validator[:alpha_constraint].should == 'abelincoln'
806
+ @validator[:alpha].should == 'abelincoln'
890
807
  end
891
808
 
892
809
  it "rejects non-alpha characters for fields with alpha constraints" do
893
- params = {'required' => '1', 'alpha_constraint' => 'duck45'}
894
-
895
- @validator.validate( params )
810
+ @validator.add( :alpha )
811
+ @validator.validate( 'alpha' => 'duck45' )
896
812
 
897
813
  @validator.should_not be_okay()
898
814
  @validator.should have_errors()
899
815
 
900
- @validator[:alpha_constraint].should be_nil()
816
+ @validator[:alpha].should be_nil()
901
817
  end
902
818
 
903
819
  ### 'alphanumeric'
904
820
  it "accepts alphanumeric characters for fields with alphanumeric constraints" do
905
- params = {'required' => '1', 'alphanumeric_constraint' => 'zombieabe11'}
906
-
907
- @validator.validate( params )
821
+ @validator.add( :username, :alphanumeric )
822
+ @validator.validate( 'username' => 'zombieabe11' )
908
823
 
909
824
  @validator.should be_okay()
910
825
  @validator.should_not have_errors()
911
826
 
912
- @validator[:alphanumeric_constraint].should == 'zombieabe11'
827
+ @validator[:username].should == 'zombieabe11'
913
828
  end
914
829
 
915
830
  it "rejects non-alphanumeric characters for fields with alphanumeric constraints" do
916
- params = {'required' => '1', 'alphanumeric_constraint' => 'duck!ling'}
917
-
918
- @validator.validate( params )
831
+ @validator.add( :username, :alphanumeric )
832
+ @validator.validate( 'username' => 'duck!ling' )
919
833
 
920
834
  @validator.should_not be_okay()
921
835
  @validator.should have_errors()
922
836
 
923
- @validator[:alphanumeric_constraint].should be_nil()
837
+ @validator[:username].should be_nil()
924
838
  end
925
839
 
926
840
  ### 'printable'
@@ -931,70 +845,64 @@ describe Strelka::ParamValidator do
931
845
  spider, carrying you into the aether with a humming, crackling sound.
932
846
  EOF
933
847
 
934
- params = {
935
- 'required' => '1',
936
- 'printable_constraint' => test_content
937
- }
938
-
939
- @validator.validate( params )
848
+ @validator.add( :prologue, :printable )
849
+ @validator.validate( 'prologue' => test_content )
940
850
 
941
851
  @validator.should be_okay()
942
- @validator[:printable_constraint].should == test_content
852
+ @validator[:prologue].should == test_content
943
853
  end
944
854
 
945
855
  it "rejects non-printable characters for fields with 'printable' constraints" do
946
- params = {'required' => '1', 'printable_constraint' => %{\0Something cold\0}}
947
-
948
- @validator.validate( params )
856
+ @validator.add( :prologue, :printable )
857
+ @validator.validate( 'prologue' => %{\0Something cold\0} )
949
858
 
950
859
  @validator.should_not be_okay()
951
860
  @validator.should have_errors()
952
861
 
953
- @validator[:printable_constraint].should be_nil()
862
+ @validator[:prologue].should be_nil()
954
863
  end
955
864
 
956
865
 
957
866
  it "accepts any word characters for fields with 'word' constraints" do
958
- params = {
959
- 'required' => '1',
960
- 'word_constraint' => "Собака"
961
- }
962
-
963
- @validator.validate( params )
867
+ @validator.add( :vocab_word, :word )
868
+ @validator.validate( 'vocab_word' => "Собака" )
964
869
 
965
870
  @validator.should_not have_errors()
966
871
  @validator.should be_okay()
967
872
 
968
- @validator[:word_constraint].should == params['word_constraint']
873
+ @validator[:vocab_word].should == "Собака"
969
874
  end
970
875
 
971
876
  it "accepts parameters for fields with Proc constraints if the Proc returns a true value" do
972
877
  test_date = '2007-07-17'
973
- params = {'required' => '1', 'proc_constraint' => test_date}
974
878
 
975
- @validator.validate( params )
879
+ @validator.add( :creation_date ) do |input|
880
+ Date.parse( input )
881
+ end
882
+ @validator.validate( 'creation_date' => test_date )
976
883
 
977
884
  @validator.should be_okay()
978
885
  @validator.should_not have_errors()
979
886
 
980
- @validator[:proc_constraint].should == Date.parse( test_date )
887
+ @validator[:creation_date].should == Date.parse( test_date )
981
888
  end
982
889
 
983
890
  it "rejects parameters for fields with Proc constraints if the Proc returns a false value" do
984
- params = {'required' => '1', 'proc_constraint' => %{::::}}
985
-
986
- @validator.validate( params )
891
+ @validator.add( :creation_date ) do |input|
892
+ Date.parse( input )
893
+ end
894
+ @validator.validate( 'creation_date' => '::::' )
987
895
 
988
896
  @validator.should_not be_okay()
989
897
  @validator.should have_errors()
990
898
 
991
- @validator[:proc_constraint].should be_nil()
899
+ @validator[:creation_date].should be_nil()
992
900
  end
993
901
 
994
902
  it "can be merged with another set of parameters" do
995
- params = {}
996
- @validator.validate( params )
997
- newval = @validator.merge( 'required' => '1' )
903
+ @validator.add( :foo, :integer, :required )
904
+ @validator.validate( {} )
905
+ newval = @validator.merge( 'foo' => '1' )
998
906
 
999
907
  newval.should_not equal( @validator )
1000
908
 
@@ -1003,57 +911,58 @@ describe Strelka::ParamValidator do
1003
911
  newval.should be_okay()
1004
912
  newval.should_not have_errors()
1005
913
 
1006
- @validator[:required].should == nil
1007
- newval[:required].should == '1'
914
+ @validator[:foo].should == nil
915
+ newval[:foo].should == 1
1008
916
  end
1009
917
 
1010
918
  it "can have required parameters merged into it after the initial validation" do
1011
- params = {}
1012
- @validator.validate( params )
1013
- @validator.merge!( 'required' => '1' )
919
+ @validator.add( :foo, :integer, :required )
920
+ @validator.validate( {} )
921
+ @validator.merge!( 'foo' => '1' )
1014
922
 
1015
923
  @validator.should be_okay()
1016
924
  @validator.should_not have_errors()
1017
925
 
1018
- @validator[:required].should == '1'
926
+ @validator[:foo].should == 1
1019
927
  end
1020
928
 
1021
929
  it "can have optional parameters merged into it after the initial validation" do
1022
- params = { 'required' => '1' }
1023
- @validator.validate( params )
1024
- @validator.merge!( 'optional' => 'yep.' )
930
+ @validator.add( :foom, /^\d+$/ )
931
+ @validator.validate( {} )
932
+ @validator.merge!( 'foom' => '5' )
1025
933
 
1026
934
  @validator.should be_okay()
1027
935
  @validator.should_not have_errors()
1028
936
 
1029
- @validator[:optional].should == 'yep.'
937
+ @validator[:foom].should == '5'
1030
938
  end
1031
939
 
1032
- it "rejects invalid parameters when they're merged after initial validation" do
1033
- params = { 'required' => '1', 'number' => '88' }
1034
- @validator.validate( params )
1035
- @validator.merge!( 'number' => 'buckwheat noodles' )
940
+ it "rejects invalid parameters when they're merged after initial validation" do
941
+ @validator.add( :foom, /^\d+$/ )
942
+ @validator.add( :bewm, /^\d+$/ )
943
+ @validator.validate( 'foom' => "1" )
1036
944
 
1037
- @validator.should_not be_okay()
1038
- @validator.should have_errors()
945
+ @validator.merge!( 'bewm' => 'buckwheat noodles' )
1039
946
 
1040
- @validator[:number].should be_nil()
1041
- end
947
+ @validator.should_not be_okay()
948
+ @validator.should have_errors()
949
+ @validator[:bewm].should == nil
950
+ end
1042
951
 
1043
- it "allows valid parameters to be fetched en masse" do
1044
- params = { 'required' => '1', 'number' => '88' }
1045
- @validator.validate( params )
1046
- @validator.values_at( :required, :number ).should == [ '1', '88' ]
1047
- end
952
+ it "allows valid parameters to be fetched en masse" do
953
+ @validator.add( :foom, /^\d+$/ )
954
+ @validator.add( :bewm, /^\d+$/ )
955
+ @validator.validate( 'foom' => "1", "bewm" => "2" )
956
+ @validator.values_at( :foom, :bewm ).should == [ '1', '2' ]
957
+ end
1048
958
 
1049
- it "treats ArgumentErrors in builtin constraints as validation failures" do
1050
- params = { 'required' => '1', 'number' => 'jalopy' }
1051
- @validator.validate( params )
1052
- @validator.should_not be_okay()
1053
- @validator.should have_errors()
1054
- @validator.error_messages.should == ["Invalid value for 'Number'"]
1055
- @validator[:number].should == nil
1056
- end
959
+ it "treats ArgumentErrors in builtin constraints as validation failures" do
960
+ @validator.add( :integer )
961
+ @validator.validate( 'integer' => 'jalopy' )
962
+ @validator.should_not be_okay()
963
+ @validator.should have_errors()
964
+ @validator[:integer].should be_nil()
965
+ end
1057
966
 
1058
967
  end
1059
968