dm-validations 0.10.1 → 0.10.2

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 (102) hide show
  1. data/Rakefile +23 -18
  2. data/VERSION +1 -0
  3. data/dm-validations.gemspec +188 -0
  4. data/lib/dm-validations.rb +25 -75
  5. data/lib/dm-validations/auto_validate.rb +7 -7
  6. data/lib/dm-validations/contextual_validators.rb +4 -18
  7. data/lib/dm-validations/exceptions.rb +3 -1
  8. data/lib/dm-validations/support/context.rb +40 -1
  9. data/lib/dm-validations/validation_errors.rb +2 -13
  10. data/lib/dm-validations/validators/absent_field_validator.rb +2 -2
  11. data/lib/dm-validations/validators/acceptance_validator.rb +14 -16
  12. data/lib/dm-validations/validators/confirmation_validator.rb +17 -15
  13. data/lib/dm-validations/validators/format_validator.rb +18 -11
  14. data/lib/dm-validations/validators/generic_validator.rb +45 -15
  15. data/lib/dm-validations/validators/length_validator.rb +2 -11
  16. data/lib/dm-validations/validators/method_validator.rb +2 -2
  17. data/lib/dm-validations/validators/numeric_validator.rb +8 -6
  18. data/lib/dm-validations/validators/primitive_validator.rb +1 -1
  19. data/lib/dm-validations/validators/uniqueness_validator.rb +13 -9
  20. data/lib/dm-validations/validators/within_validator.rb +4 -3
  21. data/spec/fixtures/basketball_player.rb +0 -3
  22. data/spec/fixtures/bill_of_landing.rb +1 -1
  23. data/spec/fixtures/city.rb +0 -1
  24. data/spec/fixtures/company.rb +1 -3
  25. data/spec/fixtures/corporate_world.rb +0 -6
  26. data/spec/fixtures/country.rb +0 -1
  27. data/spec/fixtures/event.rb +1 -1
  28. data/spec/fixtures/page.rb +2 -2
  29. data/spec/fixtures/reservation.rb +1 -1
  30. data/spec/fixtures/scm_operation.rb +0 -8
  31. data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +6 -2
  32. data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +36 -36
  33. data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +2 -2
  34. data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +4 -4
  35. data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +2 -2
  36. data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +2 -0
  37. data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +3 -1
  38. data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +2 -0
  39. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +6 -2
  40. data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +8 -4
  41. data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +3 -1
  42. data/spec/integration/automatic_validation/spec_helper.rb +6 -6
  43. data/spec/integration/block_validator/block_validator_spec.rb +1 -1
  44. data/spec/integration/conditional_validation/if_condition_spec.rb +3 -1
  45. data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +3 -1
  46. data/spec/integration/datamapper_models/association_validation_spec.rb +4 -1
  47. data/spec/integration/datamapper_models/inheritance_spec.rb +6 -2
  48. data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +3 -1
  49. data/spec/integration/format_validator/email_format_validator_spec.rb +5 -1
  50. data/spec/integration/format_validator/format_validator_spec.rb +5 -1
  51. data/spec/integration/format_validator/regexp_validator_spec.rb +5 -1
  52. data/spec/integration/format_validator/url_format_validator_spec.rb +8 -4
  53. data/spec/integration/length_validator/default_value_spec.rb +4 -2
  54. data/spec/integration/length_validator/equality_spec.rb +3 -1
  55. data/spec/integration/length_validator/error_message_spec.rb +3 -1
  56. data/spec/integration/length_validator/maximum_spec.rb +3 -1
  57. data/spec/integration/length_validator/minimum_spec.rb +3 -1
  58. data/spec/integration/length_validator/range_spec.rb +3 -1
  59. data/spec/integration/method_validator/method_validator_spec.rb +3 -1
  60. data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +5 -1
  61. data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +5 -1
  62. data/spec/integration/numeric_validator/float_type_spec.rb +3 -1
  63. data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +3 -1
  64. data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +3 -1
  65. data/spec/integration/numeric_validator/integer_only_true_spec.rb +11 -9
  66. data/spec/integration/numeric_validator/integer_type_spec.rb +10 -8
  67. data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +3 -1
  68. data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +3 -1
  69. data/spec/integration/primitive_validator/primitive_validator_spec.rb +3 -1
  70. data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +6 -6
  71. data/spec/integration/required_field_validator/association_spec.rb +9 -4
  72. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +24 -18
  73. data/spec/integration/required_field_validator/date_type_value_spec.rb +21 -19
  74. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +21 -19
  75. data/spec/integration/required_field_validator/float_type_value_spec.rb +21 -19
  76. data/spec/integration/required_field_validator/integer_type_value_spec.rb +17 -15
  77. data/spec/integration/required_field_validator/shared_examples.rb +5 -3
  78. data/spec/integration/required_field_validator/string_type_value_spec.rb +20 -16
  79. data/spec/integration/required_field_validator/text_type_value_spec.rb +10 -6
  80. data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +23 -25
  81. data/spec/integration/within_validator/within_validator_spec.rb +7 -3
  82. data/spec/public/resource_spec.rb +5 -6
  83. data/spec/rcov.opts +6 -0
  84. data/spec/spec.opts +2 -0
  85. data/spec/unit/contextual_validators/emptiness_spec.rb +1 -1
  86. data/spec/unit/contextual_validators/execution_spec.rb +9 -16
  87. data/spec/unit/generic_validator/equality_operator_spec.rb +3 -3
  88. data/spec/unit/validation_errors/adding_spec.rb +1 -1
  89. data/spec/unit/validation_errors/emptiness_spec.rb +1 -1
  90. data/spec/unit/validation_errors/enumerable_spec.rb +1 -1
  91. data/tasks/ci.rake +1 -0
  92. data/tasks/metrics.rake +36 -0
  93. data/tasks/spec.rake +41 -0
  94. data/tasks/yard.rake +9 -0
  95. data/tasks/yardstick.rake +19 -0
  96. metadata +36 -18
  97. data/History.rdoc +0 -45
  98. data/Manifest.txt +0 -139
  99. data/TODO +0 -16
  100. data/lib/dm-validations/version.rb +0 -5
  101. data/tasks/install.rb +0 -13
  102. data/tasks/spec.rb +0 -25
@@ -10,7 +10,7 @@ module DataMapper
10
10
  value = target.validation_property_value(field_name)
11
11
  property = target.validation_property(field_name)
12
12
 
13
- return true if value.nil? || value.kind_of?(property.primitive) || property.primitive == TrueClass && value.kind_of?(FalseClass)
13
+ return true if value.nil? || property.primitive?(value)
14
14
 
15
15
  error_message = @options[:message] || default_error(property)
16
16
  add_error(target, error_message, field_name)
@@ -12,30 +12,34 @@ module DataMapper
12
12
  assert_kind_of 'scope', options[:scope], Array, Symbol if options.has_key?(:scope)
13
13
  super
14
14
 
15
- @options[:allow_nil] = true unless @options.include?(:allow_nil)
15
+ set_optional_by_default
16
16
  end
17
17
 
18
18
  def call(target)
19
- value = target.send(field_name)
19
+ return true if valid?(target)
20
20
 
21
- return true if @options[:allow_nil] && value.blank?
21
+ error_message = @options[:message] || ValidationErrors.default_error_message(:taken, field_name)
22
+ add_error(target, error_message, field_name)
23
+
24
+ false
25
+ end
26
+
27
+ def valid?(target)
28
+ value = target.validation_property_value(field_name)
29
+ return true if optional?(value)
22
30
 
23
31
  opts = {
24
32
  :fields => target.model.key,
25
33
  field_name => value,
26
34
  }
27
35
 
28
- Array(@options[:scope]).each { |subject| opts[subject] = target.send(subject) }
36
+ Array(@options[:scope]).each { |subject| opts[subject] = target.__send__(subject) }
29
37
 
30
38
  resource = DataMapper.repository(target.repository.name) { target.model.first(opts) }
31
39
 
32
40
  return true if resource.nil?
33
- return true if target.saved? && resource.key == target.key
34
41
 
35
- error_message = @options[:message] || ValidationErrors.default_error_message(:taken, field_name)
36
- add_error(target, error_message, field_name)
37
-
38
- false
42
+ target.saved? && resource.key == target.key
39
43
  end
40
44
  end # class UniquenessValidator
41
45
 
@@ -14,8 +14,8 @@ module DataMapper
14
14
  end
15
15
 
16
16
  def call(target)
17
- value = target.send(field_name)
18
- return true if @options[:allow_nil] && value.blank?
17
+ value = target.validation_property_value(field_name)
18
+ return true if optional?(value)
19
19
  return true if @options[:set].include?(value)
20
20
 
21
21
  set = @options[:set]
@@ -33,7 +33,8 @@ module DataMapper
33
33
  end
34
34
 
35
35
  add_error(target, error_message, field_name)
36
- return false
36
+
37
+ false
37
38
  end
38
39
 
39
40
  def n
@@ -29,9 +29,6 @@ module DataMapper
29
29
  # precision and scale need to be defined for length to be validated
30
30
  validates_is_number :height, :weight, :precision => 10
31
31
  end
32
- BasketballPlayer.auto_migrate!
33
-
34
-
35
32
  end # Fixtures
36
33
  end # Validate
37
34
  end # DataMapper
@@ -32,7 +32,7 @@ module DataMapper
32
32
  }
33
33
 
34
34
  validates_format :email, :as => :email_address
35
- validates_format :url, :as => :url, :allow_nil => false
35
+ validates_format :url, :as => :url, :allow_nil => false, :allow_blank => false
36
36
 
37
37
  validates_format :username, :with => /[a-z]/, :message => 'Username must have at least one letter', :allow_nil => true
38
38
  validates_format :code, :with => /\d{5,6}/, :message => 'Code format is invalid'
@@ -22,4 +22,3 @@ class City
22
22
 
23
23
  validates_is_number :founded_in, :message => "Foundation year must be an integer"
24
24
  end
25
- City.auto_migrate!
@@ -61,8 +61,6 @@ module DataMapper
61
61
  validates_present :title, :message => "Product company must have a name"
62
62
  validates_present :flagship_product
63
63
  end
64
- ProductCompany.auto_migrate!
65
-
66
64
 
67
65
  class Product
68
66
  #
@@ -76,7 +74,7 @@ module DataMapper
76
74
  #
77
75
 
78
76
  property :id, Serial
79
- property :name, String, :nullable => false
77
+ property :name, String, :required => true
80
78
 
81
79
  #
82
80
  # Associations
@@ -12,8 +12,6 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
12
12
  property :domain, String, :unique_index => true
13
13
 
14
14
  validates_is_unique :domain, :allow_nil => true
15
-
16
- auto_migrate!
17
15
  end
18
16
 
19
17
  class Department
@@ -23,8 +21,6 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
23
21
  property :name, String, :unique_index => true
24
22
 
25
23
  validates_is_unique :name
26
-
27
- auto_migrate!
28
24
  end
29
25
 
30
26
  class User
@@ -38,8 +34,6 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
38
34
 
39
35
  validates_is_unique :user_name, :when => :signing_up_for_department_account, :scope => [:department]
40
36
  validates_is_unique :user_name, :when => :signing_up_for_organization_account, :scope => [:organisation]
41
-
42
- auto_migrate!
43
37
  end
44
38
  end
45
39
  end
@@ -22,4 +22,3 @@ class Country
22
22
 
23
23
  validates_is_number :area, :message => "Please use integers to specify area"
24
24
  end
25
- Country.auto_migrate!
@@ -16,7 +16,7 @@ module DataMapper
16
16
  #
17
17
 
18
18
  property :id, Serial
19
- property :name, String, :nullable => false
19
+ property :name, String, :required => true
20
20
 
21
21
  property :starts_at, DateTime
22
22
  property :ends_at, DateTime
@@ -17,14 +17,14 @@ module DataMapper
17
17
  #
18
18
 
19
19
  property :id, Serial, :key => true
20
- property :body, Text, :nullable => false
20
+ property :body, Text, :required => true
21
21
 
22
22
  #
23
23
  # Validations
24
24
  #
25
25
 
26
26
  # duplicates inferred validation for body (caused by
27
- # :nullable => false)
27
+ # :required => true)
28
28
  validates_present :body
29
29
  end
30
30
  end # Fixtures
@@ -28,7 +28,7 @@ module DataMapper
28
28
  # Validations
29
29
  #
30
30
 
31
- validates_is_confirmed :person_name, :allow_nil => false
31
+ validates_is_confirmed :person_name, :allow_nil => false, :allow_blank => false
32
32
  validates_is_confirmed :number_of_seats, :confirm => :seats_confirmation, :message => Proc.new { |resource, property|
33
33
  '%s requires confirmation for %s' % [Extlib::Inflection.demodulize(resource.model.name), property.name]
34
34
  }
@@ -33,8 +33,6 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
33
33
 
34
34
  validates_present :name
35
35
  end
36
- ScmOperation.auto_migrate!
37
-
38
36
 
39
37
  class SubversionOperation < ScmOperation
40
38
  #
@@ -44,9 +42,6 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
44
42
  validates_present :network_connection, :when => [:committing, :log_viewing]
45
43
  end
46
44
 
47
- SubversionOperation.auto_migrate!
48
-
49
-
50
45
  class GitOperation < ScmOperation
51
46
  #
52
47
  # Validations
@@ -61,7 +56,4 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
61
56
  }
62
57
  validates_present :clean_working_copy, :when => :pulling
63
58
  end
64
-
65
-
66
- GitOperation.auto_migrate!
67
59
  end
@@ -1,8 +1,10 @@
1
1
  require 'spec_helper'
2
2
  require 'integration/absent_field_validator/spec_helper'
3
3
 
4
- describe DataMapper::Validate::Fixtures::Kayak do
4
+ describe 'DataMapper::Validate::Fixtures::Kayak' do
5
5
  before :all do
6
+ DataMapper::Validate::Fixtures::Kayak.auto_migrate!
7
+
6
8
  @kayak = DataMapper::Validate::Fixtures::Kayak.new
7
9
  @kayak.should be_valid_for_sale
8
10
  end
@@ -68,8 +70,10 @@ describe DataMapper::Validate::Fixtures::Kayak do
68
70
  end
69
71
 
70
72
 
71
- describe DataMapper::Validate::Fixtures::Pirogue do
73
+ describe 'DataMapper::Validate::Fixtures::Pirogue' do
72
74
  before :all do
75
+ DataMapper::Validate::Fixtures::Pirogue.auto_migrate!
76
+
73
77
  @kayak = DataMapper::Validate::Fixtures::Pirogue.new
74
78
  @kayak.should_not be_valid_for_sale
75
79
  end
@@ -1,16 +1,20 @@
1
1
  require 'spec_helper'
2
2
  require 'integration/acceptance_validator/spec_helper'
3
3
 
4
- describe DataMapper::Validate::Fixtures::BetaTesterAccount do
5
- before :each do
4
+ describe 'DataMapper::Validate::Fixtures::BetaTesterAccount' do
5
+ before :all do
6
+ DataMapper::Validate::Fixtures::BetaTesterAccount.auto_migrate!
7
+ end
8
+
9
+ before do
6
10
  @model = DataMapper::Validate::Fixtures::BetaTesterAccount.new(:user_agreement => true,
7
- :newsletter_signup => "",
11
+ :newsletter_signup => nil,
8
12
  :privacy_agreement => "accept")
9
13
  @model.should be_valid
10
14
  end
11
15
 
12
16
  describe "with a missing newsletter signup field" do
13
- before :each do
17
+ before do
14
18
  @model.newsletter_signup = nil
15
19
  end
16
20
 
@@ -20,18 +24,17 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
20
24
  end
21
25
 
22
26
  describe "with a blank newsletter signup field" do
23
- before :each do
27
+ before do
24
28
  @model.newsletter_signup = ""
25
29
  end
26
30
 
27
- it "is perfectly valid" do
28
- @model.should be_valid
31
+ it "is NOT valid" do
32
+ @model.should_not be_valid
29
33
  end
30
34
  end
31
35
 
32
-
33
36
  describe "with a blank user agreement field" do
34
- before :each do
37
+ before do
35
38
  @model.user_agreement = ""
36
39
  end
37
40
 
@@ -40,9 +43,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
40
43
  end
41
44
  end
42
45
 
43
-
44
46
  describe "with a nil user agreement field" do
45
- before :each do
47
+ before do
46
48
  @model.user_agreement = nil
47
49
  end
48
50
 
@@ -51,9 +53,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
51
53
  end
52
54
  end
53
55
 
54
-
55
56
  describe "with user agreement field having value of 1 (as integer)" do
56
- before :each do
57
+ before do
57
58
  @model.user_agreement = 1
58
59
  end
59
60
 
@@ -62,9 +63,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
62
63
  end
63
64
  end
64
65
 
65
-
66
66
  describe "with user agreement field having value of 1 (as a string)" do
67
- before :each do
67
+ before do
68
68
  @model.user_agreement = "1"
69
69
  end
70
70
 
@@ -73,9 +73,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
73
73
  end
74
74
  end
75
75
 
76
-
77
76
  describe "with user agreement field having value of 'true' (as a string)" do
78
- before :each do
77
+ before do
79
78
  @model.user_agreement = 'true'
80
79
  end
81
80
 
@@ -84,9 +83,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
84
83
  end
85
84
  end
86
85
 
87
-
88
86
  describe "with user agreement field having value of true (TrueClass instance)" do
89
- before :each do
87
+ before do
90
88
  @model.user_agreement = true
91
89
  end
92
90
 
@@ -95,9 +93,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
95
93
  end
96
94
  end
97
95
 
98
-
99
96
  describe "with user agreement field having value of 't' (The Lisp Way)" do
100
- before :each do
97
+ before do
101
98
  @model.user_agreement = 't'
102
99
  end
103
100
 
@@ -106,9 +103,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
106
103
  end
107
104
  end
108
105
 
109
-
110
106
  describe "with user agreement field having value of 'f'" do
111
- before :each do
107
+ before do
112
108
  @model.user_agreement = 'f'
113
109
  end
114
110
 
@@ -117,9 +113,18 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
117
113
  end
118
114
  end
119
115
 
116
+ describe "with user agreement field having value of false" do
117
+ before do
118
+ @model.user_agreement = false
119
+ end
120
+
121
+ it "is NOT valid" do
122
+ @model.should_not be_valid
123
+ end
124
+ end
120
125
 
121
126
  describe "with privacy agreement field having value of 1" do
122
- before :each do
127
+ before do
123
128
  @model.privacy_agreement = 1
124
129
  end
125
130
 
@@ -129,7 +134,7 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
129
134
  end
130
135
 
131
136
  describe "with privacy agreement field having value of true" do
132
- before :each do
137
+ before do
133
138
  @model.privacy_agreement = true
134
139
  end
135
140
 
@@ -138,9 +143,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
138
143
  end
139
144
  end
140
145
 
141
-
142
146
  describe "with privacy agreement field having value of '1'" do
143
- before :each do
147
+ before do
144
148
  @model.privacy_agreement = '1'
145
149
  end
146
150
 
@@ -149,9 +153,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
149
153
  end
150
154
  end
151
155
 
152
-
153
156
  describe "with privacy agreement field having value of 't'" do
154
- before :each do
157
+ before do
155
158
  @model.privacy_agreement = 't'
156
159
  end
157
160
 
@@ -160,9 +163,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
160
163
  end
161
164
  end
162
165
 
163
-
164
166
  describe "with privacy agreement field having value of 'accept'" do
165
- before :each do
167
+ before do
166
168
  @model.privacy_agreement = 'accept'
167
169
  end
168
170
 
@@ -171,9 +173,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
171
173
  end
172
174
  end
173
175
 
174
-
175
176
  describe "with privacy agreement field having value of 'agreed'" do
176
- before :each do
177
+ before do
177
178
  @model.privacy_agreement = 'agreed'
178
179
  end
179
180
 
@@ -182,9 +183,8 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
182
183
  end
183
184
  end
184
185
 
185
-
186
186
  describe "with privacy agreement field having value of 'ah, greed'" do
187
- before :each do
187
+ before do
188
188
  @model.privacy_agreement = 'ah, greed'
189
189
  end
190
190
 
@@ -193,4 +193,4 @@ describe DataMapper::Validate::Fixtures::BetaTesterAccount do
193
193
  @model.should_not be_valid
194
194
  end
195
195
  end
196
- end # describe DataMapper::Validate::Fixtures::BetaTesterAccount do
196
+ end