validates_ip_address 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.
data.tar.gz.sig ADDED
@@ -0,0 +1 @@
1
+ wv���P�!��$ Z��΁1@�֗�WS7^�k9�Iֽyll�Eq����׹: �.P����2{8���l2V���\�N�uLƚ��F8�� wFEB��Z��@�O���?w�"����E�_�,R�K����y�Ԝ��蛎�b�ƅ��C����E���A�Trc�9�Yψ����-�D �������yB�/1��8i�GMv�Q�xlh%{Βm
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Barry Allard
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+
2
+ [![Build Status](https://travis-ci.org/steakknife/validates_ip_address.svg)](https://travis-ci.org/steakknife/validates_ip_address)
3
+
4
+ # ValidatesIpAddress
5
+
6
+ ## Usage
7
+
8
+ class Widget
9
+ # somefield can be ...
10
+ # ipv4 ipv6 single address network
11
+ validates :somefield, ip: true # y y y y
12
+ validates :somefield, ip: { ranges_only: true } # y y n y
13
+ validates :somefield, ip: { addresses_only: true } # y y y n
14
+ validates :somefield, ip: { ipv4_only: true } # y n y y
15
+ validates :somefield, ip: { ipv6_only: true } # n y y y
16
+ validates :somefield, ip: { within: '10.0.0.0/8' } # y y y y and must be 10.0.0.0 - 10.255.255.255
17
+ # combine the above if you wish, it will work
18
+ end
19
+
20
+ ## Installation
21
+
22
+ ### Bundler
23
+
24
+ gem 'validates_ip_address'
25
+
26
+ ### manually
27
+
28
+ curl -L https://raw.githubusercontent.com/steakknife/validates_ip_address/master/gem-public_cert.pem | gem cert --add -
29
+ gem install validates_ip_address -p HighSecurity
30
+
31
+ ## Author
32
+
33
+ Barry Allard
34
+
35
+ ## License
36
+
37
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ fail 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ValidatesIpAddress'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ desc 'Run RSpec specs'
20
+ task :spec do
21
+ ARGV.delete 'spec'
22
+ sh "bundle exec rspec #{ARGV.join ' '}"
23
+ end
24
+
25
+ task default: :spec
@@ -0,0 +1,53 @@
1
+ require 'ipaddr'
2
+
3
+ class IpValidator < ActiveModel::EachValidator
4
+ if defined? IPAddr::AddressFamilyError
5
+ EXCEPTIONS = [ IPAddr::InvalidAddressError, IPAddr::AddressFamilyError ]
6
+ else
7
+ EXCEPTIONS = [ IPAddr::InvalidAddressError ]
8
+ end
9
+
10
+ def validate_each(record, attribute, value)
11
+ ip = IPAddr.new(value)
12
+ rng = ip.to_range
13
+ result = true
14
+
15
+ result &= ip.ipv4? if options[:ip4_only] == true
16
+ result &= ip.ipv6? if options[:ip6_only] == true
17
+
18
+ result &= rng.first != rng.last if options[:ranges_only] == true
19
+ result &= rng.first == rng.last if options[:addresses_only] == true
20
+
21
+ result &= IPAddr.new(options[:within]).include? ip if options[:within]
22
+
23
+ add_error(record, attribute, value) unless result
24
+
25
+ rescue *EXCEPTIONS
26
+ add_error(record, attribute, value)
27
+ end
28
+
29
+ private
30
+ def error_message
31
+ 'is not a valid ' +
32
+
33
+ if options.has_key? :ip6_only
34
+ 'IPv6 '
35
+ elsif options.has_key? :ip4_only
36
+ 'IPv4 '
37
+ else
38
+ 'IPv4 or IPv6 '
39
+ end +
40
+
41
+ if options.has_key? :range
42
+ 'address range'
43
+ elsif options.has_key? :address
44
+ 'address'
45
+ else
46
+ 'address or address range'
47
+ end
48
+ end
49
+
50
+ def add_error(record, attribute, value)
51
+ record.errors[attribute] << (options[:message] || error_message)
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :validates_ip_address do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,5 @@
1
+ require 'active_model'
2
+ require File.expand_path('../../app/validators/ip_validator', __FILE__)
3
+
4
+ module ValidatesIpAddress
5
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesIpAddress
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,84 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require 'validates_ip_address'
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # (such as loading up an entire rails app) will add to the boot time of your
11
+ # test suite on EVERY test run, even for an individual file that may not need
12
+ # all of that loaded.
13
+ #
14
+ # The `.rspec` file also contains a few flags that are not defaults but that
15
+ # users commonly want.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # The settings below are suggested to provide a good initial experience
20
+ # with RSpec, but feel free to customize to your heart's content.
21
+ =begin
22
+ # These two settings work together to allow you to limit a spec run
23
+ # to individual examples or groups you care about by tagging them with
24
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
25
+ # get run.
26
+ config.filter_run :focus
27
+ config.run_all_when_everything_filtered = true
28
+
29
+ # Many RSpec users commonly either run the entire suite or an individual
30
+ # file, and it's useful to allow more verbose output when running an
31
+ # individual spec file.
32
+ if config.files_to_run.one?
33
+ # RSpec filters the backtrace by default so as not to be so noisy.
34
+ # This causes the full backtrace to be printed when running a single
35
+ # spec file (e.g. to troubleshoot a particular spec failure).
36
+ config.full_backtrace = true
37
+
38
+ # Use the documentation formatter for detailed output,
39
+ # unless a formatter has already been configured
40
+ # (e.g. via a command-line flag).
41
+ config.formatter = 'doc' if config.formatters.none?
42
+ end
43
+
44
+ # Print the 10 slowest examples and example groups at the
45
+ # end of the spec run, to help surface which specs are running
46
+ # particularly slow.
47
+ config.profile_examples = 10
48
+
49
+ # Run specs in random order to surface order dependencies. If you find an
50
+ # order dependency and want to debug it, you can fix the order by providing
51
+ # the seed, which is printed after each run.
52
+ # --seed 1234
53
+ config.order = :random
54
+
55
+ # Seed global randomization in this process using the `--seed` CLI option.
56
+ # Setting this allows you to use `--seed` to deterministically reproduce
57
+ # test failures related to randomization by passing the same `--seed` value
58
+ # as the one that triggered the failure.
59
+ Kernel.srand config.seed
60
+
61
+ # rspec-expectations config goes here. You can use an alternate
62
+ # assertion/expectation library such as wrong or the stdlib/minitest
63
+ # assertions if you prefer.
64
+ config.expect_with :rspec do |expectations|
65
+ # Enable only the newer, non-monkey-patching expect syntax.
66
+ # For more details, see:
67
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
68
+ expectations.syntax = :expect
69
+ end
70
+
71
+ # rspec-mocks config goes here. You can use an alternate test double
72
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
73
+ config.mock_with :rspec do |mocks|
74
+ # Enable only the newer, non-monkey-patching expect syntax.
75
+ # For more details, see:
76
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
77
+ mocks.syntax = :expect
78
+
79
+ # Prevents you from mocking or stubbing a method that does not exist on
80
+ # a real object. This is generally recommended.
81
+ mocks.verify_partial_doubles = true
82
+ end
83
+ =end
84
+ end
@@ -0,0 +1,720 @@
1
+ require 'active_model'
2
+
3
+ def new_model(options)
4
+ Class.new do # tableless model
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Conversion
7
+ extend ActiveModel::Naming
8
+
9
+ attr_accessor :myfield
10
+ validates :myfield, ip: (options.empty? ? true : options)
11
+
12
+ def initialize(attributes = {})
13
+ attributes.each do |name, value|
14
+ send("#{name}=", value)
15
+ end
16
+ end
17
+
18
+ def persisted?
19
+ false
20
+ end
21
+ end
22
+ end
23
+
24
+ describe IpValidator do
25
+ before(:each) do
26
+ @validator_options = {}
27
+ end
28
+
29
+ context 'IP4 and IP6' do
30
+ context 'Ranges and ip addresses' do
31
+ context 'Not within' do
32
+ before { @model = new_model(@validator_options) }
33
+
34
+ context 'IPv4' do
35
+ it 'validates an ipv4 address' do
36
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(true)
37
+ end
38
+
39
+ it 'validates an ip4 range' do
40
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(true)
41
+ end
42
+ end
43
+
44
+ context 'IPv6' do
45
+ it 'validates an ip6 address' do
46
+ expect(@model.new(myfield: '::').valid?).to be(true)
47
+ end
48
+
49
+ it 'validates an ip6 range' do
50
+ expect(@model.new(myfield: '::/48').valid?).to be(true)
51
+ end
52
+ end
53
+
54
+ it 'rejects nil' do
55
+ expect(@model.new.valid?).to be(false)
56
+ end
57
+
58
+ it 'rejects empty' do
59
+ expect(@model.new(myfield: '').valid?).to be(false)
60
+ end
61
+
62
+ it 'rejects garbage' do
63
+ expect(@model.new(myfield: 'asdfasdfasdf').valid?).to be(false)
64
+ end
65
+ end
66
+
67
+ context ' within' do
68
+ context 'IPv4' do
69
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
70
+
71
+ it 'validates an ipv4 address within range' do
72
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(true)
73
+ end
74
+
75
+ it 'validates an ipv4 range within range' do
76
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(true)
77
+ end
78
+
79
+ it 'does not validate an ipv4 address outside range' do
80
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
81
+ end
82
+
83
+ it 'does not validate an ipv4 range outside range' do
84
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
85
+ end
86
+ end
87
+
88
+ context 'IPv6' do
89
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
90
+
91
+ it 'validates an ipv6 address within range' do
92
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(true)
93
+ end
94
+
95
+ it 'validates an ipv6 range within range' do
96
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(true)
97
+ end
98
+
99
+ it 'does not validate an ipv6 address outside range' do
100
+ expect(@model.new(myfield: '::').valid?).to be(false)
101
+ end
102
+
103
+ it 'does not validate an ipv6 range outside range' do
104
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'Ranges only' do
111
+ before(:each) do
112
+ @validator_options[:ranges_only] = true
113
+ end
114
+
115
+ context 'Not within' do
116
+ before { @model = new_model(@validator_options) }
117
+
118
+ context 'IPv4' do
119
+ it 'does not validate an ipv4 address' do
120
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(false)
121
+ end
122
+
123
+ it 'validates an ip4 range' do
124
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(true)
125
+ end
126
+ end
127
+
128
+ context 'IPv6' do
129
+ it 'does not validate an ip6 address' do
130
+ expect(@model.new(myfield: '::').valid?).to be(false)
131
+ end
132
+
133
+ it 'validates an ip6 range' do
134
+ expect(@model.new(myfield: '::/48').valid?).to be(true)
135
+ end
136
+ end
137
+ end
138
+
139
+ context ' within' do
140
+ context 'IPv4' do
141
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
142
+
143
+ it 'does not validate an ipv4 address within range' do
144
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(false)
145
+ end
146
+
147
+ it 'validates an ipv4 range within range' do
148
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(true)
149
+ end
150
+
151
+ it 'does not validate an ipv4 address outside range' do
152
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
153
+ end
154
+
155
+ it 'does not validate an ipv4 range outside range' do
156
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
157
+ end
158
+ end
159
+
160
+ context 'IPv6' do
161
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
162
+
163
+ it 'does not validate an ipv6 address within range' do
164
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(false)
165
+ end
166
+
167
+ it 'validates an ipv6 range within range' do
168
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(true)
169
+ end
170
+
171
+ it 'does not validate an ipv6 address outside range' do
172
+ expect(@model.new(myfield: '::').valid?).to be(false)
173
+ end
174
+
175
+ it 'does not validate an ipv6 range outside range' do
176
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ context 'Addresses only' do
183
+ before(:each) do
184
+ @validator_options[:addresses_only] = true
185
+ end
186
+
187
+ context 'Not within' do
188
+ before { @model = new_model(@validator_options) }
189
+
190
+ context 'IPv4' do
191
+ it 'validates an ipv4 address' do
192
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(true)
193
+ end
194
+
195
+ it 'does not validate an ip4 range' do
196
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(false)
197
+ end
198
+ end
199
+
200
+ context 'IPv6' do
201
+ it 'validates an ip6 address' do
202
+ expect(@model.new(myfield: '::').valid?).to be(true)
203
+ end
204
+
205
+ it 'does not validate an ip6 range' do
206
+ expect(@model.new(myfield: '::/48').valid?).to be(false)
207
+ end
208
+ end
209
+ end
210
+
211
+ context ' within' do
212
+ before { @model = new_model(@validator_options) }
213
+
214
+ context 'IPv4' do
215
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
216
+
217
+ it 'validates an ipv4 address within range' do
218
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(true)
219
+ end
220
+
221
+ it 'does not validate an ipv4 range within range' do
222
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(false)
223
+ end
224
+
225
+ it 'does not validate an ipv4 address outside range' do
226
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
227
+ end
228
+
229
+ it 'does not validate an ipv4 range outside range' do
230
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
231
+ end
232
+ end
233
+
234
+ context 'IPv6' do
235
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
236
+
237
+ it 'validates an ipv6 address within range' do
238
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(true)
239
+ end
240
+
241
+ it 'does not validate an ipv6 range within range' do
242
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(false)
243
+ end
244
+
245
+ it 'does not validate an ipv6 address outside range' do
246
+ expect(@model.new(myfield: '::').valid?).to be(false)
247
+ end
248
+
249
+ it 'does not validate an ipv6 range outside range' do
250
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
251
+ end
252
+ end
253
+ end
254
+ end
255
+ end
256
+
257
+ context 'IP4 only' do
258
+ before(:each) do
259
+ @validator_options[:ip4_only] = true
260
+ end
261
+
262
+ context 'Ranges and ip addresses' do
263
+ context 'Not within' do
264
+ before { @model = new_model(@validator_options) }
265
+
266
+ context 'IPv4' do
267
+ it 'validates an ipv4 address' do
268
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(true)
269
+ end
270
+
271
+ it 'validates an ip4 range' do
272
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(true)
273
+ end
274
+ end
275
+
276
+ context 'IPv6' do
277
+ it 'does not validate an ip6 address' do
278
+ expect(@model.new(myfield: '::').valid?).to be(false)
279
+ end
280
+
281
+ it 'does not validate an ip6 range' do
282
+ expect(@model.new(myfield: '::/48').valid?).to be(false)
283
+ end
284
+ end
285
+
286
+ it 'rejects nil' do
287
+ expect(@model.new.valid?).to be(false)
288
+ end
289
+
290
+ it 'rejects empty' do
291
+ expect(@model.new(myfield: '').valid?).to be(false)
292
+ end
293
+
294
+ it 'rejects garbage' do
295
+ expect(@model.new(myfield: 'asdfasdfasdf').valid?).to be(false)
296
+ end
297
+ end
298
+
299
+ context ' within' do
300
+ context 'IPv4' do
301
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
302
+
303
+ it 'validates an ipv4 address within range' do
304
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(true)
305
+ end
306
+
307
+ it 'validates an ipv4 range within range' do
308
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(true)
309
+ end
310
+
311
+ it 'does not validate an ipv4 address outside range' do
312
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
313
+ end
314
+
315
+ it 'does not validate an ipv4 range outside range' do
316
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
317
+ end
318
+ end
319
+
320
+ context 'IPv6' do
321
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
322
+
323
+ it 'does not validate an ipv6 address within range' do
324
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(false)
325
+ end
326
+
327
+ it 'does not validate an ipv6 range within range' do
328
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(false)
329
+ end
330
+
331
+ it 'does not validate an ipv6 address outside range' do
332
+ expect(@model.new(myfield: '::').valid?).to be(false)
333
+ end
334
+
335
+ it 'does not validate an ipv6 range outside range' do
336
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
337
+ end
338
+ end
339
+ end
340
+ end
341
+
342
+ context 'Ranges only' do
343
+ before(:each) do
344
+ @validator_options[:ranges_only] = true
345
+ end
346
+
347
+ context 'Not within' do
348
+ before { @model = new_model(@validator_options) }
349
+
350
+ context 'IPv4' do
351
+ it 'does not validate an ipv4 address' do
352
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(false)
353
+ end
354
+
355
+ it 'validates an ip4 range' do
356
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(true)
357
+ end
358
+ end
359
+
360
+ context 'IPv6' do
361
+ it 'does not validate an ip6 address' do
362
+ expect(@model.new(myfield: '::').valid?).to be(false)
363
+ end
364
+
365
+ it 'does not validate an ip6 range' do
366
+ expect(@model.new(myfield: '::/48').valid?).to be(false)
367
+ end
368
+ end
369
+ end
370
+
371
+ context ' within' do
372
+ context 'IPv4' do
373
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
374
+
375
+ it 'does not validate an ipv4 address within range' do
376
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(false)
377
+ end
378
+
379
+ it 'validates an ipv4 range within range' do
380
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(true)
381
+ end
382
+
383
+ it 'does not validate an ipv4 address outside range' do
384
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
385
+ end
386
+
387
+ it 'does not validate an ipv4 range outside range' do
388
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
389
+ end
390
+ end
391
+
392
+ context 'IPv6' do
393
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
394
+
395
+ it 'does not validate an ipv6 address within range' do
396
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(false)
397
+ end
398
+
399
+ it 'does not validate an ipv6 range within range' do
400
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(false)
401
+ end
402
+
403
+ it 'does not validate an ipv6 address outside range' do
404
+ expect(@model.new(myfield: '::').valid?).to be(false)
405
+ end
406
+
407
+ it 'does not validate an ipv6 range outside range' do
408
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
409
+ end
410
+ end
411
+ end
412
+ end
413
+
414
+ context 'Addresses only' do
415
+ before(:each) do
416
+ @validator_options[:addresses_only] = true
417
+ end
418
+
419
+ context 'Not within' do
420
+ before { @model = new_model(@validator_options) }
421
+
422
+ context 'IPv4' do
423
+ it 'validates an ipv4 address' do
424
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(true)
425
+ end
426
+
427
+ it 'does not validate an ip4 range' do
428
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(false)
429
+ end
430
+ end
431
+
432
+ context 'IPv6' do
433
+ it 'does not validate an ip6 address' do
434
+ expect(@model.new(myfield: '::').valid?).to be(false)
435
+ end
436
+
437
+ it 'does not validate an ip6 range' do
438
+ expect(@model.new(myfield: '::/48').valid?).to be(false)
439
+ end
440
+ end
441
+ end
442
+
443
+ context ' within' do
444
+ before { @model = new_model(@validator_options) }
445
+
446
+ context 'IPv4' do
447
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
448
+
449
+ it 'validates an ipv4 address within range' do
450
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(true)
451
+ end
452
+
453
+ it 'does not validate an ipv4 range within range' do
454
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(false)
455
+ end
456
+
457
+ it 'does not validate an ipv4 address outside range' do
458
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
459
+ end
460
+
461
+ it 'does not validate an ipv4 range outside range' do
462
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
463
+ end
464
+ end
465
+
466
+ context 'IPv6' do
467
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
468
+
469
+ it 'does not validate an ipv6 address within range' do
470
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(false)
471
+ end
472
+
473
+ it 'does not validate an ipv6 range within range' do
474
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(false)
475
+ end
476
+
477
+ it 'does not validate an ipv6 address outside range' do
478
+ expect(@model.new(myfield: '::').valid?).to be(false)
479
+ end
480
+
481
+ it 'does not validate an ipv6 range outside range' do
482
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
483
+ end
484
+ end
485
+ end
486
+ end
487
+ end
488
+
489
+ context 'IP6 only' do
490
+ before(:each) do
491
+ @validator_options[:ip6_only] = true
492
+ end
493
+
494
+ context 'Ranges and ip addresses' do
495
+ context 'Not within' do
496
+ before { @model = new_model(@validator_options) }
497
+
498
+ context 'IPv4' do
499
+ it 'does not validate an ipv4 address' do
500
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(false)
501
+ end
502
+
503
+ it 'does not validate an ip4 range' do
504
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(false)
505
+ end
506
+ end
507
+
508
+ context 'IPv6' do
509
+ it 'validates an ip6 address' do
510
+ expect(@model.new(myfield: '::').valid?).to be(true)
511
+ end
512
+
513
+ it 'validates an ip6 range' do
514
+ expect(@model.new(myfield: '::/48').valid?).to be(true)
515
+ end
516
+ end
517
+
518
+ it 'rejects nil' do
519
+ expect(@model.new.valid?).to be(false)
520
+ end
521
+
522
+ it 'rejects empty' do
523
+ expect(@model.new(myfield: '').valid?).to be(false)
524
+ end
525
+
526
+ it 'rejects garbage' do
527
+ expect(@model.new(myfield: 'asdfasdfasdf').valid?).to be(false)
528
+ end
529
+ end
530
+
531
+ context ' within' do
532
+ context 'IPv4' do
533
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
534
+
535
+ it 'does not validate an ipv4 address within range' do
536
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(false)
537
+ end
538
+
539
+ it 'does not validate an ipv4 range within range' do
540
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(false)
541
+ end
542
+
543
+ it 'does not validate an ipv4 address outside range' do
544
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
545
+ end
546
+
547
+ it 'does not validate an ipv4 range outside range' do
548
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
549
+ end
550
+ end
551
+
552
+ context 'IPv6' do
553
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
554
+
555
+ it 'validates an ipv6 address within range' do
556
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(true)
557
+ end
558
+
559
+ it 'validates an ipv6 range within range' do
560
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(true)
561
+ end
562
+
563
+ it 'does not validate an ipv6 address outside range' do
564
+ expect(@model.new(myfield: '::').valid?).to be(false)
565
+ end
566
+
567
+ it 'does not validate an ipv6 range outside range' do
568
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
569
+ end
570
+ end
571
+ end
572
+ end
573
+
574
+ context 'Ranges only' do
575
+ before(:each) do
576
+ @validator_options[:ranges_only] = true
577
+ end
578
+
579
+ context 'Not within' do
580
+ before { @model = new_model(@validator_options) }
581
+
582
+ context 'IPv4' do
583
+ it 'does not validate an ipv4 address' do
584
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(false)
585
+ end
586
+
587
+ it 'does not validate an ip4 range' do
588
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(false)
589
+ end
590
+ end
591
+
592
+ context 'IPv6' do
593
+ it 'does not validate an ip6 address' do
594
+ expect(@model.new(myfield: '::').valid?).to be(false)
595
+ end
596
+
597
+ it 'validates an ip6 range' do
598
+ expect(@model.new(myfield: '::/48').valid?).to be(true)
599
+ end
600
+ end
601
+ end
602
+
603
+ context ' within' do
604
+ context 'IPv4' do
605
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
606
+
607
+ it 'does not validate an ipv4 address within range' do
608
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(false)
609
+ end
610
+
611
+ it 'does not validate an ipv4 range within range' do
612
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(false)
613
+ end
614
+
615
+ it 'does not validate an ipv4 address outside range' do
616
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
617
+ end
618
+
619
+ it 'does not validate an ipv4 range outside range' do
620
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
621
+ end
622
+ end
623
+
624
+ context 'IPv6' do
625
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
626
+
627
+ it 'does not validate an ipv6 address within range' do
628
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(false)
629
+ end
630
+
631
+ it 'validates an ipv6 range within range' do
632
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(true)
633
+ end
634
+
635
+ it 'does not validate an ipv6 address outside range' do
636
+ expect(@model.new(myfield: '::').valid?).to be(false)
637
+ end
638
+
639
+ it 'does not validate an ipv6 range outside range' do
640
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
641
+ end
642
+ end
643
+ end
644
+ end
645
+
646
+ context 'Addresses only' do
647
+ before(:each) do
648
+ @validator_options[:addresses_only] = true
649
+ end
650
+
651
+ context 'Not within' do
652
+ before { @model = new_model(@validator_options) }
653
+
654
+ context 'IPv4' do
655
+ it 'does not validate an ipv4 address' do
656
+ expect(@model.new(myfield: '1.2.3.4').valid?).to be(false)
657
+ end
658
+
659
+ it 'does not validate an ip4 range' do
660
+ expect(@model.new(myfield: '1.2.3.4/16').valid?).to be(false)
661
+ end
662
+ end
663
+
664
+ context 'IPv6' do
665
+ it 'validates an ip6 address' do
666
+ expect(@model.new(myfield: '::').valid?).to be(true)
667
+ end
668
+
669
+ it 'does not validate an ip6 range' do
670
+ expect(@model.new(myfield: '::/48').valid?).to be(false)
671
+ end
672
+ end
673
+ end
674
+
675
+ context ' within' do
676
+ before { @model = new_model(@validator_options) }
677
+
678
+ context 'IPv4' do
679
+ before { @model = new_model(@validator_options.merge(within: '10.0.0.0/8')) }
680
+
681
+ it 'does not validate an ipv4 address within range' do
682
+ expect(@model.new(myfield: '10.3.4.5').valid?).to be(false)
683
+ end
684
+
685
+ it 'does not validate an ipv4 range within range' do
686
+ expect(@model.new(myfield: '10.3.4.0/24').valid?).to be(false)
687
+ end
688
+
689
+ it 'does not validate an ipv4 address outside range' do
690
+ expect(@model.new(myfield: '172.1.1.1').valid?).to be(false)
691
+ end
692
+
693
+ it 'does not validate an ipv4 range outside range' do
694
+ expect(@model.new(myfield: '172.0.0.0/8').valid?).to be(false)
695
+ end
696
+ end
697
+
698
+ context 'IPv6' do
699
+ before { @model = new_model(@validator_options.merge(within: 'abcd::/16')) }
700
+
701
+ it 'validates an ipv6 address within range' do
702
+ expect(@model.new(myfield: 'abcd::3:4').valid?).to be(true)
703
+ end
704
+
705
+ it 'does not validate an ipv6 range within range' do
706
+ expect(@model.new(myfield: 'abcd::3:0/32').valid?).to be(false)
707
+ end
708
+
709
+ it 'does not validate an ipv6 address outside range' do
710
+ expect(@model.new(myfield: '::').valid?).to be(false)
711
+ end
712
+
713
+ it 'does not validate an ipv6 range outside range' do
714
+ expect(@model.new(myfield: '::/32').valid?).to be(false)
715
+ end
716
+ end
717
+ end
718
+ end
719
+ end
720
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_ip_address
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Barry Allard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain:
12
+ - !binary |-
13
+ LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURPakNDQWlLZ0F3SUJB
14
+ Z0lCQURBTkJna3Foa2lHOXcwQkFRVUZBREJETVJVd0V3WURWUVFEREF4aVlY
15
+ SnkKZVM1aGJHeGhjbVF4RlRBVEJnb0praWFKay9Jc1pBRVpGZ1ZuYldGcGJE
16
+ RVRNQkVHQ2dtU0pvbVQ4aXhrQVJrVwpBMk52YlRBZUZ3MHhNekEwTURnd01U
17
+ STBOVGhhRncweE5EQTBNRGd3TVRJME5UaGFNRU14RlRBVEJnTlZCQU1NCkRH
18
+ Smhjbko1TG1Gc2JHRnlaREVWTUJNR0NnbVNKb21UOGl4a0FSa1dCV2R0WVds
19
+ c01STXdFUVlLQ1pJbWlaUHkKTEdRQkdSWURZMjl0TUlJQklqQU5CZ2txaGtp
20
+ Rzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2RGNtbHhKSApleVNpVUJj
21
+ WVRxR1hhV0VUUlZZbWdmR3dXUlBaenFrbUJkSVZaa2JrOVNXeHNlaUhXSVJl
22
+ RldqUDQzOFVvVVRzCkoxN0cvSHVRYjBTbWpQQ01aeTg5NjdGYjJ3cXMrUVJi
23
+ Y21wbm10WUExdmlsZ0MyQ0l6bnRGT0ZMU0EyS3BmWkgKZEpTc2c2YWFYcXdT
24
+ NC9LSnhLNm9vRHNwK2lSNnpHVElOd2tkVXQ0a3RwcWdDSHoxVll1enZpaTJz
25
+ bG5hemJtNApjUVVpL3dXSXluSHl6emRyUHZEaEdnYVptMTYxTVlIaWRDdFYr
26
+ d3pxd2plVmVGc3lRd0NGVkVrckQvMEc5OGhvCkd0aTh2QjZYajZWak8zbitr
27
+ aC9LbFlaZ21NN1NXYXVMcG8rMlJHbElZVllkcFFRV0d3aE1FbXZTZ0J4amZY
28
+ NGMKdW5ERnhPMVpBUUlSQlFJREFRQUJvemt3TnpBSkJnTlZIUk1FQWpBQU1C
29
+ MEdBMVVkRGdRV0JCUytLazd5cGk5dQprRlVjYXFKbHBhS083ZVdpVVRBTEJn
30
+ TlZIUThFQkFNQ0JMQXdEUVlKS29aSWh2Y05BUUVGQlFBRGdnRUJBTGllCllD
31
+ TmVXOUVJUS9qOCsyZW1NbTA0ODRKVnR5Y09xUHlJdTVVQUx0c1U0MkVuMzky
32
+ SFZPWjZvRUEvcmgyS0ZSdnEKZGNEd2NKUEkvdTdQeldwOVROcDgxUFRTaHRI
33
+ TXRTczdXdjFVc0MwNHZROWI2WEJFb21XYmJ6b3h4Z3p5alA3bApaVjRMNUh6
34
+ bVgwbkRPU0VGSnlZa3Fid2dZaklvbGRnMlRNbHcyQmVvVnFHbTdHeDFsalhL
35
+ YjJLZzVpYXN5RHBJCkMvZ0FpR0JJQVg3Rnh5SVhtalpxMzh4V0JPeHlHRjNO
36
+ RkwvVzZ6K3ZoSmc4MUhHZE5CQ3BJZHdyUS9lWE9qYmEKVnF3d2ZZK01zM2dj
37
+ Q0hTRVJHMVg0QUZXMXplc1grVVdjVEN3Vkx0QXN1UldRaEM4b2REU1ZEV3pV
38
+ ZmtabU9RdApWaUl4VTFacGhJbnFsN0w1ZzM0PQotLS0tLUVORCBDRVJUSUZJ
39
+ Q0FURS0tLS0tCg==
40
+ date: 2014-04-01 00:00:00.000000000 Z
41
+ dependencies:
42
+ - !ruby/object:Gem::Dependency
43
+ name: activemodel
44
+ requirement: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ - !ruby/object:Gem::Dependency
75
+ name: rspec
76
+ requirement: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ - !ruby/object:Gem::Dependency
91
+ name: should_not
92
+ requirement: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ type: :development
99
+ prerelease: false
100
+ version_requirements: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ description: Validates IPv4 and IPv6 addresses
107
+ email:
108
+ - barry.allard@gmail.com
109
+ executables: []
110
+ extensions: []
111
+ extra_rdoc_files: []
112
+ files:
113
+ - app/validators/ip_validator.rb
114
+ - lib/tasks/validates_ip_address_tasks.rake
115
+ - lib/validates_ip_address/version.rb
116
+ - lib/validates_ip_address.rb
117
+ - LICENSE
118
+ - Rakefile
119
+ - README.md
120
+ - spec/spec_helper.rb
121
+ - spec/validators/ip_validator_spec.rb
122
+ - .rspec
123
+ homepage: https://github.com/steakknife/validates_ip_address
124
+ licenses: []
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.23
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: Rails validations for IP addreses
147
+ test_files:
148
+ - spec/spec_helper.rb
149
+ - spec/validators/ip_validator_spec.rb
150
+ - .rspec
metadata.gz.sig ADDED
Binary file