simple_range_validator 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +31 -3
- data/VERSION +1 -1
- data/lib/array_types_validator.rb +29 -0
- data/lib/array_validator.rb +27 -0
- data/lib/simple_range_validator.rb +2 -0
- data/simple_range_validator.gemspec +70 -0
- data/spec/array_types_validator_spec.rb +46 -0
- data/spec/array_validator_spec.rb +46 -0
- data/spec/range_validator_spec.rb +2 -2
- metadata +7 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Simple Range validator
|
2
2
|
|
3
|
-
Validate fields of type `Range
|
3
|
+
Validate fields of type `Range`. Also contains simple validators for Array values and types respectively ;)
|
4
4
|
|
5
5
|
## Install
|
6
6
|
|
@@ -26,10 +26,10 @@ And here using `factory_girl` gem:
|
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
FactoryGirl.define do
|
29
|
-
sequence(:cost_range)
|
29
|
+
sequence(:cost_range) { [ (0..2000), (2000..4000), (4000..8000) ].sample }
|
30
30
|
|
31
31
|
factory :search do
|
32
|
-
cost
|
32
|
+
cost { FactoryGirl.generate(:cost_range) }
|
33
33
|
end
|
34
34
|
end
|
35
35
|
```
|
@@ -44,6 +44,34 @@ Also check out https://github.com/chrisb87/range_validator
|
|
44
44
|
|
45
45
|
Could make sense to integrate both of these gems ;)
|
46
46
|
|
47
|
+
## Array Validator
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class Search
|
51
|
+
include Mongoid::Document
|
52
|
+
|
53
|
+
field :color, type: Array
|
54
|
+
|
55
|
+
# you can also use only: as the option
|
56
|
+
validates :color, array: {in: ['red', 'blue', 'green'] }
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Array Type Validator
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class Search
|
64
|
+
include Mongoid::Document
|
65
|
+
|
66
|
+
field :color, type: Array
|
67
|
+
|
68
|
+
# you can also use in: as the option
|
69
|
+
validates :color, array_types: {only: [String, Fixnum] }
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Simple as pie ;)
|
74
|
+
|
47
75
|
## Contributing to range_validator
|
48
76
|
|
49
77
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class ArrayTypesValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
record.errors[attribute] << "must contain instances of only: #{show_allowed_types}, was: #{types_for(value)}" unless allowed_type? value
|
4
|
+
end
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
def allowed_type? value
|
9
|
+
raise ArgumentError, "Must be an Array, was: #{value}" unless value.kind_of?(Array)
|
10
|
+
return true if !allowed_types || allowed_types.empty?
|
11
|
+
(types_for(value) - allowed_types).empty?
|
12
|
+
end
|
13
|
+
|
14
|
+
def types_for value
|
15
|
+
value.map(&:class).uniq
|
16
|
+
end
|
17
|
+
|
18
|
+
def show_allowed_types
|
19
|
+
allowed_types.join(", ")
|
20
|
+
end
|
21
|
+
|
22
|
+
def allowed_types
|
23
|
+
[only].flatten
|
24
|
+
end
|
25
|
+
|
26
|
+
def only
|
27
|
+
options[:only] || options[:in]
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'array_types_validator'
|
2
|
+
|
3
|
+
class ArrayValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
record.errors[attribute] << "must be in: #{show_list}" unless contains? value
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def contains? value
|
11
|
+
raise ArgumentError, "Must be an Array, was: #{value}" unless value.kind_of?(Array)
|
12
|
+
(value - list).empty?
|
13
|
+
end
|
14
|
+
|
15
|
+
# also works with range
|
16
|
+
def list
|
17
|
+
only.to_a
|
18
|
+
end
|
19
|
+
|
20
|
+
def only
|
21
|
+
options[:in] || options[:only]
|
22
|
+
end
|
23
|
+
|
24
|
+
def show_list
|
25
|
+
list.join(", ")
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "simple_range_validator"
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kristian Mandrup"]
|
12
|
+
s.date = "2012-11-06"
|
13
|
+
s.description = "Range validator for any Range model fields"
|
14
|
+
s.email = "kmandrup@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/array_types_validator.rb",
|
29
|
+
"lib/array_validator.rb",
|
30
|
+
"lib/simple_range_validator.rb",
|
31
|
+
"simple_range_validator.gemspec",
|
32
|
+
"spec/array_types_validator_spec.rb",
|
33
|
+
"spec/array_validator_spec.rb",
|
34
|
+
"spec/range_validator_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = "http://github.com/kristianmandrup/simple_range_validator"
|
38
|
+
s.licenses = ["MIT"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = "1.8.24"
|
41
|
+
s.summary = "Validate Range fields in your models"
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
|
49
|
+
s.add_development_dependency(%q<rdoc>, [">= 3.12"])
|
50
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, [">= 1.8.4"])
|
52
|
+
s.add_development_dependency(%q<simplecov>, [">= 0.5"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
55
|
+
s.add_dependency(%q<rspec>, [">= 2.8.0"])
|
56
|
+
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
57
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
58
|
+
s.add_dependency(%q<jeweler>, [">= 1.8.4"])
|
59
|
+
s.add_dependency(%q<simplecov>, [">= 0.5"])
|
60
|
+
end
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
63
|
+
s.add_dependency(%q<rspec>, [">= 2.8.0"])
|
64
|
+
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
65
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
66
|
+
s.add_dependency(%q<jeweler>, [">= 1.8.4"])
|
67
|
+
s.add_dependency(%q<simplecov>, [">= 0.5"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# http://jeffkreeftmeijer.com/2011/isolated-testing-for-custom-validators-in-rails-3/
|
4
|
+
|
5
|
+
class ValidatableType
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
validates :type, array_types: {only: [String, Fixnum]}
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ArrayTypesValidator do
|
12
|
+
|
13
|
+
subject { ValidatableType.new }
|
14
|
+
|
15
|
+
context 'with a type with no values' do
|
16
|
+
before { subject.stub(:type).and_return [] }
|
17
|
+
|
18
|
+
it { should be_valid }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with a type with a single valid value' do
|
22
|
+
before { subject.stub(:type).and_return ['apartment'] }
|
23
|
+
|
24
|
+
it { should be_valid }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with a type with all valid values' do
|
28
|
+
before { subject.stub(:type).and_return ['apartment', 27] }
|
29
|
+
|
30
|
+
it { should be_valid }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a type with some invalid values' do
|
34
|
+
before { subject.stub(:type).and_return ['apartment', :boat] }
|
35
|
+
|
36
|
+
it { should_not be_valid }
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with a type that is not an Array' do
|
40
|
+
before { subject.stub(:type).and_return 'apartment' }
|
41
|
+
|
42
|
+
specify {
|
43
|
+
expect { subject.valid? }.to raise_error(ArgumentError)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# http://jeffkreeftmeijer.com/2011/isolated-testing-for-custom-validators-in-rails-3/
|
4
|
+
|
5
|
+
class ValidatableArr
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
validates :type, array: {in: ['apartment', 'house']}
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ArrayValidator do
|
12
|
+
|
13
|
+
subject { ValidatableArr.new }
|
14
|
+
|
15
|
+
context 'with a type with no values' do
|
16
|
+
before { subject.stub(:type).and_return [] }
|
17
|
+
|
18
|
+
it { should be_valid }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with a type with a single valid value' do
|
22
|
+
before { subject.stub(:type).and_return ['apartment'] }
|
23
|
+
|
24
|
+
it { should be_valid }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with a type with all valid values' do
|
28
|
+
before { subject.stub(:type).and_return ['apartment', 'house'] }
|
29
|
+
|
30
|
+
it { should be_valid }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a type with some invalid values' do
|
34
|
+
before { subject.stub(:type).and_return ['apartment', 'boat'] }
|
35
|
+
|
36
|
+
it { should_not be_valid }
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with a type that is not an Array' do
|
40
|
+
before { subject.stub(:type).and_return 'apartment' }
|
41
|
+
|
42
|
+
specify {
|
43
|
+
expect { subject.valid? }.to raise_error(ArgumentError)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
# http://jeffkreeftmeijer.com/2011/isolated-testing-for-custom-validators-in-rails-3/
|
4
4
|
|
5
|
-
class
|
5
|
+
class ValidatableRange
|
6
6
|
include ActiveModel::Validations
|
7
7
|
|
8
8
|
validates :cost, range: {within: 0..2000}
|
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
describe RangeValidator do
|
12
12
|
|
13
|
-
subject {
|
13
|
+
subject { ValidatableRange.new }
|
14
14
|
|
15
15
|
context 'with a cost range that is not too expensive' do
|
16
16
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_range_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -123,7 +123,12 @@ files:
|
|
123
123
|
- README.md
|
124
124
|
- Rakefile
|
125
125
|
- VERSION
|
126
|
+
- lib/array_types_validator.rb
|
127
|
+
- lib/array_validator.rb
|
126
128
|
- lib/simple_range_validator.rb
|
129
|
+
- simple_range_validator.gemspec
|
130
|
+
- spec/array_types_validator_spec.rb
|
131
|
+
- spec/array_validator_spec.rb
|
127
132
|
- spec/range_validator_spec.rb
|
128
133
|
- spec/spec_helper.rb
|
129
134
|
homepage: http://github.com/kristianmandrup/simple_range_validator
|
@@ -141,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
141
146
|
version: '0'
|
142
147
|
segments:
|
143
148
|
- 0
|
144
|
-
hash:
|
149
|
+
hash: 2744261256725048417
|
145
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
151
|
none: false
|
147
152
|
requirements:
|