missing_validators 0.6.3 → 0.7.0
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.
- checksums.yaml +7 -0
- data/README.md +32 -0
- data/config/locales/en.yml +4 -0
- data/lib/missing_validators/matchers/ensure_valid_mac_address_format_of.rb +20 -0
- data/lib/missing_validators/validators/mac_address_validator.rb +37 -0
- data/lib/missing_validators/version.rb +1 -1
- data/lib/missing_validators.rb +2 -0
- data/missing_validators.gemspec +1 -1
- data/spec/validators/mac_address_spec.rb +33 -0
- metadata +38 -24
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3a753662765a4f524c5b48aa7319306c92be6723
|
4
|
+
data.tar.gz: 6728c6fcfeeb1e4e10fe55170de856997f146064
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1bec22de6521794354b4e3eaba07352523045e948ad31525c69c4e638ca85e13273f3312d3ad56574b6c4bffdb4d30140ffcafd7dba0fe0c0736f756f1fa4a8
|
7
|
+
data.tar.gz: c63f5eab4501058232e58300fc81e8fded0443289bba356018ef0affe597cc8b705f575062c84fd96a1446bf954e50228e576cb8881464c48386c7c311227574
|
data/README.md
CHANGED
@@ -111,6 +111,38 @@ RSpec matcher is also available for your convenience:
|
|
111
111
|
it { should ensure_inequality_of(:origin).to(:destination) }
|
112
112
|
end
|
113
113
|
|
114
|
+
### MacAddressValidator
|
115
|
+
|
116
|
+
Ensures that MAC address is in one of the following formats:
|
117
|
+
|
118
|
+
'08:00:2b:01:02:03'
|
119
|
+
'08-00-2b-01-02-03'
|
120
|
+
'08002b:010203'
|
121
|
+
'08002b-010203'
|
122
|
+
'0800.2b01.0203'
|
123
|
+
'08002b010203'
|
124
|
+
|
125
|
+
With an ActiveRecord model:
|
126
|
+
|
127
|
+
class Device < ActiveRecord::Base
|
128
|
+
attr_accessor :mac
|
129
|
+
validates :mac, mac_address: true
|
130
|
+
end
|
131
|
+
|
132
|
+
Or any ruby class:
|
133
|
+
|
134
|
+
class Device
|
135
|
+
include ActiveModel::Validations
|
136
|
+
attr_accessor :mac
|
137
|
+
validates :mac, mac_address: true
|
138
|
+
end
|
139
|
+
|
140
|
+
RSpec matcher is also available for your convenience:
|
141
|
+
|
142
|
+
describe Device do
|
143
|
+
it { should ensure_valid_mac_address_format_of }
|
144
|
+
end
|
145
|
+
|
114
146
|
## Contributing
|
115
147
|
|
116
148
|
Your contribution is welcome.
|
data/config/locales/en.yml
CHANGED
@@ -4,6 +4,7 @@ en:
|
|
4
4
|
inequality: "can't be equal to %{attr}"
|
5
5
|
email: "isn't a valid email address"
|
6
6
|
url: "isn't a valid URL"
|
7
|
+
mac_address: "isn't a valid MAC address"
|
7
8
|
missing_validators:
|
8
9
|
matchers:
|
9
10
|
ensure_inequality_of:
|
@@ -15,3 +16,6 @@ en:
|
|
15
16
|
ensure_valid_url_format_of:
|
16
17
|
failure_message_for_should: "#{model} should ensure valid URL format of attribute #{attr}"
|
17
18
|
failure_message_for_should_not: "#{model} should not ensure valid URL format of attribute #{attr}"
|
19
|
+
ensure_valid_mac_address_format_of:
|
20
|
+
failure_message_for_should: "#{model} should ensure valid MAC address format of attribute #{attr}"
|
21
|
+
failure_message_for_should_not: "#{model} should not ensure valid MAC address format of attribute #{attr}"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_mac_address_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "invalid.mac.address")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.mac_address'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should do |model|
|
12
|
+
I18n.t 'missing_validators.matchers.ensure_valid_mac_address_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_for_should_not do |model|
|
17
|
+
I18n.t 'missing_validators.matchers.ensure_valid_mac_address_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Allows to check if the value of a specific attribute is a valid MAC address.
|
2
|
+
#
|
3
|
+
# @example Validate that the device MAC address is valid.
|
4
|
+
# class Device << ActiveRecord::Base
|
5
|
+
# attr_accessor :mac
|
6
|
+
# validates :mac, mac_address: true
|
7
|
+
# end
|
8
|
+
class MacAddressValidator < ActiveModel::EachValidator
|
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 valid?(value, options)
|
19
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.mac_address'))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def valid?(mac_address, options)
|
26
|
+
validate_format(mac_address)
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_format(mac_address)
|
30
|
+
!!(mac_address =~ /^([A-Fa-f0-9]{2}[:]){5}[A-Fa-f0-9]{2}?$/i) || # 08:00:2b:01:02:03
|
31
|
+
!!(mac_address =~ /^([A-Fa-f0-9]{2}[-]){5}[A-Fa-f0-9]{2}?$/i) || # 08-00-2b-01-02-03
|
32
|
+
!!(mac_address =~ /^([A-Fa-f0-9]{6}):[A-Fa-f0-9]{6}?$/i) || # 08002b:010203
|
33
|
+
!!(mac_address =~ /^([A-Fa-f0-9]{6})-[A-Fa-f0-9]{6}?$/i) || # 08002b-010203
|
34
|
+
!!(mac_address =~ /^([A-Fa-f0-9]{4}[\.]){2}[A-Fa-f0-9]{4}?$/i) || # 0800.2b01.0203
|
35
|
+
!!(mac_address =~ /^[A-Fa-f0-9]{12}?$/i) # 08002b010203
|
36
|
+
end
|
37
|
+
end
|
data/lib/missing_validators.rb
CHANGED
@@ -6,3 +6,5 @@ require 'missing_validators/validators/email_validator'
|
|
6
6
|
require 'missing_validators/matchers/ensure_valid_email_format_of' if defined?(RSpec)
|
7
7
|
require 'missing_validators/validators/url_validator'
|
8
8
|
require 'missing_validators/matchers/ensure_valid_url_format_of' if defined?(RSpec)
|
9
|
+
require 'missing_validators/validators/mac_address_validator'
|
10
|
+
require 'missing_validators/matchers/ensure_valid_mac_address_format_of' if defined?(RSpec)
|
data/missing_validators.gemspec
CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/missing_validators/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Andrew Gridnev"]
|
6
6
|
gem.email = ["andrew.gridnev@gmail.com"]
|
7
|
-
gem.description = %q{Validates email addresses, URLs, and inequality of attributes.}
|
8
7
|
gem.summary = %q{Adds some handy validators.}
|
8
|
+
gem.description = %q{Validates email addresses, URLs, MAC addresses and inequality of attributes.}
|
9
9
|
gem.homepage = "https://github.com/andrewgr/missing_validators/"
|
10
10
|
gem.license = 'MIT'
|
11
11
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MacAddressValidator do
|
4
|
+
context "MAC address has valid format" do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
attr_accessor :mac, :name
|
9
|
+
validates :mac, mac_address: true
|
10
|
+
end
|
11
|
+
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
|
+
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("08002b:010203").for(:mac) }
|
21
|
+
it { should allow_value("08002b-010203").for(:mac) }
|
22
|
+
it { should allow_value("0800.2b01.0203").for(:mac) }
|
23
|
+
it { should allow_value("08002b010203").for(:mac) }
|
24
|
+
|
25
|
+
it { should_not allow_value("08:00:2b:01:02").for(:mac) }
|
26
|
+
it { should_not allow_value("08:00:2b:01:02:03:04").for(:mac) }
|
27
|
+
it { should_not allow_value("qq:00:00:00:00:00").for(:mac) }
|
28
|
+
|
29
|
+
it { should_not allow_value("08-00-2b-01-02").for(:mac) }
|
30
|
+
|
31
|
+
it { should_not allow_value("invalid").for(:mac) }
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,61 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: missing_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.7.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Andrew Gridnev
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-12-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: shoulda-matchers
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - '>='
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
36
41
|
- !ruby/object:Gem::Dependency
|
37
42
|
name: activemodel
|
38
|
-
requirement:
|
39
|
-
none: false
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
40
44
|
requirements:
|
41
45
|
- - ~>
|
42
46
|
- !ruby/object:Gem::Version
|
43
47
|
version: 3.0.0
|
44
48
|
type: :runtime
|
45
49
|
prerelease: false
|
46
|
-
version_requirements:
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
47
55
|
- !ruby/object:Gem::Dependency
|
48
56
|
name: activesupport
|
49
|
-
requirement:
|
50
|
-
none: false
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
51
58
|
requirements:
|
52
59
|
- - ~>
|
53
60
|
- !ruby/object:Gem::Version
|
54
61
|
version: 3.0.0
|
55
62
|
type: :runtime
|
56
63
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
description: Validates email addresses, URLs, MAC addresses and inequality of attributes.
|
59
70
|
email:
|
60
71
|
- andrew.gridnev@gmail.com
|
61
72
|
executables: []
|
@@ -73,43 +84,46 @@ files:
|
|
73
84
|
- lib/missing_validators.rb
|
74
85
|
- lib/missing_validators/matchers/ensure_inequality_of_matcher.rb
|
75
86
|
- lib/missing_validators/matchers/ensure_valid_email_format_of.rb
|
87
|
+
- lib/missing_validators/matchers/ensure_valid_mac_address_format_of.rb
|
76
88
|
- lib/missing_validators/matchers/ensure_valid_url_format_of.rb
|
77
89
|
- lib/missing_validators/validators/email_validator.rb
|
78
90
|
- lib/missing_validators/validators/inequality_validator.rb
|
91
|
+
- lib/missing_validators/validators/mac_address_validator.rb
|
79
92
|
- lib/missing_validators/validators/url_validator.rb
|
80
93
|
- lib/missing_validators/version.rb
|
81
94
|
- missing_validators.gemspec
|
82
95
|
- spec/spec_helper.rb
|
83
96
|
- spec/validators/email_validator_spec.rb
|
84
97
|
- spec/validators/inequality_validator_spec.rb
|
98
|
+
- spec/validators/mac_address_spec.rb
|
85
99
|
- spec/validators/url_validator_spec.rb
|
86
100
|
homepage: https://github.com/andrewgr/missing_validators/
|
87
101
|
licenses:
|
88
102
|
- MIT
|
103
|
+
metadata: {}
|
89
104
|
post_install_message:
|
90
105
|
rdoc_options: []
|
91
106
|
require_paths:
|
92
107
|
- lib
|
93
108
|
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
109
|
requirements:
|
96
|
-
- -
|
110
|
+
- - '>='
|
97
111
|
- !ruby/object:Gem::Version
|
98
112
|
version: '0'
|
99
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
-
none: false
|
101
114
|
requirements:
|
102
|
-
- -
|
115
|
+
- - '>='
|
103
116
|
- !ruby/object:Gem::Version
|
104
117
|
version: '0'
|
105
118
|
requirements: []
|
106
119
|
rubyforge_project:
|
107
|
-
rubygems_version: 1.
|
120
|
+
rubygems_version: 2.1.11
|
108
121
|
signing_key:
|
109
|
-
specification_version:
|
122
|
+
specification_version: 4
|
110
123
|
summary: Adds some handy validators.
|
111
124
|
test_files:
|
112
125
|
- spec/spec_helper.rb
|
113
126
|
- spec/validators/email_validator_spec.rb
|
114
127
|
- spec/validators/inequality_validator_spec.rb
|
128
|
+
- spec/validators/mac_address_spec.rb
|
115
129
|
- spec/validators/url_validator_spec.rb
|