dm-validations 1.0.2 → 1.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/Gemfile +25 -94
  2. data/LICENSE +1 -1
  3. data/Rakefile +2 -7
  4. data/VERSION +1 -1
  5. data/dm-validations.gemspec +281 -270
  6. data/lib/dm-validations.rb +36 -35
  7. data/lib/dm-validations/auto_validate.rb +10 -5
  8. data/lib/dm-validations/contextual_validators.rb +41 -4
  9. data/lib/dm-validations/formats/email.rb +12 -8
  10. data/lib/dm-validations/validation_errors.rb +4 -0
  11. data/lib/dm-validations/validators/acceptance_validator.rb +4 -0
  12. data/lib/dm-validations/validators/format_validator.rb +3 -3
  13. data/lib/dm-validations/validators/generic_validator.rb +8 -20
  14. data/lib/dm-validations/validators/length_validator.rb +7 -1
  15. data/lib/dm-validations/validators/uniqueness_validator.rb +8 -2
  16. data/spec/fixtures/mittelschnauzer.rb +1 -0
  17. data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +20 -24
  18. data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +11 -3
  19. data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +9 -14
  20. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +9 -0
  21. data/spec/integration/automatic_validation/inferred_uniqueness_validation_spec.rb +48 -0
  22. data/spec/integration/automatic_validation/spec_helper.rb +2 -19
  23. data/spec/integration/format_validator/email_format_validator_spec.rb +15 -1
  24. data/spec/unit/generic_validator/optional_spec.rb +54 -0
  25. data/spec/unit/validation_errors/respond_to_spec.rb +15 -0
  26. data/tasks/spec.rake +0 -3
  27. metadata +69 -42
  28. data/.gitignore +0 -37
  29. data/tasks/ci.rake +0 -1
  30. data/tasks/local_gemfile.rake +0 -16
  31. data/tasks/metrics.rake +0 -36
@@ -12,7 +12,7 @@ require 'integration/automatic_validation/spec_helper'
12
12
 
13
13
  describe "with an integer value" do
14
14
  before :all do
15
- @model.set(column => 1)
15
+ @model.attributes = {column => 1}
16
16
  end
17
17
 
18
18
  it_should_behave_like "valid model"
@@ -20,7 +20,7 @@ require 'integration/automatic_validation/spec_helper'
20
20
 
21
21
  describe "with a float value" do
22
22
  before :all do
23
- @model.set(column => 1.0)
23
+ @model.attributes = {column => 1.0}
24
24
  end
25
25
 
26
26
  it_should_behave_like "valid model"
@@ -28,10 +28,18 @@ require 'integration/automatic_validation/spec_helper'
28
28
 
29
29
  describe "with a BigDecimal value" do
30
30
  before :all do
31
- @model.set(column => BigDecimal('1'))
31
+ @model.attributes = {column => BigDecimal('1')}
32
32
  end
33
33
 
34
34
  it_should_behave_like "valid model"
35
35
  end
36
+
37
+ describe "with an uncoercible value" do
38
+ before :all do
39
+ @model.attributes = {column => "foo"}
40
+ end
41
+
42
+ it_should_behave_like "invalid model"
43
+ end
36
44
  end
37
45
  end
@@ -8,31 +8,26 @@ describe "A model with an Integer property" do
8
8
  @model = SailBoat.new
9
9
  end
10
10
 
11
+ # success case
11
12
  describe "assigned to an integer" do
12
13
  before :all do
13
- @model.set(:id => 1)
14
+ @model.id = 1
14
15
  end
15
16
 
16
17
  it_should_behave_like "valid model"
17
18
  end
18
19
 
19
- describe "assigned to a float" do
20
+ describe "assigned a value coercible into an integer" do
20
21
  before :all do
21
- @model.set(:id => 1.0)
22
+ @model.id = 1.0
22
23
  end
23
24
 
24
- it "is invalid" do
25
- @model.should_not be_valid
26
- end
27
-
28
- it "has a meaningful default error message" do
29
- @model.errors.on(:id).should == [ 'Id must be an integer' ]
30
- end
25
+ it_should_behave_like "valid model"
31
26
  end
32
27
 
33
- describe "assigned to a BigDecimal" do
28
+ describe "assigned a value not coercible into an integer" do
34
29
  before :all do
35
- @model.set(:id => BigDecimal('1'))
30
+ @model.id = "foo"
36
31
  end
37
32
 
38
33
  it "is invalid" do
@@ -46,7 +41,7 @@ describe "A model with an Integer property" do
46
41
 
47
42
  describe "assigned to a too-small integer" do
48
43
  before :all do
49
- @model.set(:id => 0)
44
+ @model.id = 0
50
45
  end
51
46
 
52
47
  it "is invalid" do
@@ -60,7 +55,7 @@ describe "A model with an Integer property" do
60
55
 
61
56
  describe "assigned to a too-large integer" do
62
57
  before :all do
63
- @model.set(:id => 11)
58
+ @model.id = 11
64
59
  end
65
60
 
66
61
  it "is invalid" do
@@ -122,4 +122,13 @@ describe 'DataMapper::Validations::Fixtures::SmsMessage' do
122
122
  @model.errors.on(:body).should == [ 'Body must be between 1 and 500 characters long' ]
123
123
  end
124
124
  end
125
+
126
+ describe 'with an infinitely long note' do
127
+ it "should raise when trying to set the upper bound of a property length range to Infinity" do
128
+ expected_msg = "Infinity is no valid upper bound for a length range"
129
+ lambda {
130
+ @model.class.property :body, String, :length => (1..1.0/0)
131
+ }.should raise_error(ArgumentError, expected_msg)
132
+ end
133
+ end
125
134
  end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'integration/automatic_validation/spec_helper'
3
+
4
+ describe 'uniqueness' do
5
+ describe 'single column' do
6
+ before :all do
7
+ @klass = Class.new do
8
+ include DataMapper::Resource
9
+
10
+ storage_names[:default] = 'unique_events_single'
11
+
12
+ property :id, Integer, :key => true
13
+ property :start_year, Integer, :unique => true
14
+ end
15
+ @klass.auto_migrate!
16
+
17
+ @existing = @klass.create(:id => 1, :start_year => 2008)
18
+ @new = @klass.new(:id => 2, :start_year => 2008)
19
+ end
20
+
21
+ it 'validates' do
22
+ @new.should_not be_valid
23
+ end
24
+ end
25
+
26
+ describe 'multiple columns' do
27
+ before :all do
28
+ @klass = Class.new do
29
+ include DataMapper::Resource
30
+
31
+ storage_names[:default] = 'unique_events_multiple'
32
+
33
+ property :id, Integer, :key => true
34
+ property :start_year, Integer, :unique => :years
35
+ property :stop_year, Integer, :unique => :years
36
+ end
37
+ @klass.auto_migrate!
38
+
39
+ @new = @klass.new(:id => 1, :start_year => 2008, :stop_year => 2009)
40
+ end
41
+
42
+ it 'validates uniquness' do
43
+ lambda {
44
+ @new.should_not be_valid
45
+ }.should raise_error(ArgumentError)
46
+ end
47
+ end
48
+ end
@@ -10,15 +10,6 @@
10
10
  # and use a few more realistic models with ParanoidBoolean and all
11
11
  # that
12
12
 
13
- module TypecastBypassSetter
14
- # Bypass typecasting so we can set values for specs
15
- def set(attributes)
16
- attributes.each do |k, v|
17
- instance_variable_set("@#{k}", v)
18
- end
19
- end
20
- end
21
-
22
13
  class SailBoat
23
14
  include DataMapper::Resource
24
15
 
@@ -36,8 +27,6 @@ class SailBoat
36
27
  property :build_date, Date, :validates => :primitive_test
37
28
  property :float, Float, :precision => 2, :scale => 1
38
29
  property :big_decimal, Decimal, :precision => 2, :scale => 1
39
-
40
- include TypecastBypassSetter
41
30
  end
42
31
 
43
32
  class HasNullableBoolean
@@ -47,28 +36,22 @@ class HasNullableBoolean
47
36
  # use Serial in real world apps
48
37
  property :id, Integer, :key => true
49
38
  property :bool, Boolean # :required => false by default
50
-
51
- include TypecastBypassSetter
52
39
  end
53
40
 
54
- class HasNotNullableBoolean
41
+ class HasRequiredBoolean
55
42
  include DataMapper::Resource
56
43
 
57
44
  # this one is not Serial intentionally
58
45
  # use Serial in real world apps
59
46
  property :id, Integer, :key => true
60
47
  property :bool, Boolean, :required => true
61
-
62
- include TypecastBypassSetter
63
48
  end
64
49
 
65
- class HasNotNullableParanoidBoolean
50
+ class HasRequiredParanoidBoolean
66
51
  include DataMapper::Resource
67
52
 
68
53
  # this one is not Serial intentionally
69
54
  # use Serial in real world apps
70
55
  property :id, Integer, :key => true
71
56
  property :bool, ParanoidBoolean, :required => true
72
-
73
- include TypecastBypassSetter
74
57
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'spec_helper'
2
3
  require 'integration/format_validator/spec_helper'
3
4
 
@@ -15,7 +16,6 @@ describe 'DataMapper::Validations::Fixtures::BillOfLading' do
15
16
  '{_dave_}@example.com',
16
17
  '"[[ dave ]]"@example.com',
17
18
  'dave."dave"@example.com',
18
- 'test@localhost',
19
19
  'test@example.com',
20
20
  'test@example.co.uk',
21
21
  'test@example.com.br',
@@ -40,6 +40,7 @@ describe 'DataMapper::Validations::Fixtures::BillOfLading' do
40
40
  '.dave@example.com',
41
41
  'Max@Job 3:14',
42
42
  'Job@Book of Job',
43
+ 'test@localhost',
43
44
  'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com'].each do |email|
44
45
  describe "with email value of #{email} (non RFC2822 compliant)" do
45
46
  before :all do
@@ -50,6 +51,19 @@ describe 'DataMapper::Validations::Fixtures::BillOfLading' do
50
51
  end
51
52
  end
52
53
 
54
+ describe "with valid email including unicode characters" do
55
+ before :all do
56
+ @model = DataMapper::Validations::Fixtures::BillOfLading.new(valid_attributes.merge(:email => 'pelé@gmail.com'))
57
+ end
58
+
59
+ # Unicode emails not supported on MRI18
60
+ unless !defined?(RUBY_ENGINE) && RUBY_VERSION == '1.8.7'
61
+ it 'should behave like valid model' do
62
+ @model.should be_valid
63
+ end
64
+ end
65
+ end
66
+
53
67
 
54
68
  it 'should have a pre-defined URL format' do
55
69
  bad = [ 'http:// example.com',
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'DataMapper::Validations::GenericValidator', '#optional?' do
4
+ def validator(opts = {})
5
+ DataMapper::Validations::LengthValidator.new(:name, opts)
6
+ end
7
+
8
+ describe 'allowing blank' do
9
+ subject do
10
+ validator(
11
+ :allow_blank => true
12
+ )
13
+ end
14
+
15
+ it { subject.optional?("" ).should be }
16
+ it { subject.optional?(nil).should be }
17
+ end
18
+
19
+ describe 'allowing nil' do
20
+ subject do
21
+ validator(
22
+ :allow_nil => true
23
+ )
24
+ end
25
+
26
+ it { subject.optional?("" ).should_not be }
27
+ it { subject.optional?(nil).should be }
28
+ end
29
+
30
+ describe 'allowing blank, but now allowing nil' do
31
+ subject do
32
+ validator(
33
+ :allow_blank => true,
34
+ :allow_nil => false
35
+ )
36
+ end
37
+
38
+ it { subject.optional?("" ).should be }
39
+ it { subject.optional?(nil).should_not be }
40
+ end
41
+
42
+ describe 'allowing nil, but now allowing blank' do
43
+ subject do
44
+ validator(
45
+ :allow_blank => false,
46
+ :allow_nil => true
47
+ )
48
+ end
49
+
50
+ it { subject.optional?("" ).should_not be }
51
+ it { subject.optional?(nil).should be }
52
+ end
53
+
54
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'DataMapper::Validations::ValidationErrors#respond_to?' do
4
+
5
+ subject { DataMapper::Validations::ValidationErrors.new(Object.new) }
6
+
7
+ it 'should look for the method in self' do
8
+ subject.should respond_to(:full_messages)
9
+ end
10
+
11
+ it 'should delegate lookup to the underlying errors hash' do
12
+ subject.should respond_to(:size)
13
+ end
14
+
15
+ end
data/tasks/spec.rake CHANGED
@@ -35,7 +35,4 @@ rescue LoadError
35
35
  end
36
36
  end
37
37
 
38
- task :spec => :check_dependencies
39
- task :rcov => :check_dependencies
40
-
41
38
  task :default => :spec
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-validations
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 2
9
- version: 1.0.2
4
+ prerelease: 6
5
+ version: 1.1.0.rc1
10
6
  platform: ruby
11
7
  authors:
12
8
  - Guy van den Berg
@@ -14,53 +10,86 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-09-07 00:00:00 -07:00
13
+ date: 2011-02-28 00:00:00 -08:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
- name: dm-core
22
- prerelease: false
17
+ name: activesupport
23
18
  requirement: &id001 !ruby/object:Gem::Requirement
24
19
  none: false
25
20
  requirements:
26
21
  - - ~>
27
22
  - !ruby/object:Gem::Version
28
- segments:
29
- - 1
30
- - 0
31
- - 2
32
- version: 1.0.2
23
+ version: 3.0.4
33
24
  type: :runtime
25
+ prerelease: false
34
26
  version_requirements: *id001
35
27
  - !ruby/object:Gem::Dependency
36
- name: rspec
37
- prerelease: false
28
+ name: i18n
38
29
  requirement: &id002 !ruby/object:Gem::Requirement
39
30
  none: false
40
31
  requirements:
41
32
  - - ~>
42
33
  - !ruby/object:Gem::Version
43
- segments:
44
- - 1
45
- - 3
46
- version: "1.3"
47
- type: :development
34
+ version: 0.5.0
35
+ type: :runtime
36
+ prerelease: false
48
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: dm-core
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.0.rc1
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: dm-types
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.1.0.rc1
57
+ type: :development
51
58
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: jeweler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
53
63
  none: false
54
64
  requirements:
55
65
  - - ~>
56
66
  - !ruby/object:Gem::Version
57
- segments:
58
- - 1
59
- - 0
60
- - 2
61
- version: 1.0.2
67
+ version: 1.5.2
62
68
  type: :development
63
- version_requirements: *id003
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: rake
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 0.8.7
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rspec
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
64
93
  description: Library for performing validations on DM models and pure Ruby object
65
94
  email: vandenberg.guy [a] gmail [d] com
66
95
  executables: []
@@ -71,7 +100,6 @@ extra_rdoc_files:
71
100
  - LICENSE
72
101
  - README.rdoc
73
102
  files:
74
- - .gitignore
75
103
  - Gemfile
76
104
  - LICENSE
77
105
  - README.rdoc
@@ -142,6 +170,7 @@ files:
142
170
  - spec/integration/automatic_validation/inferred_length_validation_spec.rb
143
171
  - spec/integration/automatic_validation/inferred_presence_validation_spec.rb
144
172
  - spec/integration/automatic_validation/inferred_primitive_validation_spec.rb
173
+ - spec/integration/automatic_validation/inferred_uniqueness_validation_spec.rb
145
174
  - spec/integration/automatic_validation/inferred_within_validation_spec.rb
146
175
  - spec/integration/automatic_validation/spec_helper.rb
147
176
  - spec/integration/block_validator/block_validator_spec.rb
@@ -206,23 +235,22 @@ files:
206
235
  - spec/unit/contextual_validators/execution_spec.rb
207
236
  - spec/unit/contextual_validators/spec_helper.rb
208
237
  - spec/unit/generic_validator/equality_operator_spec.rb
238
+ - spec/unit/generic_validator/optional_spec.rb
209
239
  - spec/unit/validation_errors/adding_spec.rb
210
240
  - spec/unit/validation_errors/emptiness_spec.rb
211
241
  - spec/unit/validation_errors/enumerable_spec.rb
212
242
  - spec/unit/validation_errors/reading_spec.rb
213
- - tasks/ci.rake
214
- - tasks/local_gemfile.rake
215
- - tasks/metrics.rake
243
+ - spec/unit/validation_errors/respond_to_spec.rb
216
244
  - tasks/spec.rake
217
245
  - tasks/yard.rake
218
246
  - tasks/yardstick.rake
219
- has_rdoc: yard
247
+ has_rdoc: true
220
248
  homepage: http://github.com/datamapper/dm-validations
221
249
  licenses: []
222
250
 
223
251
  post_install_message:
224
- rdoc_options:
225
- - --charset=UTF-8
252
+ rdoc_options: []
253
+
226
254
  require_paths:
227
255
  - lib
228
256
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -230,21 +258,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
230
258
  requirements:
231
259
  - - ">="
232
260
  - !ruby/object:Gem::Version
233
- segments:
234
- - 0
235
261
  version: "0"
236
262
  required_rubygems_version: !ruby/object:Gem::Requirement
237
263
  none: false
238
264
  requirements:
239
- - - ">="
265
+ - - ">"
240
266
  - !ruby/object:Gem::Version
241
- segments:
242
- - 0
243
- version: "0"
267
+ version: 1.3.1
244
268
  requirements: []
245
269
 
246
270
  rubyforge_project: datamapper
247
- rubygems_version: 1.3.7
271
+ rubygems_version: 1.5.2
248
272
  signing_key:
249
273
  specification_version: 3
250
274
  summary: Library for performing validations on DM models and pure Ruby object
@@ -291,6 +315,7 @@ test_files:
291
315
  - spec/integration/automatic_validation/inferred_length_validation_spec.rb
292
316
  - spec/integration/automatic_validation/inferred_presence_validation_spec.rb
293
317
  - spec/integration/automatic_validation/inferred_primitive_validation_spec.rb
318
+ - spec/integration/automatic_validation/inferred_uniqueness_validation_spec.rb
294
319
  - spec/integration/automatic_validation/inferred_within_validation_spec.rb
295
320
  - spec/integration/automatic_validation/spec_helper.rb
296
321
  - spec/integration/block_validator/block_validator_spec.rb
@@ -353,7 +378,9 @@ test_files:
353
378
  - spec/unit/contextual_validators/execution_spec.rb
354
379
  - spec/unit/contextual_validators/spec_helper.rb
355
380
  - spec/unit/generic_validator/equality_operator_spec.rb
381
+ - spec/unit/generic_validator/optional_spec.rb
356
382
  - spec/unit/validation_errors/adding_spec.rb
357
383
  - spec/unit/validation_errors/emptiness_spec.rb
358
384
  - spec/unit/validation_errors/enumerable_spec.rb
359
385
  - spec/unit/validation_errors/reading_spec.rb
386
+ - spec/unit/validation_errors/respond_to_spec.rb