activemodel-ipaddr_validator 0.0.1
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/.gitignore +34 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +42 -0
- data/Rakefile +5 -0
- data/activemodel-ipaddr_validator.gemspec +22 -0
- data/config/locales/en.yml +4 -0
- data/config/locales/ja.yml +4 -0
- data/lib/activemodel-ipaddr_validator.rb +3 -0
- data/lib/ipaddr_validator.rb +45 -0
- data/lib/ipaddr_validator/engine.rb +6 -0
- data/lib/ipaddr_validator/version.rb +3 -0
- data/spec/ipaddr_validator_spec.rb +244 -0
- data/spec/spec_helper.rb +23 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: be71e5c90ac86d95518c142d9243969badeca1bd
|
4
|
+
data.tar.gz: b8e2b5727b7405ae66420a0c9db5d19c17b38823
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b29d674a86724069d6500a7538f51627df082444e62ffc69fb921cfe7737a284bfc702bb570c87ca499b6d495c0bcd6980b56300de579ffa61d09392f32063b
|
7
|
+
data.tar.gz: 0fca558ff8310aff7dd62a9ec395ad10b4d1775f672f3a87bb8b91b9bd2cb4568c3073d4a967014806eaefa2713e9072d95db082ede5092f9897da6a6a750e3e
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
Gemfile.lock
|
30
|
+
.ruby-version
|
31
|
+
.ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# activemodel-ipaddr_validator [](https://travis-ci.org/increments/activemodel-ipaddr_validator)
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
Add to your Gemfile:
|
6
|
+
|
7
|
+
```rb
|
8
|
+
gem 'activemodel-ipaddr_validator'
|
9
|
+
```
|
10
|
+
|
11
|
+
Run:
|
12
|
+
|
13
|
+
```
|
14
|
+
bundle install
|
15
|
+
```
|
16
|
+
|
17
|
+
Then add the following to your model:
|
18
|
+
|
19
|
+
```rb
|
20
|
+
validates :my_ipaddr_attribute, ipaddr: true
|
21
|
+
```
|
22
|
+
|
23
|
+
### Custom options
|
24
|
+
|
25
|
+
Name | Value | Default | Description
|
26
|
+
--------|---------|---------|-------------------------------------
|
27
|
+
`ipv4` | Boolean | true | Accept IPv4.
|
28
|
+
`ipv6` | Boolean | false | Accept IPv6.
|
29
|
+
`array` | Boolean | false | Expect an array of strings.
|
30
|
+
|
31
|
+
```rb
|
32
|
+
validates :ipv6s_attribute, ipaddr: { array: true, ipv4: false, ipv6: true }
|
33
|
+
serialize :ipv6s_attribute, Array
|
34
|
+
```
|
35
|
+
|
36
|
+
## Validation outside a model
|
37
|
+
|
38
|
+
If you need to validate an IP outside a model, you can do that:
|
39
|
+
|
40
|
+
```rb
|
41
|
+
IpaddrValidator.valid?(value, options)
|
42
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
file = File.open(File.expand_path('../lib/ipaddr_validator/version.rb', __FILE__))
|
2
|
+
version = file.read.scan(/\d+\.\d+\.\d+/).first
|
3
|
+
file.close
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'activemodel-ipaddr_validator'
|
7
|
+
spec.version = version
|
8
|
+
spec.authors = ['Yuku Takahashi']
|
9
|
+
spec.email = ['yuku@qiita.com']
|
10
|
+
spec.summary = 'A IPv4 and IPv6 validator for Rails 3 and 4.'
|
11
|
+
spec.homepage = 'https://github.com/increments/activemodel-ipaddr_validator'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.require_paths = ['lib']
|
16
|
+
|
17
|
+
spec.add_dependency 'activemodel'
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
21
|
+
spec.add_development_dependency 'rspec'
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'ipaddr'
|
2
|
+
|
3
|
+
class IpaddrValidator < ActiveModel::EachValidator
|
4
|
+
class << self
|
5
|
+
def valid?(value, options = {})
|
6
|
+
options = default_options.merge(options)
|
7
|
+
array =
|
8
|
+
if options[:array]
|
9
|
+
return false unless value.is_a?(Array)
|
10
|
+
value
|
11
|
+
else
|
12
|
+
[value]
|
13
|
+
end
|
14
|
+
array.all? do |string|
|
15
|
+
validate_single_ipaddr(string, options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def default_options
|
20
|
+
{ ipv4: true, ipv6: false, array: false }
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def validate_single_ipaddr(string, options)
|
26
|
+
ip = IPAddr.new(string)
|
27
|
+
case
|
28
|
+
when options[:ipv4] && ip.ipv4?
|
29
|
+
true
|
30
|
+
when options[:ipv6] && ip.ipv6?
|
31
|
+
true
|
32
|
+
else
|
33
|
+
false
|
34
|
+
end
|
35
|
+
rescue IPAddr::Error
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate_each(record, attribute, value)
|
41
|
+
unless self.class.valid?(value, options)
|
42
|
+
record.errors.add(attribute, options[:message] || :invalid_ipaddr)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,244 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Shared examples for value.
|
4
|
+
|
5
|
+
shared_examples_for 'valid IPv4 string is given', given: :ipv4 do
|
6
|
+
let(:value) do
|
7
|
+
'192.168.2.0/24'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
shared_examples_for 'valid IPv6 string is given', given: :ipv6 do
|
12
|
+
let(:value) do
|
13
|
+
'3ffe:505:2::1'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
shared_examples_for 'valid IPv4 array is given', given: :ipv4_array do
|
18
|
+
let(:value) do
|
19
|
+
['192.168.2.0/24', '192.168.3.0/24']
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
shared_examples_for 'valid IPv6 array is given', given: :ipv6_array do
|
24
|
+
let(:value) do
|
25
|
+
['3ffe:505:2::1', '3ffe:505:2::2']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
shared_examples_for 'invalid string is given', given: :invalid do
|
30
|
+
let(:value) do
|
31
|
+
'invalid ipaddr'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
shared_examples_for 'invalid array is given', given: :invalid_array do
|
36
|
+
let(:value) do
|
37
|
+
['invalid ipaddr']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Shared examples for options.
|
42
|
+
|
43
|
+
IpaddrValidator.default_options.keys.product([true, false]).each do |option, value|
|
44
|
+
shared_examples_for "#{option} is #{value}", option => value do
|
45
|
+
let(option) do
|
46
|
+
value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe IpaddrValidator do
|
52
|
+
let(:options) do
|
53
|
+
options = {}
|
54
|
+
described_class.default_options.keys.each do |option_name|
|
55
|
+
options[option_name] = send(option_name) if respond_to? option_name
|
56
|
+
end
|
57
|
+
options
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '.valid?' do
|
61
|
+
subject do
|
62
|
+
described_class.valid?(value, options)
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when a valid IPv4 string is given', given: :ipv4 do
|
66
|
+
context 'and ipv4 option is true', ipv4: true do
|
67
|
+
context 'and array option is true', array: true do
|
68
|
+
it { should be_falsey }
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'and array option is false', array: false do
|
72
|
+
it { should be_truthy }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'and ipv4 option is false', ipv4: false do
|
77
|
+
it { should be_falsey }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'when a valid IPv6 string is given', given: :ipv6 do
|
82
|
+
context 'and ipv6 option is true', ipv6: true do
|
83
|
+
context 'and array option is true', array: true do
|
84
|
+
it { should be_falsey }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'and array option is false', array: false do
|
88
|
+
it { should be_truthy }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'and ipv6 option is false', ipv6: false do
|
93
|
+
it { should be_falsey }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'when a valid IPv4 array is given', given: :ipv4_array do
|
98
|
+
context 'and ipv4 option is true', ipv4: true do
|
99
|
+
context 'and array option is true', array: true do
|
100
|
+
it { should be_truthy }
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'and array option is false', array: false do
|
104
|
+
it { should be_falsey }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'and ipv4 option is false', ipv4: false do
|
109
|
+
it { should be_falsey }
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when a valid IPv6 array is given', given: :ipv6_array do
|
114
|
+
context 'and ipv6 option is true', ipv6: true do
|
115
|
+
context 'and array option is true', array: true do
|
116
|
+
it { should be_truthy }
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'and array option is false', array: false do
|
120
|
+
it { should be_falsey }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'and ipv6 option is false', ipv6: false do
|
125
|
+
it { should be_falsey }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'when an invalid string is given', given: :invalid do
|
130
|
+
context 'and array option is true', array: true do
|
131
|
+
it { should be_falsey }
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'and array option is false', array: false do
|
135
|
+
it { should be_falsey }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when an invalid array is given', given: :invalid_array do
|
140
|
+
context 'and array option is true', array: true do
|
141
|
+
it { should be_falsey }
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'and array option is false', array: false do
|
145
|
+
it { should be_falsey }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe 'validation' do
|
151
|
+
subject do
|
152
|
+
model_class.new(attr: value)
|
153
|
+
end
|
154
|
+
|
155
|
+
let(:model_class) do
|
156
|
+
opts = options
|
157
|
+
Class.new(TestModel) do
|
158
|
+
validates :attr, ipaddr: opts
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'when invalid value is given', given: :invalid do
|
163
|
+
it { should be_invalid }
|
164
|
+
end
|
165
|
+
|
166
|
+
context 'when invalid array is given', given: :invalid_array do
|
167
|
+
it { should be_invalid }
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'when default option is used' do
|
171
|
+
let(:options) do
|
172
|
+
true
|
173
|
+
end
|
174
|
+
|
175
|
+
context 'and IPv4 string is given', given: :ipv4 do
|
176
|
+
it { should be_valid }
|
177
|
+
end
|
178
|
+
|
179
|
+
[:ipv4_array, :ipv6, :ipv6_array].each do |given|
|
180
|
+
context "and #{given} is given", given: given do
|
181
|
+
it { should be_invalid }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context(
|
187
|
+
'when ipv4 and ipv6 options are true and false respectively',
|
188
|
+
ipv4: true, ipv6: false
|
189
|
+
) do
|
190
|
+
context 'and array option is true', array: true do
|
191
|
+
context 'and IPv4 array is given', given: :ipv4_array do
|
192
|
+
it { should be_valid }
|
193
|
+
end
|
194
|
+
|
195
|
+
[:ipv4, :ipv6, :ipv6_array].each do |given|
|
196
|
+
context "and #{given} is given", given: given do
|
197
|
+
it { should be_invalid }
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'and array option is false', array: false do
|
203
|
+
context 'and IPv4 string is given', given: :ipv4 do
|
204
|
+
it { should be_valid }
|
205
|
+
end
|
206
|
+
|
207
|
+
[:ipv4_array, :ipv6, :ipv6_array].each do |given|
|
208
|
+
context "and #{given} is given", given: given do
|
209
|
+
it { should be_invalid }
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context(
|
216
|
+
'when ipv4 and ipv6 options are false and true respectively',
|
217
|
+
ipv4: false, ipv6: true
|
218
|
+
) do
|
219
|
+
context 'and array option is true', array: true do
|
220
|
+
context 'and IPv6 array is given', given: :ipv6_array do
|
221
|
+
it { should be_valid }
|
222
|
+
end
|
223
|
+
|
224
|
+
[:ipv4, :ipv4_array, :ipv6].each do |given|
|
225
|
+
context "and #{given} is given", given: given do
|
226
|
+
it { should be_invalid }
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
context 'and array option is false', array: false do
|
232
|
+
context 'and IPv6 string is given', given: :ipv6 do
|
233
|
+
it { should be_valid }
|
234
|
+
end
|
235
|
+
|
236
|
+
[:ipv4, :ipv4_array, :ipv6_array].each do |given|
|
237
|
+
context "and #{given} is given", given: given do
|
238
|
+
it { should be_invalid }
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'ipaddr_validator'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.color = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestModel
|
10
|
+
include ActiveModel::Validations
|
11
|
+
|
12
|
+
def self.name
|
13
|
+
'TestModel'
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(attributes = {})
|
17
|
+
@attributes = attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_attribute_for_validation(key)
|
21
|
+
@attributes[key]
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activemodel-ipaddr_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuku Takahashi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- yuku@qiita.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- activemodel-ipaddr_validator.gemspec
|
82
|
+
- config/locales/en.yml
|
83
|
+
- config/locales/ja.yml
|
84
|
+
- lib/activemodel-ipaddr_validator.rb
|
85
|
+
- lib/ipaddr_validator.rb
|
86
|
+
- lib/ipaddr_validator/engine.rb
|
87
|
+
- lib/ipaddr_validator/version.rb
|
88
|
+
- spec/ipaddr_validator_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: https://github.com/increments/activemodel-ipaddr_validator
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: A IPv4 and IPv6 validator for Rails 3 and 4.
|
114
|
+
test_files: []
|