missing_validators 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +95 -1
- data/config/locales/en.yml +4 -0
- data/lib/missing_validators.rb +3 -0
- data/lib/missing_validators/matchers/ensure_equality_of_matcher.rb +27 -0
- data/lib/missing_validators/validators/base_validator.rb +19 -0
- data/lib/missing_validators/validators/color_validator.rb +3 -18
- data/lib/missing_validators/validators/email_validator.rb +4 -19
- data/lib/missing_validators/validators/equality_validator.rb +24 -0
- data/lib/missing_validators/validators/imei_validator.rb +4 -18
- data/lib/missing_validators/validators/inequality_validator.rb +9 -3
- data/lib/missing_validators/validators/latitude_validator.rb +1 -16
- data/lib/missing_validators/validators/longitude_validator.rb +1 -16
- data/lib/missing_validators/validators/mac_address_validator.rb +1 -16
- data/lib/missing_validators/validators/url_validator.rb +0 -1
- data/lib/missing_validators/version.rb +1 -1
- data/missing_validators.gemspec +1 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/validators/equality_validator_spec.rb +69 -0
- data/spec/validators/inequality_validator_spec.rb +54 -30
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86d60392993a9b27e6d49a2ea7eb698c6ee98f1c
|
4
|
+
data.tar.gz: 5ff9de4d33d2e3a97c9c07743c7bc60b9d87f8dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0349e0818d3a3281cbd2bd77803c6fc12409e85a890ba537ced0897e0d22e826f91c0a53d16f2c1256df86d394678ce358919ab4b249820f26c90930df103111
|
7
|
+
data.tar.gz: 91229866d28463716b4f65bc81a472c87efb487e2c32504cc3d0e7930930475f7bb802bf4fc28522fe7b5ffcc0f9ae9762ab32ea49db2ab36467722653edff80
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# MissingValidators
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/andrewgr/missing_validators.png)](https://travis-ci.org/andrewgr/missing_validators)
|
4
|
-
[![Code Climate](https://codeclimate.com/
|
4
|
+
[![Code Climate](https://codeclimate.com/github/andrewgr/missing_validators/badges/gpa.svg)](https://codeclimate.com/github/andrewgr/missing_validators)
|
5
5
|
|
6
6
|
MissingValidators is a collection of custom validators that are often required in Rails applications plus shoulda-style RSpec matchers to test the validation rules.
|
7
7
|
|
@@ -143,6 +143,100 @@ RSpec matcher is also available for your convenience:
|
|
143
143
|
it { should ensure_valid_mac_address_format_of }
|
144
144
|
end
|
145
145
|
|
146
|
+
### ColorValidator
|
147
|
+
|
148
|
+
Ensures that the color is a hexadecimal value starting with '#':
|
149
|
+
|
150
|
+
With an ActiveRecord model:
|
151
|
+
|
152
|
+
class Widget < ActiveRecord::Base
|
153
|
+
attr_accessor :color
|
154
|
+
validates :color, color: true
|
155
|
+
end
|
156
|
+
|
157
|
+
Or any ruby class:
|
158
|
+
|
159
|
+
class Widget
|
160
|
+
include ActiveModel::Validations
|
161
|
+
attr_accessor :color
|
162
|
+
validates :color, color: true
|
163
|
+
end
|
164
|
+
|
165
|
+
RSpec matcher is not available yet.
|
166
|
+
|
167
|
+
### ImeiValidator
|
168
|
+
|
169
|
+
Ensures that IMEI is in one of the following formats:
|
170
|
+
|
171
|
+
'356843052637512'
|
172
|
+
'35-6843052-637512'
|
173
|
+
'35.6843052.637512'
|
174
|
+
|
175
|
+
and its check digit is correct.
|
176
|
+
|
177
|
+
With an ActiveRecord model:
|
178
|
+
|
179
|
+
class Phone < ActiveRecord::Base
|
180
|
+
attr_accessor :imei
|
181
|
+
validates :imei, imei: true
|
182
|
+
end
|
183
|
+
|
184
|
+
Or any ruby class:
|
185
|
+
|
186
|
+
class Phone
|
187
|
+
include ActiveModel::Validations
|
188
|
+
attr_accessor :imei
|
189
|
+
validates :imei, imei: true
|
190
|
+
end
|
191
|
+
|
192
|
+
RSpec matcher is also available for your convenience:
|
193
|
+
|
194
|
+
describe Phone do
|
195
|
+
it { should ensure_valid_imei_format_of }
|
196
|
+
end
|
197
|
+
|
198
|
+
### LatitudeValidator
|
199
|
+
|
200
|
+
Ensures that the value is between -90 and 90:
|
201
|
+
|
202
|
+
With an ActiveRecord model:
|
203
|
+
|
204
|
+
class Coordinate < ActiveRecord::Base
|
205
|
+
attr_accessor :latitude
|
206
|
+
validates :latitude, latitude: true
|
207
|
+
end
|
208
|
+
|
209
|
+
Or any ruby class:
|
210
|
+
|
211
|
+
class Coordinate
|
212
|
+
include ActiveModel::Validations
|
213
|
+
attr_accessor :latitude
|
214
|
+
validates :latitude, latitude: true
|
215
|
+
end
|
216
|
+
|
217
|
+
RSpec matcher is not available yet.
|
218
|
+
|
219
|
+
### LongitudeValidator
|
220
|
+
|
221
|
+
Ensures that the value is between -180 and 180:
|
222
|
+
|
223
|
+
With an ActiveRecord model:
|
224
|
+
|
225
|
+
class Coordinate < ActiveRecord::Base
|
226
|
+
attr_accessor :longitude
|
227
|
+
validates :longitude, longitude: true
|
228
|
+
end
|
229
|
+
|
230
|
+
Or any ruby class:
|
231
|
+
|
232
|
+
class Coordinate
|
233
|
+
include ActiveModel::Validations
|
234
|
+
attr_accessor :longitude
|
235
|
+
validates :longitude, longitude: true
|
236
|
+
end
|
237
|
+
|
238
|
+
RSpec matcher is not available yet.
|
239
|
+
|
146
240
|
## Contributing
|
147
241
|
|
148
242
|
Your contribution is welcome.
|
data/config/locales/en.yml
CHANGED
@@ -3,6 +3,7 @@ en:
|
|
3
3
|
messages:
|
4
4
|
imei: "is not a valid IMEI"
|
5
5
|
inequality: "cannot be equal to %{attr}"
|
6
|
+
equality: "must be equal to %{attr}"
|
6
7
|
email: "is not a valid email address"
|
7
8
|
url: "is not a valid URL"
|
8
9
|
mac_address: "is not a valid MAC address"
|
@@ -14,6 +15,9 @@ en:
|
|
14
15
|
ensure_inequality_of:
|
15
16
|
failure_message_for_should: "#{model} should ensure inequality on attribute #{attr}"
|
16
17
|
failure_message_for_should_not: "#{model} should not ensure inequality on attribute #{attr}"
|
18
|
+
ensure_equality_of:
|
19
|
+
failure_message_for_should: "#{model} should ensure equality on attribute #{attr}"
|
20
|
+
failure_message_for_should_not: "#{model} should not ensure equality on attribute #{attr}"
|
17
21
|
ensure_valid_email_format_of:
|
18
22
|
failure_message_for_should: "#{model} should ensure valid email format of attribute #{attr}"
|
19
23
|
failure_message_for_should_not: "#{model} should not ensure valid email format of attribute #{attr}"
|
data/lib/missing_validators.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'rspec/matchers' if defined?(RSpec)
|
3
3
|
require 'missing_validators/version'
|
4
|
+
require 'missing_validators/validators/base_validator'
|
4
5
|
require 'missing_validators/validators/inequality_validator'
|
5
6
|
require 'missing_validators/matchers/ensure_inequality_of_matcher' if defined?(RSpec)
|
7
|
+
require 'missing_validators/validators/equality_validator'
|
8
|
+
require 'missing_validators/matchers/ensure_equality_of_matcher' if defined?(RSpec)
|
6
9
|
require 'missing_validators/validators/email_validator'
|
7
10
|
require 'missing_validators/matchers/ensure_valid_email_format_of' if defined?(RSpec)
|
8
11
|
require 'missing_validators/validators/url_validator'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_equality_of do |attribute|
|
2
|
+
chain :to do |to|
|
3
|
+
@to = to
|
4
|
+
end
|
5
|
+
|
6
|
+
match do |model|
|
7
|
+
raise Exception if @to.nil?
|
8
|
+
|
9
|
+
value = model.send(attribute)
|
10
|
+
model.send("#{@to}=", !!value)
|
11
|
+
model.valid?
|
12
|
+
|
13
|
+
if model.errors.has_key?(attribute)
|
14
|
+
model.errors[attribute].include?(I18n.t('errors.messages.equality', attr: @to))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
failure_message do |model|
|
19
|
+
I18n.t 'missing_validators.matchers.ensure_equality_of.failure_message_for_should',
|
20
|
+
model: model.class, attr: attribute.inspect
|
21
|
+
end
|
22
|
+
|
23
|
+
failure_message_when_negated do |model|
|
24
|
+
I18n.t 'missing_validators.matchers.ensure_equality_of.failure_message_for_should_not',
|
25
|
+
model: model.class, attr: attribute.inspect
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Base class for most validators in the gem.
|
2
|
+
#
|
3
|
+
class BaseValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
allow_blank = options[:allow_blank] || false
|
6
|
+
return if allow_blank && value.blank?
|
7
|
+
|
8
|
+
unless self.class.valid?(value, options)
|
9
|
+
key = self.class.name.underscore.split('_').reverse.drop(1).reverse.join('_')
|
10
|
+
record.errors[attribute] << (options[:message] || I18n.t("errors.messages.#{key}"))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def self.valid?(color, options)
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
19
|
+
end
|
@@ -5,29 +5,14 @@
|
|
5
5
|
# attr_accessor :color
|
6
6
|
# validates :color, color: true
|
7
7
|
# end
|
8
|
-
class ColorValidator <
|
9
|
-
# Checks if an attribute value is a valid hex color.
|
10
|
-
#
|
11
|
-
# @param [Object] record object to validate
|
12
|
-
# @param [String] attribute name of the object attribute to validate
|
13
|
-
# @param [Object] value attribute value
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
allow_blank = options[:allow_blank] || false
|
16
|
-
return if allow_blank && value.blank?
|
17
|
-
|
18
|
-
unless valid?(value, options)
|
19
|
-
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.color'))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
8
|
+
class ColorValidator < BaseValidator
|
23
9
|
def self.validate_format(color)
|
24
10
|
!!(color =~ /^#(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/i)
|
25
11
|
end
|
26
12
|
|
27
13
|
private
|
28
14
|
|
29
|
-
def valid?(color, options)
|
30
|
-
|
15
|
+
def self.valid?(color, options)
|
16
|
+
validate_format(color)
|
31
17
|
end
|
32
|
-
|
33
18
|
end
|
@@ -5,21 +5,7 @@
|
|
5
5
|
# attr_accessor :email, :name
|
6
6
|
# validates :email, email: true
|
7
7
|
# end
|
8
|
-
class EmailValidator <
|
9
|
-
# Checks if an attribute value is a valid email address.
|
10
|
-
#
|
11
|
-
# @param [Object] record object to validate
|
12
|
-
# @param [String] attribute name of the object attribute to validate
|
13
|
-
# @param [Object] value attribute value
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
allow_blank = options[:allow_blank] || false
|
16
|
-
return if allow_blank && value.blank?
|
17
|
-
|
18
|
-
unless valid?(value, options)
|
19
|
-
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email'))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
8
|
+
class EmailValidator < BaseValidator
|
23
9
|
def self.validate_format(email)
|
24
10
|
!!(email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
|
25
11
|
end
|
@@ -31,9 +17,8 @@ class EmailValidator < ActiveModel::EachValidator
|
|
31
17
|
|
32
18
|
private
|
33
19
|
|
34
|
-
def valid?(email, options)
|
35
|
-
|
36
|
-
&&
|
20
|
+
def self.valid?(email, options)
|
21
|
+
validate_format(email) \
|
22
|
+
&& validate_domain(email, [*(options[:domain])])
|
37
23
|
end
|
38
|
-
|
39
24
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Allows to check if the value of a specific attribute is equal to
|
2
|
+
# the value of another attribute of an object.
|
3
|
+
#
|
4
|
+
class EqualityValidator < ActiveModel::EachValidator
|
5
|
+
# Checks if an attribute value is unequal to another attrubute value.
|
6
|
+
#
|
7
|
+
# @param [Object] record object to validate
|
8
|
+
# @param [String] attribute name of the object attribute to validate
|
9
|
+
# @param [Object] value attribute value
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
equal_to = options[:to]
|
12
|
+
|
13
|
+
equal_to_value = if equal_to.respond_to?(:call)
|
14
|
+
options[:to].call(record)
|
15
|
+
else
|
16
|
+
record.send(equal_to.to_sym)
|
17
|
+
end
|
18
|
+
|
19
|
+
if equal_to.present? && value != equal_to_value
|
20
|
+
message = options[:message] || I18n.t('errors.messages.equality', attr: equal_to)
|
21
|
+
record.errors[attribute] << message
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -5,21 +5,7 @@
|
|
5
5
|
# attr_accessor :imei
|
6
6
|
# validates :imei, imei: true
|
7
7
|
# end
|
8
|
-
class ImeiValidator <
|
9
|
-
# Checks if an attribute value is a valid IMEI address.
|
10
|
-
#
|
11
|
-
# @param [Object] record object to validate
|
12
|
-
# @param [String] attribute name of the object attribute to validate
|
13
|
-
# @param [Object] value attribute value
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
allow_blank = options[:allow_blank] || false
|
16
|
-
return if allow_blank && value.blank?
|
17
|
-
|
18
|
-
unless valid?(value.to_s, options)
|
19
|
-
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.imei'))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
8
|
+
class ImeiValidator < BaseValidator
|
23
9
|
def self.validate_format(imei_number)
|
24
10
|
!!(imei_number =~ /\A[\d\.\:\-\s]+\z/i) # 356843052637512 or 35-6843052-637512 or 35.6843052.637512
|
25
11
|
end
|
@@ -43,8 +29,8 @@ class ImeiValidator < ActiveModel::EachValidator
|
|
43
29
|
|
44
30
|
private
|
45
31
|
|
46
|
-
def valid?(imei, options)
|
47
|
-
|
48
|
-
&&
|
32
|
+
def self.valid?(imei, options)
|
33
|
+
validate_format(imei.to_s) \
|
34
|
+
&& luhn_valid?(imei.to_s)
|
49
35
|
end
|
50
36
|
end
|
@@ -13,10 +13,16 @@ class InequalityValidator < ActiveModel::EachValidator
|
|
13
13
|
# @param [String] attribute name of the object attribute to validate
|
14
14
|
# @param [Object] value attribute value
|
15
15
|
def validate_each(record, attribute, value)
|
16
|
-
|
16
|
+
unequal_to = options[:to]
|
17
17
|
|
18
|
-
if
|
19
|
-
|
18
|
+
unequal_to_value = if unequal_to.respond_to?(:call)
|
19
|
+
options[:to].call(record)
|
20
|
+
else
|
21
|
+
record.send(unequal_to.to_sym)
|
22
|
+
end
|
23
|
+
|
24
|
+
if unequal_to.present? && value == unequal_to_value
|
25
|
+
message = options[:message] || I18n.t('errors.messages.inequality', attr: unequal_to)
|
20
26
|
record.errors[attribute] << message
|
21
27
|
end
|
22
28
|
end
|
@@ -5,25 +5,10 @@
|
|
5
5
|
# attr_accessor :lat
|
6
6
|
# validates :lat, latitude: true
|
7
7
|
# end
|
8
|
-
class LatitudeValidator <
|
9
|
-
# Checks if an attribute value is a valid MAC address.
|
10
|
-
#
|
11
|
-
# @param [Object] record object to validate
|
12
|
-
# @param [String] attribute name of the object attribute to validate
|
13
|
-
# @param [Object] value attribute value
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
allow_blank = options[:allow_blank] || false
|
16
|
-
return if allow_blank && value.blank?
|
17
|
-
|
18
|
-
unless self.class.valid?(value, options)
|
19
|
-
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.latitude'))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
8
|
+
class LatitudeValidator < BaseValidator
|
23
9
|
private
|
24
10
|
|
25
11
|
def self.valid?(latitude, options)
|
26
12
|
latitude.present? && latitude >= -90 && latitude <= 90
|
27
13
|
end
|
28
|
-
|
29
14
|
end
|
@@ -5,25 +5,10 @@
|
|
5
5
|
# attr_accessor :lon
|
6
6
|
# validates :lon, longitude: true
|
7
7
|
# end
|
8
|
-
class LongitudeValidator <
|
9
|
-
# Checks if an attribute value is a valid MAC address.
|
10
|
-
#
|
11
|
-
# @param [Object] record object to validate
|
12
|
-
# @param [String] attribute name of the object attribute to validate
|
13
|
-
# @param [Object] value attribute value
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
allow_blank = options[:allow_blank] || false
|
16
|
-
return if allow_blank && value.blank?
|
17
|
-
|
18
|
-
unless self.class.valid?(value, options)
|
19
|
-
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.longitude'))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
8
|
+
class LongitudeValidator < BaseValidator
|
23
9
|
private
|
24
10
|
|
25
11
|
def self.valid?(longitude, options)
|
26
12
|
longitude.present? && longitude >= -180 && longitude <= 180
|
27
13
|
end
|
28
|
-
|
29
14
|
end
|
@@ -5,21 +5,7 @@
|
|
5
5
|
# attr_accessor :mac
|
6
6
|
# validates :mac, mac_address: true
|
7
7
|
# end
|
8
|
-
class MacAddressValidator <
|
9
|
-
# Checks if an attribute value is a valid MAC address.
|
10
|
-
#
|
11
|
-
# @param [Object] record object to validate
|
12
|
-
# @param [String] attribute name of the object attribute to validate
|
13
|
-
# @param [Object] value attribute value
|
14
|
-
def validate_each(record, attribute, value)
|
15
|
-
allow_blank = options[:allow_blank] || false
|
16
|
-
return if allow_blank && value.blank?
|
17
|
-
|
18
|
-
unless self.class.valid?(value, options)
|
19
|
-
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.mac_address'))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
8
|
+
class MacAddressValidator < BaseValidator
|
23
9
|
def self.validate_format(mac_address)
|
24
10
|
!!(mac_address =~ /^([\h]{2}:){5}[\h]{2}?$/i) || # 08:00:2b:01:02:03
|
25
11
|
!!(mac_address =~ /^([\h]{2}[-|\.|\s]){5}[\h]{2}?$/i) || # 08-00-2b-01-02-03 or 08.00.2b.01.02.03
|
@@ -34,5 +20,4 @@ class MacAddressValidator < ActiveModel::EachValidator
|
|
34
20
|
def self.valid?(mac_address, options)
|
35
21
|
validate_format(mac_address)
|
36
22
|
end
|
37
|
-
|
38
23
|
end
|
data/missing_validators.gemspec
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EqualityValidator do
|
4
|
+
|
5
|
+
describe do
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveModel::Validations
|
9
|
+
attr_accessor :attr
|
10
|
+
validates :attr, equality: { to: Proc.new { |o| 'valid value' } }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject(:model){ klass.new }
|
15
|
+
|
16
|
+
specify "field is not the same as the result of the validating proc" do
|
17
|
+
model.attr = "invalid value"
|
18
|
+
expect(model).to be_invalid
|
19
|
+
end
|
20
|
+
|
21
|
+
specify "field is the same as the result of the validating proc" do
|
22
|
+
model.attr = "valid value"
|
23
|
+
expect(model).to be_valid
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe do
|
28
|
+
let(:klass) do
|
29
|
+
Class.new do
|
30
|
+
include ActiveModel::Validations
|
31
|
+
attr_accessor :origin, :destination, :airline
|
32
|
+
validates :origin, equality: { to: :destination }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
subject(:model){ klass.new }
|
37
|
+
|
38
|
+
it { should ensure_equality_of(:origin).to(:destination) }
|
39
|
+
it { should_not ensure_equality_of(:origin).to(:airline) }
|
40
|
+
|
41
|
+
specify "both fields have same values" do
|
42
|
+
model.origin = model.destination = "MOW"
|
43
|
+
expect(model).to be_valid
|
44
|
+
end
|
45
|
+
|
46
|
+
specify "fields have different value" do
|
47
|
+
model.origin = "NYC"
|
48
|
+
model.destination = "MOW"
|
49
|
+
expect(model).to be_invalid
|
50
|
+
end
|
51
|
+
|
52
|
+
specify "first field has value, the second is nil" do
|
53
|
+
model.origin = "NYC"
|
54
|
+
model.destination = nil
|
55
|
+
expect(model).to be_invalid
|
56
|
+
end
|
57
|
+
|
58
|
+
specify "first field is nil, the second has value" do
|
59
|
+
model.origin = nil
|
60
|
+
model.destination = "NYC"
|
61
|
+
expect(model).to be_invalid
|
62
|
+
end
|
63
|
+
|
64
|
+
specify "both fields are nil" do
|
65
|
+
model.origin = model.destination = nil
|
66
|
+
expect(model).to be_valid
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -1,44 +1,68 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe InequalityValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
describe do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
attr_accessor :attr
|
9
|
+
validates :attr, inequality: { to: Proc.new { |o| "invalid value" } }
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
+
subject(:model){ klass.new }
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
specify "field is the same as the result of the validating proc" do
|
16
|
+
model.attr = "invalid value"
|
17
|
+
expect(model).to be_invalid
|
18
|
+
end
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
specify "field is not the same as the result of the validating proc" do
|
21
|
+
model.attr = "valid value"
|
22
|
+
expect(model).to be_valid
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
describe do
|
27
|
+
let(:klass) do
|
28
|
+
Class.new do
|
29
|
+
include ActiveModel::Validations
|
30
|
+
attr_accessor :origin, :destination, :airline
|
31
|
+
validates :origin, inequality: { to: :destination }
|
32
|
+
end
|
33
|
+
end
|
27
34
|
|
28
|
-
|
29
|
-
model.origin = "NYC"
|
30
|
-
model.destination = nil
|
31
|
-
expect(model).to be_valid
|
32
|
-
end
|
35
|
+
subject(:model){ klass.new }
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
it { should ensure_inequality_of(:origin).to(:destination) }
|
38
|
+
it { should_not ensure_inequality_of(:origin).to(:airline) }
|
39
|
+
|
40
|
+
specify "both fields have same values" do
|
41
|
+
model.origin = model.destination = "MOW"
|
42
|
+
expect(model).to be_invalid
|
43
|
+
end
|
44
|
+
|
45
|
+
specify "fields have different value" do
|
46
|
+
model.origin = "NYC"
|
47
|
+
model.destination = "MOW"
|
48
|
+
expect(model).to be_valid
|
49
|
+
end
|
50
|
+
|
51
|
+
specify "first field has value, the second is nil" do
|
52
|
+
model.origin = "NYC"
|
53
|
+
model.destination = nil
|
54
|
+
expect(model).to be_valid
|
55
|
+
end
|
56
|
+
|
57
|
+
specify "first field is nil, the second has value" do
|
58
|
+
model.origin = nil
|
59
|
+
model.destination = "NYC"
|
60
|
+
expect(model).to be_valid
|
61
|
+
end
|
39
62
|
|
40
|
-
|
41
|
-
|
42
|
-
|
63
|
+
specify "both fields are nil" do
|
64
|
+
model.origin = model.destination = nil
|
65
|
+
expect(model).to be_invalid
|
66
|
+
end
|
43
67
|
end
|
44
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: missing_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Gridnev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codeclimate-test-reporter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Validates email addresses, URLs, IMEI, MAC addresses, latitude, longitude,
|
70
84
|
hex colors and inequality of attributes.
|
71
85
|
email:
|
@@ -83,13 +97,16 @@ files:
|
|
83
97
|
- Rakefile
|
84
98
|
- config/locales/en.yml
|
85
99
|
- lib/missing_validators.rb
|
100
|
+
- lib/missing_validators/matchers/ensure_equality_of_matcher.rb
|
86
101
|
- lib/missing_validators/matchers/ensure_inequality_of_matcher.rb
|
87
102
|
- lib/missing_validators/matchers/ensure_valid_email_format_of.rb
|
88
103
|
- lib/missing_validators/matchers/ensure_valid_imei_format_of.rb
|
89
104
|
- lib/missing_validators/matchers/ensure_valid_mac_address_format_of.rb
|
90
105
|
- lib/missing_validators/matchers/ensure_valid_url_format_of.rb
|
106
|
+
- lib/missing_validators/validators/base_validator.rb
|
91
107
|
- lib/missing_validators/validators/color_validator.rb
|
92
108
|
- lib/missing_validators/validators/email_validator.rb
|
109
|
+
- lib/missing_validators/validators/equality_validator.rb
|
93
110
|
- lib/missing_validators/validators/imei_validator.rb
|
94
111
|
- lib/missing_validators/validators/inequality_validator.rb
|
95
112
|
- lib/missing_validators/validators/latitude_validator.rb
|
@@ -101,6 +118,7 @@ files:
|
|
101
118
|
- spec/spec_helper.rb
|
102
119
|
- spec/validators/color_validator_spec.rb
|
103
120
|
- spec/validators/email_validator_spec.rb
|
121
|
+
- spec/validators/equality_validator_spec.rb
|
104
122
|
- spec/validators/imei_spec.rb
|
105
123
|
- spec/validators/inequality_validator_spec.rb
|
106
124
|
- spec/validators/latitude_validator_spec.rb
|
@@ -135,6 +153,7 @@ test_files:
|
|
135
153
|
- spec/spec_helper.rb
|
136
154
|
- spec/validators/color_validator_spec.rb
|
137
155
|
- spec/validators/email_validator_spec.rb
|
156
|
+
- spec/validators/equality_validator_spec.rb
|
138
157
|
- spec/validators/imei_spec.rb
|
139
158
|
- spec/validators/inequality_validator_spec.rb
|
140
159
|
- spec/validators/latitude_validator_spec.rb
|