dm-validations 0.9.9 → 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/History.txt +15 -0
  2. data/Manifest.txt +21 -2
  3. data/lib/dm-validations.rb +14 -14
  4. data/lib/dm-validations/absent_field_validator.rb +3 -4
  5. data/lib/dm-validations/acceptance_validator.rb +6 -10
  6. data/lib/dm-validations/confirmation_validator.rb +5 -5
  7. data/lib/dm-validations/contextual_validators.rb +1 -1
  8. data/lib/dm-validations/custom_validator.rb +1 -1
  9. data/lib/dm-validations/format_validator.rb +6 -5
  10. data/lib/dm-validations/generic_validator.rb +7 -10
  11. data/lib/dm-validations/length_validator.rb +7 -7
  12. data/lib/dm-validations/method_validator.rb +2 -2
  13. data/lib/dm-validations/numeric_validator.rb +4 -4
  14. data/lib/dm-validations/primitive_validator.rb +4 -4
  15. data/lib/dm-validations/required_field_validator.rb +5 -5
  16. data/lib/dm-validations/uniqueness_validator.rb +2 -2
  17. data/lib/dm-validations/validation_errors.rb +34 -2
  18. data/lib/dm-validations/version.rb +1 -1
  19. data/lib/dm-validations/within_validator.rb +15 -13
  20. data/spec/integration/absent_field_validator_spec.rb +4 -2
  21. data/spec/integration/acceptance_validator_spec.rb +3 -3
  22. data/spec/integration/auto_validate_spec.rb +16 -9
  23. data/spec/integration/block_validator_spec.rb +2 -8
  24. data/spec/integration/confirmation_validator_spec.rb +11 -8
  25. data/spec/integration/contextual_validators_spec.rb +2 -1
  26. data/spec/integration/format_validator_spec.rb +1 -1
  27. data/spec/integration/length_validator/error_message_spec.rb +23 -0
  28. data/spec/integration/length_validator/maximum_spec.rb +31 -0
  29. data/spec/integration/length_validator/minimum_spec.rb +31 -0
  30. data/spec/integration/length_validator/range_spec.rb +95 -0
  31. data/spec/integration/length_validator/spec_helper.rb +12 -0
  32. data/spec/integration/length_validator/valid_objects_spec.rb +13 -0
  33. data/spec/integration/method_validator_spec.rb +3 -3
  34. data/spec/integration/numeric_validator/float_type_spec.rb +102 -0
  35. data/spec/integration/numeric_validator/integer_only_true_spec.rb +92 -0
  36. data/spec/integration/numeric_validator/integer_type_spec.rb +100 -0
  37. data/spec/integration/numeric_validator/spec_helper.rb +77 -0
  38. data/spec/integration/numeric_validator_spec.rb +19 -6
  39. data/spec/integration/primitive_validator_spec.rb +2 -1
  40. data/spec/integration/required_field_validator/association_spec.rb +98 -0
  41. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +149 -0
  42. data/spec/integration/required_field_validator/date_type_value_spec.rb +126 -0
  43. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +126 -0
  44. data/spec/integration/required_field_validator/float_type_value_spec.rb +130 -0
  45. data/spec/integration/required_field_validator/integer_type_value_spec.rb +98 -0
  46. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +36 -0
  47. data/spec/integration/required_field_validator/shared_examples.rb +24 -0
  48. data/spec/integration/required_field_validator/spec_helper.rb +68 -0
  49. data/spec/integration/required_field_validator/string_type_value_spec.rb +164 -0
  50. data/spec/integration/required_field_validator/text_type_value_spec.rb +46 -0
  51. data/spec/integration/uniqueness_validator_spec.rb +10 -8
  52. data/spec/integration/validation_spec.rb +25 -25
  53. data/spec/integration/within_validator_spec.rb +36 -11
  54. data/tasks/spec.rb +1 -1
  55. metadata +24 -5
  56. data/spec/integration/length_validator_spec.rb +0 -115
  57. data/spec/integration/required_field_validator_spec.rb +0 -93
@@ -0,0 +1,31 @@
1
+ require 'pathname'
2
+ __dir__ = Pathname(__FILE__).dirname.expand_path
3
+
4
+ # global first, then local to length validators
5
+ require __dir__.parent.parent + "spec_helper"
6
+ require __dir__ + 'spec_helper'
7
+
8
+ describe DataMapper::Validate::LengthValidator do
9
+ it "lets user specify a minimum length of a string field" do
10
+ class ::MotorLaunch
11
+ validates_length :name, :min => 3
12
+ end
13
+
14
+ launch = MotorLaunch.new
15
+ launch.name = 'Ab'
16
+ launch.should_not be_valid
17
+ launch.errors.on(:name).should include('Name must be more than 3 characters long')
18
+ end
19
+
20
+ it "aliases :minimum for :min" do
21
+ class ::MotorLaunch
22
+ validators.clear!
23
+ validates_length :name, :minimum => 3
24
+ end
25
+
26
+ launch = MotorLaunch.new
27
+ launch.name = 'Ab'
28
+ launch.should_not be_valid
29
+ launch.errors.on(:name).should include('Name must be more than 3 characters long')
30
+ end
31
+ end
@@ -0,0 +1,95 @@
1
+ require 'pathname'
2
+ __dir__ = Pathname(__FILE__).dirname.expand_path
3
+
4
+ # global first, then local to length validators
5
+ require __dir__.parent.parent + "spec_helper"
6
+ require __dir__ + 'spec_helper'
7
+
8
+ class MotorLaunch
9
+ validators.clear!
10
+ validates_length :name, :in => (3..5)
11
+ end
12
+
13
+
14
+ describe MotorLaunch do
15
+ before :each do
16
+ @launch = MotorLaunch.new
17
+ end
18
+
19
+ describe "with a value that is out of range bounds (too long)" do
20
+ before :each do
21
+ @launch.name = 'a'
22
+ @launch.valid?
23
+ end
24
+
25
+ it "is invalid" do
26
+ @launch.should_not be_valid
27
+ end
28
+ it "includes range bounds and field name in error message" do
29
+ @launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')
30
+ end
31
+ end
32
+
33
+
34
+ describe "with a value that is out of range bounds (too short)" do
35
+ before :each do
36
+ @launch.name = 'L'
37
+ @launch.valid?
38
+ end
39
+
40
+ it "is invalid" do
41
+ @launch.should_not be_valid
42
+ end
43
+ it "includes range bounds and field name in error message" do
44
+ @launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')
45
+ end
46
+ end
47
+
48
+
49
+ # arguable but reasonable for 80% of cases
50
+ # to treat nil as a 0 lengh value
51
+ # reported in
52
+ # http://datamapper.lighthouseapp.com/projects/20609/tickets/646
53
+ describe "with a nil value" do
54
+ before :each do
55
+ @launch.name = nil
56
+ @launch.valid?
57
+ end
58
+
59
+ it "is invalid" do
60
+ @launch.should_not be_valid
61
+ end
62
+ it "includes range bounds and field name in error message" do
63
+ @launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')
64
+ end
65
+ end
66
+
67
+
68
+
69
+ describe "with a value that is within range bounds" do
70
+ before :each do
71
+ @launch.name = 'Lisp'
72
+ @launch.valid?
73
+ end
74
+
75
+ it "is valid" do
76
+ @launch.should be_valid
77
+ end
78
+ it "has blank error message" do
79
+ @launch.errors.on(:name).should be_blank
80
+ end
81
+ end
82
+
83
+
84
+
85
+ it "aliases :within for :in" do
86
+ class ::MotorLaunch
87
+ validators.clear!
88
+ validates_length :name, :within => (3..5)
89
+ end
90
+
91
+ launch = MotorLaunch.new
92
+ launch.name = 'Ride'
93
+ launch.valid?.should == true
94
+ end
95
+ end
@@ -0,0 +1,12 @@
1
+ class MotorLaunch
2
+ include DataMapper::Resource
3
+ property :id, Integer, :serial => true
4
+ property :name, String, :auto_validation => false
5
+ end
6
+
7
+ class BoatDock
8
+ include DataMapper::Resource
9
+ property :id, Integer, :serial => true
10
+ property :name, String, :auto_validation => false, :default => "I'm a long string"
11
+ validates_length :name, :min => 3
12
+ end
@@ -0,0 +1,13 @@
1
+ require 'pathname'
2
+ __dir__ = Pathname(__FILE__).dirname.expand_path
3
+
4
+ # global first, then local to length validators
5
+ require __dir__.parent.parent + "spec_helper"
6
+ require __dir__ + 'spec_helper'
7
+
8
+ describe DataMapper::Validate::LengthValidator do
9
+ it "passes if a default fulfills the requirements" do
10
+ doc = BoatDock.new
11
+ doc.should be_valid
12
+ end
13
+ end
@@ -3,7 +3,7 @@ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
3
 
4
4
  describe DataMapper::Validate::MethodValidator do
5
5
  before(:all) do
6
- class Ship
6
+ class ::Ship
7
7
  include DataMapper::Resource
8
8
  property :id, Integer, :key => true
9
9
  property :name, String
@@ -41,13 +41,13 @@ describe DataMapper::Validate::MethodValidator do
41
41
  Ship.new.valid_for_testing_success?.should == true
42
42
  ship = Ship.new
43
43
  ship.valid_for_testing_failure?.should == false
44
- ship.errors.full_messages.include?('Validation failed').should == true
44
+ ship.errors.on(:fail_validation).should include('Validation failed')
45
45
  end
46
46
 
47
47
  it "should run multiple validation methods" do
48
48
  ship = Ship.new
49
49
  ship.valid_for_multiple_validations?.should == false
50
- ship.errors.full_messages.should include('Second Validation was false')
50
+ ship.errors.on(:second_validation).should include('Second Validation was false')
51
51
  end
52
52
 
53
53
  it "should validate via a method and add error to field" do
@@ -0,0 +1,102 @@
1
+ require 'pathname'
2
+ __dir__ = Pathname(__FILE__).dirname.expand_path
3
+
4
+ require __dir__.parent.parent + 'spec_helper'
5
+ require __dir__ + 'spec_helper'
6
+
7
+ describe BasketballPlayer do
8
+ before(:each) do
9
+ @mj = BasketballPlayer.new(:name => "Michael Jordan", :height => 198.1, :weight => 97.2)
10
+ end
11
+
12
+ describe "with height as float" do
13
+ before(:each) do
14
+ # no op in this case
15
+ end
16
+
17
+ it "is valid" do
18
+ @mj.should be_valid
19
+ end
20
+ end
21
+
22
+
23
+ describe "with height as integer" do
24
+ before(:each) do
25
+ @mj.height = 198
26
+ end
27
+
28
+ it "is valid" do
29
+ @mj.should be_valid
30
+ end
31
+ end
32
+
33
+
34
+ describe "with height as string containing only integers" do
35
+ before(:each) do
36
+ @mj.height = "198"
37
+ end
38
+
39
+ it "is valid" do
40
+ @mj.should be_valid
41
+ end
42
+ end
43
+
44
+
45
+ describe "with height as string containing a float" do
46
+ before(:each) do
47
+ @mj.height = "198.1"
48
+ end
49
+
50
+ it "is valid" do
51
+ @mj.should be_valid
52
+ end
53
+ end
54
+
55
+
56
+ describe "with height as string containing random alphanumeric characters" do
57
+ before(:each) do
58
+ @mj.height = "height=198.1"
59
+ end
60
+
61
+ it "is set to 0.0" do
62
+ @mj.height.should == 0.0
63
+ end
64
+
65
+ it "IS valid" do
66
+ # float property is set to 0.0 here
67
+ @mj.should be_valid
68
+ end
69
+ end
70
+
71
+
72
+ describe "with height as string containing random punctuation characters" do
73
+ before(:each) do
74
+ @mj.height = "$$ * $?"
75
+ end
76
+
77
+ it "is set to 0.0" do
78
+ @mj.height.should == 0.0
79
+ end
80
+
81
+ it "IS valid" do
82
+ # float property is set to 0.0 here
83
+ @mj.should be_valid
84
+ end
85
+ end
86
+
87
+
88
+ describe "with nil height" do
89
+ before(:each) do
90
+ @mj.height = nil
91
+ end
92
+
93
+ it "is NOT valid" do
94
+ @mj.should_not be_valid
95
+ end
96
+
97
+ it "has a meaningful error message on for the property" do
98
+ @mj.valid?
99
+ @mj.errors.on(:height).should include("Height must be a number")
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,92 @@
1
+ require 'pathname'
2
+ __dir__ = Pathname(__FILE__).dirname.expand_path
3
+
4
+ require __dir__.parent.parent + 'spec_helper'
5
+ require __dir__ + 'spec_helper'
6
+
7
+ describe Country do
8
+ before(:each) do
9
+ @country = Country.new(:name => "Italy", :area => "301318")
10
+ end
11
+
12
+ describe "with area as integer" do
13
+ before(:each) do
14
+ # no op in this case
15
+ end
16
+
17
+ it "is valid" do
18
+ @country.should be_valid
19
+ end
20
+ end
21
+
22
+
23
+ describe "with area as integer" do
24
+ before(:each) do
25
+ @country.area = 1603
26
+ end
27
+
28
+ it "is valid" do
29
+ @country.should be_valid
30
+ end
31
+ end
32
+
33
+
34
+ describe "with area as string containing only integers" do
35
+ before(:each) do
36
+ @country.area = "301318"
37
+ end
38
+
39
+ it "is valid" do
40
+ @country.should be_valid
41
+ end
42
+ end
43
+
44
+
45
+ describe "with area as string containing a float" do
46
+ before(:each) do
47
+ @country.area = "301318.6"
48
+ end
49
+
50
+ it "IS valid" do
51
+ @country.should be_valid
52
+ end
53
+ end
54
+
55
+
56
+ describe "with area as string containing random alphanumeric characters" do
57
+ before(:each) do
58
+ @country.area = "area=51"
59
+ end
60
+
61
+ it "IS NOT valid" do
62
+ @country.should_not be_valid
63
+ end
64
+ end
65
+
66
+
67
+ describe "with area as string containing random punctuation characters" do
68
+ before(:each) do
69
+ @country.area = "$$ * $?"
70
+ end
71
+
72
+ it "IS NOT valid" do
73
+ @country.should_not be_valid
74
+ end
75
+ end
76
+
77
+
78
+ describe "with unknown area" do
79
+ before(:each) do
80
+ @country.area = nil
81
+ end
82
+
83
+ it "is NOT valid" do
84
+ @country.should_not be_valid
85
+ end
86
+
87
+ it "has a meaningful error message on for the property" do
88
+ @country.valid?
89
+ @country.errors.on(:area).should include("Please use integers to specify area")
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,100 @@
1
+ require 'pathname'
2
+ __dir__ = Pathname(__FILE__).dirname.expand_path
3
+
4
+ require __dir__.parent.parent + 'spec_helper'
5
+ require __dir__ + 'spec_helper'
6
+
7
+ describe City do
8
+ before(:each) do
9
+ @city = City.new(:name => "Tokyo", :founded_in => 1603)
10
+ end
11
+
12
+ describe "with foundation year as integer" do
13
+ before(:each) do
14
+ # no op in this case
15
+ end
16
+
17
+ it "is valid" do
18
+ @city.should be_valid
19
+ end
20
+ end
21
+
22
+
23
+ describe "with foundation year as integer" do
24
+ before(:each) do
25
+ @city.founded_in = 1603
26
+ end
27
+
28
+ it "is valid" do
29
+ @city.should be_valid
30
+ end
31
+ end
32
+
33
+
34
+ describe "with foundation year as string containing only integers" do
35
+ before(:each) do
36
+ @city.founded_in = "1603"
37
+ end
38
+
39
+ it "is valid" do
40
+ @city.should be_valid
41
+ end
42
+ end
43
+
44
+
45
+ describe "with foundation year as string containing a float" do
46
+ before(:each) do
47
+ @city.founded_in = "1603.6"
48
+ end
49
+
50
+ it "is valid" do
51
+ @city.should be_valid
52
+ end
53
+ end
54
+
55
+
56
+ describe "with foundation year as string containing random alphanumeric characters" do
57
+ before(:each) do
58
+ @city.founded_in = "founded-in=1603"
59
+ end
60
+
61
+ it "is set to nil" do
62
+ @city.founded_in.should be(nil)
63
+ end
64
+
65
+ it "IS NOT valid" do
66
+ @city.should_not be_valid
67
+ end
68
+ end
69
+
70
+
71
+ describe "with foundation year as string containing random punctuation characters" do
72
+ before(:each) do
73
+ @city.founded_in = "$$ * $?"
74
+ end
75
+
76
+ it "is set to nil" do
77
+ @city.founded_in.should be(nil)
78
+ end
79
+
80
+ it "IS NOT valid" do
81
+ @city.should_not be_valid
82
+ end
83
+ end
84
+
85
+
86
+ describe "with unknown foundation date" do
87
+ before(:each) do
88
+ @city.founded_in = nil
89
+ end
90
+
91
+ it "is NOT valid" do
92
+ @city.should_not be_valid
93
+ end
94
+
95
+ it "has a meaningful error message on for the property" do
96
+ @city.valid?
97
+ @city.errors.on(:founded_in).should include("Foundation year must be an integer")
98
+ end
99
+ end
100
+ end