validate 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -5
- data/lib/validate/validator.rb +8 -5
- data/lib/validate/version.rb +1 -1
- data/spec/validate_spec.rb +25 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# Validate
|
2
2
|
|
3
|
-
|
3
|
+
Validate is a validations library that can validate *any* object that can be
|
4
4
|
converted to a hash using `to_hash`.
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
|
-
gem '
|
10
|
+
gem 'validate'
|
11
11
|
|
12
12
|
And then execute:
|
13
13
|
|
@@ -15,7 +15,7 @@ And then execute:
|
|
15
15
|
|
16
16
|
Or install it yourself as:
|
17
17
|
|
18
|
-
$ gem install
|
18
|
+
$ gem install validate
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
@@ -35,7 +35,7 @@ module Store
|
|
35
35
|
|
36
36
|
# Order matters, first include library. Also: validations are
|
37
37
|
# executed and checked in the order in which they are defined.
|
38
|
-
include
|
38
|
+
include Validate
|
39
39
|
|
40
40
|
# Validations are contained within a block, so as to not pollute
|
41
41
|
# the class namespace with a DSL's implementation.
|
data/lib/validate/validator.rb
CHANGED
@@ -11,15 +11,18 @@ module Validate
|
|
11
11
|
|
12
12
|
def validates?(context)
|
13
13
|
bool = @validations
|
14
|
-
.select do |v|
|
15
|
-
# `:when` is a special case, this gets processed right away and
|
16
|
-
# filtered out...
|
17
|
-
!(v[:opts] || {})[:when].is_a?(Proc) || context.instance_exec(&v[:opts][:when])
|
18
|
-
end
|
19
14
|
.map do |v|
|
20
15
|
# destructure fields
|
21
16
|
v[:fields].map {|f| v.merge(fields: f) }
|
22
17
|
end.flatten(1)
|
18
|
+
.select do |v|
|
19
|
+
# `:when` is a special case, this gets processed right away and
|
20
|
+
# filtered out...
|
21
|
+
when_opt = (v[:opts] || {})[:when]
|
22
|
+
# :is_set is short for checking if then field is set
|
23
|
+
when_opt = -> { self.to_hash.include?(v[:fields]) } if when_opt == :is_set
|
24
|
+
!when_opt.is_a?(Proc) || context.instance_exec(&when_opt)
|
25
|
+
end
|
23
26
|
.map do |v|
|
24
27
|
# lastly, execute validation
|
25
28
|
validator = if v[:validations]
|
data/lib/validate/version.rb
CHANGED
data/spec/validate_spec.rb
CHANGED
@@ -86,6 +86,31 @@ describe Validate do
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
+
context 'when option' do
|
90
|
+
before do
|
91
|
+
class TestClass < BaseTestClass
|
92
|
+
validations do
|
93
|
+
validates_type_of :name, is: Symbol, when: :is_set
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'validates when when does not match' do
|
99
|
+
test = TestClass.new(something: false)
|
100
|
+
test.validates?.should == true
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'validates when when does match' do
|
104
|
+
test = TestClass.new(name: :sym)
|
105
|
+
test.validates?.should == true
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'fails' do
|
109
|
+
test = TestClass.new(name: 'me')
|
110
|
+
test.validates?.should == false
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
89
114
|
context 'run_when block' do
|
90
115
|
before do
|
91
116
|
class TestClass < BaseTestClass
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|