missing_validators 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +7 -11
- data/Rakefile +9 -3
- data/config/locales/en.yml +2 -8
- data/lib/missing_validators.rb +10 -8
- data/lib/missing_validators/matchers/ensure_valid_email_format_of.rb +6 -6
- data/lib/missing_validators/matchers/ensure_valid_imei_format_of.rb +6 -6
- data/lib/missing_validators/matchers/ensure_valid_mac_address_format_of.rb +6 -6
- data/lib/missing_validators/matchers/ensure_valid_url_format_of.rb +6 -6
- data/lib/missing_validators/validators/base_validator.rb +17 -6
- data/lib/missing_validators/validators/color_validator.rb +5 -7
- data/lib/missing_validators/validators/email_validator.rb +20 -10
- data/lib/missing_validators/validators/equality_validator.rb +18 -12
- data/lib/missing_validators/validators/imei_validator.rb +14 -13
- data/lib/missing_validators/validators/inequality_validator.rb +8 -24
- data/lib/missing_validators/validators/latitude_validator.rb +6 -6
- data/lib/missing_validators/validators/longitude_validator.rb +6 -6
- data/lib/missing_validators/validators/mac_address_validator.rb +12 -12
- data/lib/missing_validators/validators/url_validator.rb +37 -19
- data/lib/missing_validators/version.rb +3 -3
- data/missing_validators.gemspec +17 -13
- data/spec/spec_helper.rb +3 -3
- data/spec/validators/color_validator_spec.rb +11 -13
- data/spec/validators/email_validator_spec.rb +31 -21
- data/spec/validators/equality_validator_spec.rb +16 -29
- data/spec/validators/imei_spec.rb +25 -27
- data/spec/validators/inequality_validator_spec.rb +16 -28
- data/spec/validators/latitude_validator_spec.rb +15 -17
- data/spec/validators/longitude_validator_spec.rb +15 -17
- data/spec/validators/mac_address_spec.rb +42 -45
- data/spec/validators/url_validator_spec.rb +44 -44
- metadata +85 -12
- data/lib/missing_validators/matchers/ensure_equality_of_matcher.rb +0 -27
- data/lib/missing_validators/matchers/ensure_inequality_of_matcher.rb +0 -27
@@ -1,39 +1,37 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ImeiValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
validates :imei, imei: true
|
10
|
-
end
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :imei, :name
|
8
|
+
validates :imei, imei: true
|
11
9
|
end
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
it { should ensure_valid_imei_format_of(:imei) }
|
16
|
-
it { should_not ensure_valid_imei_format_of(:name) }
|
12
|
+
subject { klass.new }
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
it { should allow_value("35-684305-2637512").for(:imei) }
|
21
|
-
it { should allow_value("35-684305.263.7512").for(:imei) }
|
14
|
+
it { should ensure_valid_imei_format_of(:imei) }
|
15
|
+
it { should_not ensure_valid_imei_format_of(:name) }
|
22
16
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
context 'value is valid' do
|
18
|
+
it { should allow_value(356_843_052_637_512).for(:imei) }
|
19
|
+
it { should allow_value('356843052637512').for(:imei) }
|
20
|
+
it { should allow_value('35-684305-2637512').for(:imei) }
|
21
|
+
it { should allow_value('35-684305.263.7512').for(:imei) }
|
22
|
+
end
|
27
23
|
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
context 'value too short' do
|
25
|
+
it { should_not allow_value('3568430537512').for(:imei) }
|
26
|
+
it { should_not allow_value('3').for(:imei) }
|
27
|
+
end
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
29
|
+
context 'value is too long' do
|
30
|
+
it { should_not allow_value('35684305263751233').for(:imei) }
|
31
|
+
end
|
36
32
|
|
37
|
-
|
33
|
+
context 'luhn checksum does not match' do
|
34
|
+
it { should_not allow_value('356843052637513').for(:imei) }
|
35
|
+
it { should_not allow_value('156843052637512').for(:imei) }
|
38
36
|
end
|
39
37
|
end
|
@@ -1,26 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe InequalityValidator do
|
4
|
+
subject(:model) { klass.new }
|
5
|
+
|
4
6
|
describe do
|
5
7
|
let(:klass) do
|
6
8
|
Class.new do
|
7
9
|
include ActiveModel::Validations
|
8
10
|
attr_accessor :attr
|
9
|
-
validates :attr, inequality: { to:
|
11
|
+
validates :attr, inequality: { to: 'invalid value' }
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
13
|
-
|
14
|
-
|
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
|
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
|
15
|
+
it { should allow_value('valid value').for(:attr) }
|
16
|
+
it { should_not allow_value('invalid value').for(:attr) }
|
24
17
|
end
|
25
18
|
|
26
19
|
describe do
|
@@ -28,39 +21,34 @@ describe InequalityValidator do
|
|
28
21
|
Class.new do
|
29
22
|
include ActiveModel::Validations
|
30
23
|
attr_accessor :origin, :destination, :airline
|
31
|
-
validates :origin, inequality: { to:
|
24
|
+
validates :origin, inequality: { to: ->(o) { o.destination } }
|
32
25
|
end
|
33
26
|
end
|
34
27
|
|
35
|
-
|
36
|
-
|
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"
|
28
|
+
specify 'both fields have same values' do
|
29
|
+
model.origin = model.destination = 'MOW'
|
42
30
|
expect(model).to be_invalid
|
43
31
|
end
|
44
32
|
|
45
|
-
specify
|
46
|
-
model.origin =
|
47
|
-
model.destination =
|
33
|
+
specify 'fields have different value' do
|
34
|
+
model.origin = 'NYC'
|
35
|
+
model.destination = 'MOW'
|
48
36
|
expect(model).to be_valid
|
49
37
|
end
|
50
38
|
|
51
|
-
specify
|
52
|
-
model.origin =
|
39
|
+
specify 'first field has value, the second is nil' do
|
40
|
+
model.origin = 'NYC'
|
53
41
|
model.destination = nil
|
54
42
|
expect(model).to be_valid
|
55
43
|
end
|
56
44
|
|
57
|
-
specify
|
45
|
+
specify 'first field is nil, the second has value' do
|
58
46
|
model.origin = nil
|
59
|
-
model.destination =
|
47
|
+
model.destination = 'NYC'
|
60
48
|
expect(model).to be_valid
|
61
49
|
end
|
62
50
|
|
63
|
-
specify
|
51
|
+
specify 'both fields are nil' do
|
64
52
|
model.origin = model.destination = nil
|
65
53
|
expect(model).to be_invalid
|
66
54
|
end
|
@@ -1,26 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe LatitudeValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
validates :lat, latitude: true
|
10
|
-
end
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :lat
|
8
|
+
validates :lat, latitude: true
|
11
9
|
end
|
10
|
+
end
|
12
11
|
|
13
|
-
|
12
|
+
subject { klass.new }
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
it { should allow_value(-90).for(:lat) }
|
15
|
+
it { should allow_value(90).for(:lat) }
|
16
|
+
it { should allow_value(0).for(:lat) }
|
17
|
+
it { should allow_value(9.33).for(:lat) }
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
it { should_not allow_value(-90.1).for(:lat) }
|
20
|
+
it { should_not allow_value(90.1).for(:lat) }
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
end
|
22
|
+
it { should_not allow_value(nil).for(:lat) }
|
23
|
+
it { should_not allow_value('').for(:lat) }
|
26
24
|
end
|
@@ -1,26 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe LongitudeValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
validates :lon, longitude: true
|
10
|
-
end
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :lon
|
8
|
+
validates :lon, longitude: true
|
11
9
|
end
|
10
|
+
end
|
12
11
|
|
13
|
-
|
12
|
+
subject { klass.new }
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
it { should allow_value(-180).for(:lon) }
|
15
|
+
it { should allow_value(180).for(:lon) }
|
16
|
+
it { should allow_value(0).for(:lon) }
|
17
|
+
it { should allow_value(9.33).for(:lon) }
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
it { should_not allow_value(-181.1).for(:lon) }
|
20
|
+
it { should_not allow_value(181.1).for(:lon) }
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
end
|
22
|
+
it { should_not allow_value(nil).for(:lon) }
|
23
|
+
it { should_not allow_value('').for(:lon) }
|
26
24
|
end
|
@@ -1,51 +1,48 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MacAddressValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
validates :mac, mac_address: true
|
10
|
-
end
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :mac, :name
|
8
|
+
validates :mac, mac_address: true
|
11
9
|
end
|
12
|
-
|
13
|
-
subject { klass.new }
|
14
|
-
|
15
|
-
it { should ensure_valid_mac_address_format_of(:mac) }
|
16
|
-
it { should_not ensure_valid_mac_address_format_of(:name) }
|
17
|
-
|
18
|
-
# Valid formats
|
19
|
-
it { should allow_value("08:00:2b:01:02:03").for(:mac) }
|
20
|
-
it { should allow_value("08-00-2b-01-02-03").for(:mac) }
|
21
|
-
it { should allow_value("08.00.2b.01.02.03").for(:mac) }
|
22
|
-
it { should allow_value("08 00 2b 01 02 03").for(:mac) }
|
23
|
-
it { should allow_value("08002b:010203").for(:mac) }
|
24
|
-
it { should allow_value("08002b.010203").for(:mac) }
|
25
|
-
it { should allow_value("08002b-010203").for(:mac) }
|
26
|
-
it { should allow_value("0800.2b01.0203").for(:mac) }
|
27
|
-
it { should allow_value("0800-2b01-0203").for(:mac) }
|
28
|
-
it { should allow_value("0800 2b01 0203").for(:mac) }
|
29
|
-
it { should allow_value("08002b010203").for(:mac) }
|
30
|
-
|
31
|
-
# Mixed Separators
|
32
|
-
it { should_not allow_value("08-00:2b:01:02:03").for(:mac) }
|
33
|
-
it { should_not allow_value("08.00:2b:01:02:03").for(:mac) }
|
34
|
-
it { should_not allow_value("08 00:2b:01:02:03").for(:mac) }
|
35
|
-
it { should_not allow_value("0800-2b01:0203").for(:mac) }
|
36
|
-
it { should_not allow_value("0800 2b01:0203").for(:mac) }
|
37
|
-
|
38
|
-
# Too Short
|
39
|
-
it { should_not allow_value("08:00:2b:01:02").for(:mac) }
|
40
|
-
it { should_not allow_value("08-00-2b-01-02").for(:mac) }
|
41
|
-
|
42
|
-
# Too Long
|
43
|
-
it { should_not allow_value("08:00:2b:01:02:03:04").for(:mac) }
|
44
|
-
|
45
|
-
# Non-Hex Characters
|
46
|
-
it { should_not allow_value("qq:00:00:00:00:00").for(:mac) }
|
47
|
-
|
48
|
-
|
49
|
-
it { should_not allow_value("invalid").for(:mac) }
|
50
10
|
end
|
11
|
+
|
12
|
+
subject { klass.new }
|
13
|
+
|
14
|
+
it { should ensure_valid_mac_address_format_of(:mac) }
|
15
|
+
it { should_not ensure_valid_mac_address_format_of(:name) }
|
16
|
+
|
17
|
+
# Valid formats
|
18
|
+
it { should allow_value('08:00:2b:01:02:03').for(:mac) }
|
19
|
+
it { should allow_value('08-00-2B-01-02-03').for(:mac) }
|
20
|
+
it { should allow_value('08.00.2b.01.02.03').for(:mac) }
|
21
|
+
it { should allow_value('08 00 2B 01 02 03').for(:mac) }
|
22
|
+
it { should allow_value('08002b:010203').for(:mac) }
|
23
|
+
it { should allow_value('08002B.010203').for(:mac) }
|
24
|
+
it { should allow_value('08002b-010203').for(:mac) }
|
25
|
+
it { should allow_value('0800.2b01.0203').for(:mac) }
|
26
|
+
it { should allow_value('0800-2B01-0203').for(:mac) }
|
27
|
+
it { should allow_value('0800 2b01 0203').for(:mac) }
|
28
|
+
it { should allow_value('08002b010203').for(:mac) }
|
29
|
+
|
30
|
+
# Mixed Separators
|
31
|
+
it { should_not allow_value('08-00:2b:01:02:03').for(:mac) }
|
32
|
+
it { should_not allow_value('08.00:2b:01:02:03').for(:mac) }
|
33
|
+
it { should_not allow_value('08 00:2b:01:02:03').for(:mac) }
|
34
|
+
it { should_not allow_value('0800-2b01:0203').for(:mac) }
|
35
|
+
it { should_not allow_value('0800 2B01:0203').for(:mac) }
|
36
|
+
|
37
|
+
# Too Short
|
38
|
+
it { should_not allow_value('08:00:2b:01:02').for(:mac) }
|
39
|
+
it { should_not allow_value('08-00-2B-01-02').for(:mac) }
|
40
|
+
|
41
|
+
# Too Long
|
42
|
+
it { should_not allow_value('08:00:2b:01:02:03:04').for(:mac) }
|
43
|
+
|
44
|
+
# Non-Hex Characters
|
45
|
+
it { should_not allow_value('qq:00:00:00:00:00').for(:mac) }
|
46
|
+
|
47
|
+
it { should_not allow_value('invalid').for(:mac) }
|
51
48
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe UrlValidator do
|
4
|
-
context
|
4
|
+
context 'url has valid format' do
|
5
5
|
let(:klass) do
|
6
6
|
Class.new do
|
7
7
|
include ActiveModel::Validations
|
@@ -15,28 +15,29 @@ describe UrlValidator do
|
|
15
15
|
it { should ensure_valid_url_format_of(:url) }
|
16
16
|
it { should_not ensure_valid_url_format_of(:name) }
|
17
17
|
|
18
|
-
it { should allow_value(
|
19
|
-
it { should allow_value(
|
20
|
-
it { should allow_value(
|
21
|
-
it { should allow_value(
|
22
|
-
it { should allow_value(
|
23
|
-
it { should allow_value(
|
24
|
-
it { should allow_value(
|
25
|
-
it { should allow_value(
|
26
|
-
it { should allow_value(
|
27
|
-
it { should allow_value(
|
28
|
-
it { should allow_value(
|
29
|
-
it { should allow_value(
|
30
|
-
it { should allow_value(
|
31
|
-
|
32
|
-
|
33
|
-
it { should_not allow_value(
|
34
|
-
it { should_not allow_value(
|
35
|
-
it { should_not allow_value(
|
36
|
-
it { should_not allow_value(
|
18
|
+
it { should allow_value('http://example.com').for(:url) }
|
19
|
+
it { should allow_value('http://FooBar.cOm').for(:url) }
|
20
|
+
it { should allow_value('http://foo.bar.baz.com').for(:url) }
|
21
|
+
it { should allow_value('http://123.com').for(:url) }
|
22
|
+
it { should allow_value('http://www.example.ru').for(:url) }
|
23
|
+
it { should allow_value('http://user-example.co.uk').for(:url) }
|
24
|
+
it { should allow_value('https://example.com').for(:url) }
|
25
|
+
it { should allow_value('http://example.org/').for(:url) }
|
26
|
+
it { should allow_value('https://example.net/index.html').for(:url) }
|
27
|
+
it { should allow_value('http://example.net/login.php').for(:url) }
|
28
|
+
it { should allow_value('https://example.travel/').for(:url) }
|
29
|
+
it { should allow_value('http://example.aero').for(:url) }
|
30
|
+
it { should allow_value('http://example.aero?foo=bar').for(:url) }
|
31
|
+
it { should allow_value('http://user_example.com').for(:url) }
|
32
|
+
|
33
|
+
it { should_not allow_value('http://user_examplecom').for(:url) }
|
34
|
+
it { should_not allow_value('http://user example.com').for(:url) }
|
35
|
+
it { should_not allow_value('http://user_example.a').for(:url) }
|
36
|
+
it { should_not allow_value(':').for(:url) }
|
37
|
+
it { should_not allow_value('.').for(:url) }
|
37
38
|
end
|
38
39
|
|
39
|
-
describe
|
40
|
+
describe 'url must be in a specific domain' do
|
40
41
|
let(:klass) do
|
41
42
|
Class.new do
|
42
43
|
include ActiveModel::Validations
|
@@ -48,17 +49,17 @@ describe UrlValidator do
|
|
48
49
|
|
49
50
|
subject { klass.new }
|
50
51
|
|
51
|
-
it { should allow_value(
|
52
|
-
it { should_not allow_value(
|
52
|
+
it { should allow_value('http://example.org').for(:url1) }
|
53
|
+
it { should_not allow_value('http://example.com').for(:url1) }
|
53
54
|
|
54
|
-
it { should allow_value(
|
55
|
-
it { should allow_value(
|
56
|
-
it { should allow_value(
|
57
|
-
it { should allow_value(
|
58
|
-
it { should_not allow_value(
|
55
|
+
it { should allow_value('http://example.org').for(:url2) }
|
56
|
+
it { should allow_value('http://example.edu').for(:url2) }
|
57
|
+
it { should allow_value('http://example.com.au').for(:url2) }
|
58
|
+
it { should allow_value('http://example.Com.Au').for(:url2) }
|
59
|
+
it { should_not allow_value('http://example.com').for(:url2) }
|
59
60
|
end
|
60
61
|
|
61
|
-
describe
|
62
|
+
describe 'url must be domain root' do
|
62
63
|
let(:klass) do
|
63
64
|
Class.new do
|
64
65
|
include ActiveModel::Validations
|
@@ -70,18 +71,17 @@ describe UrlValidator do
|
|
70
71
|
|
71
72
|
subject { klass.new }
|
72
73
|
|
73
|
-
it { should allow_value(
|
74
|
-
it { should allow_value(
|
75
|
-
it { should_not allow_value(
|
76
|
-
it { should_not allow_value(
|
77
|
-
it { should_not allow_value(
|
74
|
+
it { should allow_value('http://example.org').for(:url1) }
|
75
|
+
it { should allow_value('http://example.org/').for(:url1) }
|
76
|
+
it { should_not allow_value('http://example.com/test').for(:url1) }
|
77
|
+
it { should_not allow_value('http://example.com/#fragment').for(:url1) }
|
78
|
+
it { should_not allow_value('http://example.com/?key=value').for(:url1) }
|
78
79
|
|
79
|
-
|
80
|
-
it { should allow_value(
|
81
|
-
it { should allow_value("http://example.org/lorem").for(:url2) }
|
80
|
+
it { should allow_value('http://example.org').for(:url2) }
|
81
|
+
it { should allow_value('http://example.org/lorem').for(:url2) }
|
82
82
|
end
|
83
83
|
|
84
|
-
describe
|
84
|
+
describe 'url must have a specific scheme' do
|
85
85
|
let(:klass) do
|
86
86
|
Class.new do
|
87
87
|
include ActiveModel::Validations
|
@@ -93,12 +93,12 @@ describe UrlValidator do
|
|
93
93
|
|
94
94
|
subject { klass.new }
|
95
95
|
|
96
|
-
it { should allow_value(
|
97
|
-
it { should_not allow_value(
|
96
|
+
it { should allow_value('http://example.org').for(:url1) }
|
97
|
+
it { should_not allow_value('https://example.org').for(:url1) }
|
98
98
|
|
99
|
-
it { should allow_value(
|
100
|
-
it { should allow_value(
|
101
|
-
it { should allow_value(
|
102
|
-
it { should_not allow_value(
|
99
|
+
it { should allow_value('http://example.org').for(:url2) }
|
100
|
+
it { should allow_value('https://example.org').for(:url2) }
|
101
|
+
it { should allow_value('HTTPS://example.org').for(:url2) }
|
102
|
+
it { should_not allow_value('ftp://example.org').for(:url2) }
|
103
103
|
end
|
104
104
|
end
|