validates_simple 0.0.3 → 0.0.4
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 +4 -4
- data/README.md +13 -9
- data/lib/validates_simple.rb +1 -1
- data/lib/validation/rules.rb +39 -1
- data/spec/validation_spec.rb +27 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdae6f59f59eb6adb7152b99d44d0f3c678f5383
|
4
|
+
data.tar.gz: e4e789bd55ddfa84c0815e4db33596f700546c3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcdfe6efba25d6528eed3011244fb5d862346d4c964c38de0b5a842fd57b9316cf1da7027aec38d64c687a96120bfca05cd05ea3f41806b2abf1921ae75cddf0
|
7
|
+
data.tar.gz: f0b5dd8fcea22cd98b082aea5dcdfd8bb17cacd185f7b4550f40205d693a611a322077fb4bafb15db27314a54ff586749ecb19bebde32fc632142b292b895616
|
data/README.md
CHANGED
@@ -14,6 +14,7 @@ json etc. So i am creating one.
|
|
14
14
|
else
|
15
15
|
false
|
16
16
|
end
|
17
|
+
end
|
17
18
|
validates(field, callback, message)
|
18
19
|
end
|
19
20
|
end
|
@@ -21,14 +22,14 @@ json etc. So i am creating one.
|
|
21
22
|
|
22
23
|
2. It can reuse defined validations when defining one
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
module Validation
|
26
|
+
module Rules
|
27
|
+
def validates_numericality_of(field, message='')
|
28
|
+
validates_presence_of(field)
|
29
|
+
validates_format_of(field, /^\d+(\.\d+)?$/
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
32
33
|
|
33
34
|
3. It should provide some predefined validations
|
34
35
|
* validates_presence_of - checks if the field is present and it is not nil or empty string
|
@@ -37,9 +38,12 @@ json etc. So i am creating one.
|
|
37
38
|
* validates_numericality_of - checks if a field is a correct number format
|
38
39
|
* validates_greather_then - checks if a field is greather then some value
|
39
40
|
* validates_less_then - checks if a field is less then some value
|
40
|
-
*
|
41
|
+
* validates_length_of_within - checks if the length is in some bounds
|
42
|
+
* validates_option_in - checks if the field is one of allowed options
|
41
43
|
|
42
44
|
For additional info check the specs for usage [validates simple specs](https://github.com/sirfilip/validates_simple/blob/master/spec/validation_spec.rb)
|
43
45
|
|
44
46
|
|
45
47
|
License: MIT
|
48
|
+
|
49
|
+
[](http://badge.fury.io/rb/validates_simple)
|
data/lib/validates_simple.rb
CHANGED
data/lib/validation/rules.rb
CHANGED
@@ -5,9 +5,20 @@ module Validation
|
|
5
5
|
@rules << {:callback => callback, :message => Error.new(field, message)}
|
6
6
|
end
|
7
7
|
|
8
|
+
def validates_presence_of_field(field, message='')
|
9
|
+
callback = lambda do |data|
|
10
|
+
if data.has_key?(field)
|
11
|
+
true
|
12
|
+
else
|
13
|
+
false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
validates(field, callback, message)
|
17
|
+
end
|
18
|
+
|
8
19
|
def validates_presence_of(field, message='')
|
9
20
|
callback = lambda do |data|
|
10
|
-
if data.
|
21
|
+
if data.fetch(field, nil).to_s !~ /\A\s*\z/
|
11
22
|
true
|
12
23
|
else
|
13
24
|
false
|
@@ -108,5 +119,32 @@ module Validation
|
|
108
119
|
end
|
109
120
|
validates(field, callback, message)
|
110
121
|
end
|
122
|
+
|
123
|
+
def validates_option_in(field, options, message='')
|
124
|
+
validates_presence_of(field, message)
|
125
|
+
callback = lambda do |data|
|
126
|
+
if options.include? data[field]
|
127
|
+
true
|
128
|
+
else
|
129
|
+
false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
validates(field, callback, message)
|
133
|
+
end
|
134
|
+
|
135
|
+
def validates_url_format_of(field, message='')
|
136
|
+
require 'uri'
|
137
|
+
validates_presence_of(field, message)
|
138
|
+
callback = lambda do |data|
|
139
|
+
url = URI.parse(data[field]) rescue nil
|
140
|
+
if url.nil?
|
141
|
+
false
|
142
|
+
else
|
143
|
+
true
|
144
|
+
end
|
145
|
+
end
|
146
|
+
validates(field, callback, message)
|
147
|
+
end
|
148
|
+
|
111
149
|
end
|
112
150
|
end
|
data/spec/validation_spec.rb
CHANGED
@@ -11,6 +11,16 @@ describe Validation do
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
describe '#validates_presence_of_field' do
|
15
|
+
it 'checks if the field is present regardless of the value' do
|
16
|
+
validator.validates_presence_of_field('a field')
|
17
|
+
validator.validate({'a field' => false}).should be_true
|
18
|
+
validator.validate({'a field' => nil}).should be_true
|
19
|
+
validator.validate({'a field' => ''}).should be_true
|
20
|
+
validator.validate({'another field' => false}).should be_false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
14
24
|
describe "#validates presence of" do
|
15
25
|
it "fails if field is not inside of the hash" do
|
16
26
|
validator.validates_presence_of('fieldname', ":field is required")
|
@@ -113,6 +123,23 @@ describe Validation do
|
|
113
123
|
end
|
114
124
|
end
|
115
125
|
|
126
|
+
describe '#validates_option_in' do
|
127
|
+
it 'checks if the option is one of the predefined' do
|
128
|
+
validator.validates_option_in('subscription', ['basic', 'premium'], 'Subscription must be basic or premium')
|
129
|
+
validator.validate({'subscription' => 'basic'}).should be_true
|
130
|
+
validator.validate({'subscription' => 'other'}).should be_false
|
131
|
+
validator.validate({'other-field' => 42}).should be_false
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe '#validates_url_format_of' do
|
136
|
+
it 'checks if the url is in a correct format' do
|
137
|
+
validator.validates_url_format_of('url', 'Url format is not allowed')
|
138
|
+
validator.validate({'url' => 'http://google.com'}).should be_true
|
139
|
+
validator.validate({'url' => 'f:||asdsdfsadf'}).should be_false
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
116
143
|
describe 'a simple sample of successfull validation' do
|
117
144
|
it "validates successfully" do
|
118
145
|
validator.validates_presence_of('username', 'Username is required')
|