validation_rules 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzVjNTIxODFjZDZkNTVjMWMwMzAyYmFiOTIxNGFmN2JhN2UyYmRmYQ==
4
+ NzQ2NzQ2ZjEwNWIzNTg3MzJhNzIzOTAxMzM2MzhlZDYxNTE1NzcxOA==
5
5
  data.tar.gz: !binary |-
6
- OTQ2ZDJkYjE5OWJkNmE3MGY1YzZkZmEwOWJhMmViMmMwOGRkNzc0Zg==
6
+ YmUyZjg3ZWNiYzI5NzIyODM5ZTU2NTRjMDYzOTU0M2JiNTljMzRlZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NDlmMmY0NWU2ZTk5MmZhMGYyNDE2Zjg3OTIwOWQyYzA4MzdlZTI0ZGZhNzFk
10
- MTVlYmE2ZGFmZTNmNjMwM2FmMDA1ZTkyZDY0NzM3MjY4OGE3MzExNjcwNWUx
11
- OTBjZTUzNDQwOWY1N2YxMTQ3M2YwNzY5ZWZhY2NlNTZiZDBhZDY=
9
+ YTMzNWU3M2NjYjU5ZDVkZWNlYTM0ZjEyYTBmMWZkZTc1OWRhNjVmYzg1OGRh
10
+ MzViMzIyNTY2YjIxY2YwODViZWZlZWEwODAwMzI0N2IxZDJmNjRhNDg5OTcy
11
+ OWJkNjc3ODNlMThmNWQwOTYwYWI1OWQzYzM4M2Y3YmI3MDg2NTc=
12
12
  data.tar.gz: !binary |-
13
- ZGFiYzYxNDI2ZTYxNzNlMWJmMjE0MDMyMWU4ZjcwYmNlYmE1N2NkZWM2ZTQ1
14
- ZDlhNGVjZDM0NGJlN2JkYThkOWIwMDE2MmQ1MzdkNTc1MzM3MmUwZDIzZjM4
15
- YjZlZTZhZWJhNjc2MDUyNWEwYTQwNmZlZmE2NDAwZDg0ZGVjYzk=
13
+ MjcyMjhmODA3YWM0OTBiZWVhYzQ2ZTE2ZjMxMmViZWZiZTFhMWZkNjQxMDQy
14
+ M2VjMDAyYjU1MzFhMTY3MmE2Mzk2NDVlZjgyOTg0ZmVkZDU1Njc3YWMyYjAz
15
+ N2QzMTY1MTNhOWE3NjQzNDYxNjkyNTBhNzJlMzRmMTUxYzRkZGM=
@@ -1,6 +1,7 @@
1
1
  require 'json'
2
2
  require 'date'
3
3
  require 'time'
4
+ require 'uri'
4
5
 
5
6
  module ValidationRules
6
7
  EMAIL_REGEX = /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i
@@ -72,6 +73,19 @@ module ValidationRules
72
73
  value.to_i >= Time.now.to_i
73
74
  end
74
75
 
76
+ def self.past_date(value)
77
+ value = Time.parse(value) if value.is_a? String
78
+ value.to_i <= Time.now.to_i
79
+ end
80
+
81
+ def self.between_dates(value, date1, date2)
82
+ value = Time.parse(value) if value.is_a? String
83
+ date1 = Time.parse(date1) if date1.is_a? String
84
+ date2 = Time.parse(date2) if date2.is_a? String
85
+
86
+ value.between?(date1, date2)
87
+ end
88
+
75
89
  def self.string(value)
76
90
  value.is_a? String
77
91
  end
@@ -123,6 +137,10 @@ module ValidationRules
123
137
  value.to_f > 0
124
138
  end
125
139
 
140
+ def self.negative(value)
141
+ value.to_f < 0
142
+ end
143
+
126
144
  def self.range(value, min, max)
127
145
  value.to_f >= min.to_f and value.to_f <= max.to_f
128
146
  end
@@ -134,4 +152,13 @@ module ValidationRules
134
152
  def self.any_bool(value)
135
153
  [true, false, 0, 1, "0", "1", "true", "false"].include?(value)
136
154
  end
155
+
156
+ def self.url(value)
157
+ begin
158
+ uri = URI.parse(value)
159
+ uri.kind_of? URI::HTTP
160
+ rescue URI::InvalidURIError
161
+ false
162
+ end
163
+ end
137
164
  end
@@ -1,3 +1,3 @@
1
1
  module ValidationRules
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
3
  end
@@ -245,6 +245,16 @@ describe ValidationRules do
245
245
  ValidationRules.positive(0).should be_false
246
246
  end
247
247
 
248
+ it 'validates negative' do
249
+ ValidationRules.negative('-10.99').should be_true
250
+ ValidationRules.negative(-10.99).should be_true
251
+
252
+ ValidationRules.negative(10).should be_false
253
+ ValidationRules.negative(10.99).should be_false
254
+ ValidationRules.negative('10.99').should be_false
255
+ ValidationRules.negative(0).should be_false
256
+ end
257
+
248
258
  it 'validates range' do
249
259
  ValidationRules.range(5, 1, 10).should be_true
250
260
  ValidationRules.range(5.5, 1, 10).should be_true
@@ -289,12 +299,29 @@ describe ValidationRules do
289
299
  end
290
300
 
291
301
  it 'validates future dates' do
292
- ValidationRules.future_date('2060-10-25').should be_true
302
+ ValidationRules.future_date('2100-10-25').should be_true
293
303
  ValidationRules.future_date(Time.now).should be_true
294
304
 
295
305
  ValidationRules.future_date('2010-10-25').should be_false
296
306
  end
297
307
 
308
+ it 'validates past dates' do
309
+ ValidationRules.past_date('1998-10-25').should be_true
310
+ ValidationRules.past_date(Time.now).should be_true
311
+
312
+ ValidationRules.past_date('2100-10-25').should be_false
313
+ end
314
+
315
+ describe '.between_dates' do
316
+ it 'validates valid between' do
317
+ ValidationRules.between_dates(Time.now, '2010-10-25', '2100-10-25').should be_true
318
+
319
+ ValidationRules.between_dates(Time.now, '2010-10-25', '2011-10-25').should be_false
320
+ ValidationRules.between_dates(Time.now, '2100-10-25', '2120-10-25').should be_false
321
+ ValidationRules.between_dates(Time.now, '2100-10-25', '2011-10-25').should be_false
322
+ end
323
+ end
324
+
298
325
  subject do
299
326
  ValidationRules
300
327
  end
@@ -356,5 +383,24 @@ describe ValidationRules do
356
383
  subject.any_bool('abc').should be_false
357
384
  subject.any_bool(5).should be_false
358
385
  end
386
+ end # /.any_bool
387
+
388
+ describe '.url' do
389
+ it 'allows valid urls' do
390
+ subject.url('http://www.example.com').should be_true
391
+ subject.url('http://www.example.com/path').should be_true
392
+ subject.url('http://example.com').should be_true
393
+ subject.url('http://example.com?param=something').should be_true
394
+ subject.url('http://test:test@example.com').should be_true
395
+ subject.url('http://example.co.uk').should be_true
396
+ subject.url('http://example.com:80').should be_true
397
+ subject.url('http://localhost').should be_true
398
+ subject.url('example.com/some%20path').should be_false
399
+ end
400
+
401
+ it 'rejects invalid urls' do
402
+ subject.url('example.com').should be_false
403
+ subject.url('example.com/some path').should be_false
404
+ end
359
405
  end
360
406
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation_rules
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Gotterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-17 00:00:00.000000000 Z
11
+ date: 2013-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec