dm-validations 0.9.11 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. data/{History.txt → History.rdoc} +10 -3
  2. data/LICENSE +1 -0
  3. data/Manifest.txt +103 -33
  4. data/README.rdoc +172 -0
  5. data/Rakefile +4 -5
  6. data/lib/dm-validations.rb +73 -75
  7. data/lib/dm-validations/auto_validate.rb +98 -56
  8. data/lib/dm-validations/contextual_validators.rb +37 -14
  9. data/lib/dm-validations/exceptions.rb +3 -0
  10. data/lib/dm-validations/formats/url.rb +3 -1
  11. data/lib/dm-validations/support/context.rb +54 -0
  12. data/lib/dm-validations/validation_errors.rb +51 -27
  13. data/lib/dm-validations/{absent_field_validator.rb → validators/absent_field_validator.rb} +2 -8
  14. data/lib/dm-validations/{acceptance_validator.rb → validators/acceptance_validator.rb} +7 -7
  15. data/lib/dm-validations/{block_validator.rb → validators/block_validator.rb} +0 -0
  16. data/lib/dm-validations/{confirmation_validator.rb → validators/confirmation_validator.rb} +7 -7
  17. data/lib/dm-validations/{format_validator.rb → validators/format_validator.rb} +10 -16
  18. data/lib/dm-validations/{generic_validator.rb → validators/generic_validator.rb} +28 -6
  19. data/lib/dm-validations/validators/length_validator.rb +214 -0
  20. data/lib/dm-validations/validators/method_validator.rb +68 -0
  21. data/lib/dm-validations/validators/numeric_validator.rb +171 -0
  22. data/lib/dm-validations/{primitive_validator.rb → validators/primitive_validator.rb} +2 -7
  23. data/lib/dm-validations/{required_field_validator.rb → validators/required_field_validator.rb} +0 -6
  24. data/lib/dm-validations/{uniqueness_validator.rb → validators/uniqueness_validator.rb} +9 -20
  25. data/lib/dm-validations/{within_validator.rb → validators/within_validator.rb} +4 -4
  26. data/lib/dm-validations/version.rb +1 -1
  27. data/spec/fixtures/barcode.rb +32 -0
  28. data/spec/fixtures/basketball_court.rb +58 -0
  29. data/spec/fixtures/basketball_player.rb +37 -0
  30. data/spec/fixtures/beta_tester_account.rb +33 -0
  31. data/spec/fixtures/bill_of_landing.rb +42 -0
  32. data/spec/fixtures/boat_dock.rb +26 -0
  33. data/spec/fixtures/city.rb +25 -0
  34. data/spec/fixtures/company.rb +95 -0
  35. data/spec/fixtures/corporate_world.rb +47 -0
  36. data/spec/fixtures/country.rb +25 -0
  37. data/spec/fixtures/currency.rb +42 -0
  38. data/spec/fixtures/ethernet_frame.rb +56 -0
  39. data/spec/fixtures/event.rb +44 -0
  40. data/spec/fixtures/g3_concert.rb +57 -0
  41. data/spec/fixtures/jabberwock.rb +27 -0
  42. data/spec/fixtures/kayak.rb +28 -0
  43. data/spec/fixtures/lernean_hydra.rb +39 -0
  44. data/spec/fixtures/mathematical_function.rb +34 -0
  45. data/spec/fixtures/memory_object.rb +30 -0
  46. data/spec/fixtures/mittelschnauzer.rb +35 -0
  47. data/spec/fixtures/motor_launch.rb +21 -0
  48. data/spec/fixtures/page.rb +32 -0
  49. data/spec/fixtures/phone_number.rb +28 -0
  50. data/spec/fixtures/pirogue.rb +28 -0
  51. data/spec/fixtures/programming_language.rb +83 -0
  52. data/spec/fixtures/reservation.rb +38 -0
  53. data/spec/fixtures/scm_operation.rb +67 -0
  54. data/spec/fixtures/sms_message.rb +22 -0
  55. data/spec/fixtures/udp_packet.rb +49 -0
  56. data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +86 -0
  57. data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
  58. data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
  59. data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
  60. data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +47 -0
  61. data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +41 -0
  62. data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +104 -0
  63. data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +35 -0
  64. data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +33 -0
  65. data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +73 -0
  66. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +121 -0
  67. data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +41 -0
  68. data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +20 -0
  69. data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +33 -0
  70. data/spec/integration/automatic_validation/spec_helper.rb +74 -0
  71. data/spec/integration/block_validator/block_validator_spec.rb +32 -0
  72. data/spec/integration/block_validator/spec_helper.rb +5 -0
  73. data/spec/integration/conditional_validation/if_condition_spec.rb +61 -0
  74. data/spec/integration/conditional_validation/spec_helper.rb +5 -0
  75. data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +74 -0
  76. data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
  77. data/spec/integration/datamapper_models/association_validation_spec.rb +23 -0
  78. data/spec/integration/datamapper_models/inheritance_spec.rb +78 -0
  79. data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +22 -0
  80. data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
  81. data/spec/integration/format_validator/email_format_validator_spec.rb +113 -0
  82. data/spec/integration/format_validator/format_validator_spec.rb +60 -0
  83. data/spec/integration/format_validator/regexp_validator_spec.rb +29 -0
  84. data/spec/integration/format_validator/spec_helper.rb +5 -0
  85. data/spec/integration/format_validator/url_format_validator_spec.rb +63 -0
  86. data/spec/integration/length_validator/default_value_spec.rb +12 -0
  87. data/spec/integration/length_validator/equality_spec.rb +79 -0
  88. data/spec/integration/length_validator/error_message_spec.rb +15 -18
  89. data/spec/integration/length_validator/maximum_spec.rb +40 -24
  90. data/spec/integration/length_validator/minimum_spec.rb +32 -21
  91. data/spec/integration/length_validator/range_spec.rb +50 -60
  92. data/spec/integration/length_validator/spec_helper.rb +7 -12
  93. data/spec/integration/method_validator/method_validator_spec.rb +239 -0
  94. data/spec/integration/method_validator/spec_helper.rb +5 -0
  95. data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +61 -0
  96. data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +37 -0
  97. data/spec/integration/numeric_validator/float_type_spec.rb +42 -56
  98. data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +35 -0
  99. data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +35 -0
  100. data/spec/integration/numeric_validator/integer_only_true_spec.rb +3 -6
  101. data/spec/integration/numeric_validator/integer_type_spec.rb +8 -24
  102. data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +35 -0
  103. data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +35 -0
  104. data/spec/integration/numeric_validator/spec_helper.rb +5 -77
  105. data/spec/integration/primitive_validator/primitive_validator_spec.rb +90 -0
  106. data/spec/integration/primitive_validator/spec_helper.rb +5 -0
  107. data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
  108. data/spec/integration/required_field_validator/association_spec.rb +5 -8
  109. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +4 -7
  110. data/spec/integration/required_field_validator/date_type_value_spec.rb +3 -6
  111. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +3 -6
  112. data/spec/integration/required_field_validator/float_type_value_spec.rb +3 -6
  113. data/spec/integration/required_field_validator/integer_type_value_spec.rb +2 -5
  114. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +5 -8
  115. data/spec/integration/required_field_validator/shared_examples.rb +1 -1
  116. data/spec/integration/required_field_validator/spec_helper.rb +6 -67
  117. data/spec/integration/required_field_validator/string_type_value_spec.rb +2 -5
  118. data/spec/integration/required_field_validator/text_type_value_spec.rb +2 -5
  119. data/spec/integration/shared/default_validation_context.rb +13 -0
  120. data/spec/integration/shared/valid_and_invalid_model.rb +27 -0
  121. data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
  122. data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +114 -0
  123. data/spec/integration/within_validator/spec_helper.rb +5 -0
  124. data/spec/integration/within_validator/within_validator_spec.rb +164 -0
  125. data/spec/public/resource_spec.rb +68 -0
  126. data/spec/spec.opts +1 -0
  127. data/spec/spec_helper.rb +12 -4
  128. data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
  129. data/spec/unit/contextual_validators/execution_spec.rb +55 -0
  130. data/spec/unit/contextual_validators/spec_helper.rb +37 -0
  131. data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
  132. data/spec/unit/validation_errors/adding_spec.rb +54 -0
  133. data/spec/unit/validation_errors/emptiness_spec.rb +38 -0
  134. data/spec/unit/validation_errors/enumerable_spec.rb +32 -0
  135. data/tasks/install.rb +1 -1
  136. data/tasks/spec.rb +4 -4
  137. metadata +118 -55
  138. data/README.txt +0 -72
  139. data/lib/dm-validations/custom_validator.rb +0 -72
  140. data/lib/dm-validations/length_validator.rb +0 -113
  141. data/lib/dm-validations/method_validator.rb +0 -68
  142. data/lib/dm-validations/numeric_validator.rb +0 -83
  143. data/spec/integration/absent_field_validator_spec.rb +0 -36
  144. data/spec/integration/acceptance_validator_spec.rb +0 -87
  145. data/spec/integration/auto_validate_spec.rb +0 -342
  146. data/spec/integration/block_validator_spec.rb +0 -30
  147. data/spec/integration/confirmation_validator_spec.rb +0 -105
  148. data/spec/integration/contextual_validators_spec.rb +0 -27
  149. data/spec/integration/custom_validator_spec.rb +0 -9
  150. data/spec/integration/format_validator_spec.rb +0 -193
  151. data/spec/integration/generic_validator_spec.rb +0 -17
  152. data/spec/integration/length_validator/valid_objects_spec.rb +0 -13
  153. data/spec/integration/method_validator_spec.rb +0 -58
  154. data/spec/integration/numeric_validator_spec.rb +0 -253
  155. data/spec/integration/primitive_validator_spec.rb +0 -30
  156. data/spec/integration/uniqueness_validator_spec.rb +0 -97
  157. data/spec/integration/validation_errors_spec.rb +0 -18
  158. data/spec/integration/validation_spec.rb +0 -404
  159. data/spec/integration/within_validator_spec.rb +0 -79
@@ -1,30 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::ValidatesWithBlock do
5
- before(:all) do
6
- class ::Ship
7
- include DataMapper::Resource
8
- property :id, Integer, :key => true
9
- property :name, String
10
-
11
- validates_with_block :when => [:testing_failure] do
12
- [false, 'Validation failed']
13
- end
14
- validates_with_block :when => [:testing_success] do
15
- true
16
- end
17
- validates_with_block :name, :when => [:testing_name_validation] do
18
- [false, 'Name is invalid']
19
- end
20
- end
21
- end
22
-
23
- it "should validate via a block on the resource"
24
-
25
- it "should validate via a block and add error to field" do
26
- ship = Ship.new
27
- ship.should_not be_valid_for_testing_name_validation
28
- ship.errors.on(:name).should include('Name is invalid')
29
- end
30
- end
@@ -1,105 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::ConfirmationValidator do
5
- before(:all) do
6
- class ::Canoe
7
- include DataMapper::Resource
8
-
9
- property :id, Integer, :serial => true
10
- property :name, String
11
- property :name_confirmation, String
12
- property :size, Integer
13
-
14
- validates_is_confirmed :name
15
- end
16
- end
17
-
18
- it "should only validate if the attribute is dirty" do
19
- class ::Transformer
20
- include DataMapper::Resource
21
-
22
- property :id, Integer, :serial => true
23
- property :name, String
24
- property :assoc, String
25
-
26
- validates_is_confirmed :name
27
-
28
- attr_accessor :name_confirmation
29
- end
30
- Transformer.auto_migrate!
31
- # attribute_dirty?
32
- tf = Transformer.new(:name => "Optimus Prime", :name_confirmation => "Optimus Prime", :assoc => "Autobot")
33
- tf.should be_valid
34
- tf.save.should == true
35
- tf = Transformer.first
36
- tf.update_attributes(:assoc => "Autobot!").should == true
37
- end
38
-
39
- it "should validate the confirmation of a value on an instance of a resource" do
40
- canoe = Canoe.new
41
- canoe.name = 'White Water'
42
- canoe.name_confirmation = 'Not confirmed'
43
- canoe.should_not be_valid
44
- canoe.errors.on(:name).should include('Name does not match the confirmation')
45
- canoe.errors.full_messages.first.should == 'Name does not match the confirmation'
46
-
47
- canoe.name_confirmation = 'White Water'
48
- canoe.should be_valid
49
- end
50
-
51
- it "should default the name of the confirmation field to <field>_confirmation
52
- if one is not specified" do
53
- canoe = Canoe.new
54
- canoe.name = 'White Water'
55
- canoe.name_confirmation = 'White Water'
56
- canoe.should be_valid
57
- end
58
-
59
- it "should default to allowing nil values on the fields if not specified to" do
60
- Canoe.new.should be_valid
61
- end
62
-
63
- it "should not pass validation with a nil value when specified to" do
64
- class ::Canoe
65
- validators.clear!
66
- validates_is_confirmed :name, :allow_nil => false
67
- end
68
- canoe = Canoe.new
69
- canoe.should_not be_valid
70
- canoe.errors.on(:name).should include('Name does not match the confirmation')
71
- end
72
-
73
- it "should allow the name of the confirmation field to be set" do
74
- class ::Canoe
75
- validators.clear!
76
- validates_is_confirmed :name, :confirm => :name_check
77
- def name_check=(value)
78
- @name_check = value
79
- end
80
-
81
- def name_check
82
- @name_confirmation ||= nil
83
- end
84
- end
85
- canoe = Canoe.new
86
- canoe.name = 'Float'
87
- canoe.name_check = 'Float'
88
- canoe.should be_valid
89
- end
90
-
91
- it "should not require that the confirmation field be a property" do
92
- class ::Raft
93
- include DataMapper::Resource
94
- attr_accessor :name, :name_confirmation
95
-
96
- property :id, Integer, :serial => true
97
-
98
- validates_is_confirmed :name
99
- end
100
-
101
- raft = Raft.new(:id => 10)
102
- raft.name = 'Lifeboat'
103
- raft.should_not be_valid
104
- end
105
- end
@@ -1,27 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::ContextualValidators do
5
-
6
- before :all do
7
- class ::Kayak
8
- include DataMapper::Resource
9
- property :id, Integer, :key => true
10
- property :salesman, String, :auto_validation => false
11
- validates_absent :salesman, :when => :sold
12
- end
13
- end
14
-
15
- it "should pass validation for a specific context" do
16
- k = Kayak.new
17
- k.valid?(:sold).should == true
18
- k.salesman = 'John Doe'
19
- k.valid?(:sold).should_not == true
20
- k.errors.on(:salesman).should include('Salesman must be absent')
21
- end
22
-
23
- it "should raise an exception if you provide an invalid context to save" do
24
- lambda { Kayak.new.save(:invalid_context) }.should raise_error
25
- lambda { Kayak.new.save(false) }.should raise_error
26
- end
27
- end
@@ -1,9 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::CustomValidator do
5
- it "should have specs" do
6
- pending
7
- # FIXME do something, add specs
8
- end
9
- end
@@ -1,193 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::FormatValidator do
5
- before :all do
6
- class ::BillOfLading
7
- include DataMapper::Resource
8
-
9
- property :id, Serial
10
- property :doc_no, String, :auto_validation => false
11
- property :email, String, :auto_validation => false
12
- property :username, String, :auto_validation => false
13
- property :url, String, :auto_validation => false
14
-
15
- # this is a trivial example
16
- validates_format :doc_no, :with => lambda { |code|
17
- code =~ /\AA\d{4}\z/ || code =~ /\A[B-Z]\d{6}X12\z/
18
- }
19
-
20
- validates_format :email, :as => :email_address
21
- validates_format :url, :as => :url
22
-
23
- validates_format :username, :with => /[a-z]/, :message => 'Username must have at least one letter', :allow_nil => true
24
- end
25
- end
26
-
27
- def valid_attributes
28
- { :id => 1, :doc_no => 'A1234', :email => 'user@example.com', :url => 'http://example.com' }
29
- end
30
-
31
- it 'should validate the format of a value on an instance of a resource' do
32
- bol = BillOfLading.new(valid_attributes)
33
- bol.should be_valid
34
-
35
- bol.doc_no = 'BAD CODE :)'
36
- bol.should_not be_valid
37
- bol.errors.on(:doc_no).should include('Doc no has an invalid format')
38
-
39
- bol.doc_no = 'A1234'
40
- bol.should be_valid
41
-
42
- bol.doc_no = 'B123456X12'
43
- bol.should be_valid
44
- end
45
-
46
- describe "RFC2822 compatible email addresses" do
47
- before do
48
- @bol = BillOfLading.new(valid_attributes.except(:email))
49
-
50
- @valid_email_addresses = [
51
- '+1~1+@example.com',
52
- '{_dave_}@example.com',
53
- '"[[ dave ]]"@example.com',
54
- 'dave."dave"@example.com',
55
- 'test@localhost',
56
- 'test@example.com',
57
- 'test@example.co.uk',
58
- 'test@example.com.br',
59
- '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
60
- 'me@[187.223.45.119]',
61
- 'someone@123.com',
62
- 'simon&garfunkel@songs.com'
63
- ]
64
-
65
- @invalid_email_addresses = [
66
- '-- dave --@example.com',
67
- '[dave]@example.com',
68
- '.dave@example.com',
69
- 'Max@Job 3:14',
70
- 'Job@Book of Job',
71
- 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com'
72
- ]
73
- end
74
-
75
- it "should match the RFC reference addresses" do
76
- @valid_email_addresses.each do |email|
77
- email.should =~ DataMapper::Validate::Format::Email::EmailAddress
78
- end
79
- end
80
-
81
- it "should not be valid" do
82
- @invalid_email_addresses.each do |email|
83
- email.should_not =~ DataMapper::Validate::Format::Email::EmailAddress
84
- end
85
- end
86
-
87
- end
88
-
89
- it 'should have a pre-defined URL format' do
90
- bad = [ 'http:// example.com',
91
- 'ftp://example.com',
92
- 'http://.com',
93
- 'http://',
94
- 'test',
95
- '...'
96
- ]
97
-
98
- good = [
99
- 'http://example.com',
100
- 'http://www.example.com',
101
- ]
102
-
103
- bol = BillOfLading.new(valid_attributes.except(:url))
104
- bol.should_not be_valid
105
- bol.errors.on(:url).should include('Url has an invalid format')
106
-
107
- bad.map do |e|
108
- bol.url = e
109
- bol.valid?
110
- bol.errors.on(:url).should include('Url has an invalid format')
111
- end
112
-
113
- good.map do |e|
114
- bol.url = e
115
- bol.valid?
116
- bol.errors.on(:url).should be_nil
117
- end
118
-
119
- end
120
-
121
- describe 'with a regexp' do
122
- before do
123
- @bol = BillOfLading.new(valid_attributes)
124
- @bol.should be_valid
125
- end
126
-
127
- describe 'if matched' do
128
- before do
129
- @bol.username = 'a12345'
130
- end
131
-
132
- it 'should validate' do
133
- @bol.should be_valid
134
- end
135
- end
136
-
137
- describe 'if not matched' do
138
- before do
139
- @bol.username = '12345'
140
- end
141
-
142
- it 'should not validate' do
143
- @bol.should_not be_valid
144
- end
145
-
146
- it 'should set an error message' do
147
- @bol.valid?
148
- @bol.errors.on(:username).should include('Username must have at least one letter')
149
- end
150
- end
151
- end
152
- end
153
-
154
- =begin
155
- addresses = [
156
- '-- dave --@example.com', # (spaces are invalid unless enclosed in quotation marks)
157
- '[dave]@example.com', # (square brackets are invalid, unless contained within quotation marks)
158
- '.dave@example.com', # (the local part of a domain name cannot start with a period)
159
- 'Max@Job 3:14',
160
- 'Job@Book of Job',
161
- 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
162
- ]
163
- addresses.each do |address|
164
- if address =~ RFC2822::EmailAddress
165
- puts "#{address} deveria ter sido rejeitado, ERRO"
166
- else
167
- puts "#{address} rejeitado, OK"
168
- end
169
- end
170
-
171
-
172
- addresses = [
173
- '+1~1+@example.com',
174
- '{_dave_}@example.com',
175
- '"[[ dave ]]"@example.com',
176
- 'dave."dave"@example.com',
177
- 'test@localhost',
178
- 'test@example.com',
179
- 'test@example.co.uk',
180
- 'test@example.com.br',
181
- '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
182
- 'me@[187.223.45.119]',
183
- 'someone@123.com',
184
- 'simon&garfunkel@songs.com'
185
- ]
186
- addresses.each do |address|
187
- if address =~ RFC2822::EmailAddress
188
- puts "#{address} aceito, OK"
189
- else
190
- puts "#{address} deveria ser aceito, ERRO"
191
- end
192
- end
193
- =end
@@ -1,17 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- module DataMapper
5
- module Validate
6
- describe DataMapper::Validate::GenericValidator do
7
- describe "#==" do
8
- it "should return true if types and fields are equal" do
9
- RequiredFieldValidator.new(:name).should == RequiredFieldValidator.new(:name)
10
- end
11
- it "should return false of types differ" do
12
- RequiredFieldValidator.new(:name).should_not == UniquenessValidator.new(:name)
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,13 +0,0 @@
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
@@ -1,58 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
-
4
- describe DataMapper::Validate::MethodValidator do
5
- before(:all) do
6
- class ::Ship
7
- include DataMapper::Resource
8
- property :id, Integer, :key => true
9
- property :name, String
10
- property :size, String
11
-
12
- validates_with_method :fail_validation, :when => [:testing_failure]
13
- validates_with_method :pass_validation, :when => [:testing_success]
14
- validates_with_method :first_validation, :second_validation, :when => [:multiple_validations]
15
- validates_with_method :name, :method => :name_validation, :when => [:testing_name_validation]
16
-
17
- def fail_validation
18
- return false, 'Validation failed'
19
- end
20
-
21
- def pass_validation
22
- return true
23
- end
24
-
25
- def first_validation
26
- return true
27
- end
28
-
29
- def second_validation
30
- return false, 'Second Validation was false'
31
- end
32
-
33
- def name_validation
34
- return false, 'Name is invalid'
35
- end
36
- end
37
- end
38
-
39
- it "should validate via a method on the resource" do
40
- Ship.new.valid_for_testing_failure?.should == false
41
- Ship.new.valid_for_testing_success?.should == true
42
- ship = Ship.new
43
- ship.valid_for_testing_failure?.should == false
44
- ship.errors.on(:fail_validation).should include('Validation failed')
45
- end
46
-
47
- it "should run multiple validation methods" do
48
- ship = Ship.new
49
- ship.valid_for_multiple_validations?.should == false
50
- ship.errors.on(:second_validation).should include('Second Validation was false')
51
- end
52
-
53
- it "should validate via a method and add error to field" do
54
- ship = Ship.new
55
- ship.should_not be_valid_for_testing_name_validation
56
- ship.errors.on(:name).should include('Name is invalid')
57
- end
58
- end