va 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/lib/va.rb +15 -5
- data/lib/va/string_validations.rb +15 -0
- data/lib/va/version.rb +1 -1
- data/spec/errors_spec.rb +3 -3
- data/spec/spec_helper.rb +2 -1
- data/spec/string_validations_spec.rb +29 -0
- data/spec/va_spec.rb +34 -7
- data/spec/validations_spec.rb +35 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71852c4aa71d300422814133cf073f6ddef674f3
|
4
|
+
data.tar.gz: 98f5f50bf80557f9e76a2f401845c0754700a39f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbf0f57ab08834eed1ed195ffc4696f332b5f43bc4290e51466b133e99c69ffc158decc1b14ffebd6f3e86bf1da921ec6e562d07c128df62e25a24fd1071f071
|
7
|
+
data.tar.gz: 2ff88a638807598c7e219fc02e0e38f96d827d6cb6a5bc44dd5103db29a68144f65671353f5c85c14b568c880084d0f9ef8d12572d7a4991a4c453273613c0e8
|
data/README.md
CHANGED
@@ -59,6 +59,31 @@ And if you pass a non-declared attribute, it will be ignored:
|
|
59
59
|
# => #<Signup:0x000000015f8dd0 @attributes={:email=>"fede@example.com"}>
|
60
60
|
```
|
61
61
|
|
62
|
+
### Default Values
|
63
|
+
|
64
|
+
You can assign a default value for an attribute, in which case, if no value is provided, it will get assigned either way:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class UserValidator < Va::Model
|
68
|
+
attribute :name, default: "N/A"
|
69
|
+
attribute :admin, default: false
|
70
|
+
attribute :editor, default: true
|
71
|
+
attribute :remember_me, default: true
|
72
|
+
attribute :age
|
73
|
+
attribute :height
|
74
|
+
end
|
75
|
+
|
76
|
+
u = UserValidator.new(age: 30, remember_me: false)
|
77
|
+
u.attributes
|
78
|
+
# => {:name=>"N/A",
|
79
|
+
# :admin=>false,
|
80
|
+
# :editor=>true,
|
81
|
+
# :remember_me=>false,
|
82
|
+
# :age=>30}
|
83
|
+
```
|
84
|
+
|
85
|
+
Note that `age` and `remember_me` were set explicitly, `name`, `admin` and `editor` where set by default, and `height wasn't set at all.
|
86
|
+
|
62
87
|
### Custom Validations
|
63
88
|
|
64
89
|
|
data/lib/va.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "va/version"
|
2
2
|
|
3
3
|
module Va
|
4
|
-
class
|
4
|
+
class Validator
|
5
5
|
attr_reader :attributes
|
6
6
|
attr_reader :errors
|
7
7
|
|
@@ -48,11 +48,21 @@ module Va
|
|
48
48
|
validations << [attrs, msg, block]
|
49
49
|
end
|
50
50
|
|
51
|
-
def self.
|
51
|
+
def self.validate_multiple(*attrs, &block)
|
52
52
|
attrs.each do |attr|
|
53
|
-
validate(attr)
|
54
|
-
|
55
|
-
|
53
|
+
validate(attr, &block)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.validate_present(*attrs)
|
58
|
+
validate_multiple(*attrs) do |attr|
|
59
|
+
attr && attr != ""
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.validate_not_nil(*attrs)
|
64
|
+
validate_multiple(*attrs) do |attr|
|
65
|
+
attr != nil
|
56
66
|
end
|
57
67
|
end
|
58
68
|
|
data/lib/va/version.rb
CHANGED
data/spec/errors_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "./spec/spec_helper"
|
2
2
|
|
3
3
|
scope "errors" do
|
4
|
-
class Truthy < Va::
|
4
|
+
class Truthy < Va::Validator
|
5
5
|
attribute :t
|
6
6
|
validate(:t, "not truthy") do |t|
|
7
7
|
t
|
@@ -14,7 +14,7 @@ scope "errors" do
|
|
14
14
|
@errors == { t: "not truthy"}
|
15
15
|
end
|
16
16
|
|
17
|
-
class Falsey < Va::
|
17
|
+
class Falsey < Va::Validator
|
18
18
|
attribute :f1
|
19
19
|
attribute :f2
|
20
20
|
validate(:f1, :f2, "not falsey") do |f1, f2|
|
@@ -28,7 +28,7 @@ scope "errors" do
|
|
28
28
|
@errors == { [:f1, :f2] => "not falsey"}
|
29
29
|
end
|
30
30
|
|
31
|
-
class WithoutMessages < Va::
|
31
|
+
class WithoutMessages < Va::Validator
|
32
32
|
attribute :a
|
33
33
|
attribute :b
|
34
34
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
$:.unshift(File.expand_path("../lib/", __dir__))
|
2
|
+
require "va"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "./spec/spec_helper"
|
2
|
+
|
3
|
+
require "va/string_validations"
|
4
|
+
|
5
|
+
scope "string validations" do
|
6
|
+
scope "is string" do
|
7
|
+
class Person < Va::Validator
|
8
|
+
include StringValidations
|
9
|
+
attribute :name
|
10
|
+
|
11
|
+
validate_string(:name)
|
12
|
+
end
|
13
|
+
|
14
|
+
spec "is a string" do
|
15
|
+
@p = Person.new(name: "Fede")
|
16
|
+
@p.valid?
|
17
|
+
end
|
18
|
+
|
19
|
+
spec "not a string" do
|
20
|
+
@p = Person.new(name: 5)
|
21
|
+
! @p.valid?
|
22
|
+
end
|
23
|
+
|
24
|
+
spec "accepts nils" do
|
25
|
+
@p = Person.new
|
26
|
+
@p.valid?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/va_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "./spec/spec_helper"
|
2
2
|
|
3
3
|
scope do
|
4
|
-
class Login < Va::
|
4
|
+
class Login < Va::Validator
|
5
5
|
attribute :email
|
6
6
|
attribute :pass
|
7
7
|
end
|
@@ -33,7 +33,7 @@ end
|
|
33
33
|
|
34
34
|
scope "custom validations" do
|
35
35
|
spec "basic passing validation" do
|
36
|
-
class VeryValid < Va::
|
36
|
+
class VeryValid < Va::Validator
|
37
37
|
validate do
|
38
38
|
true
|
39
39
|
end
|
@@ -44,7 +44,7 @@ scope "custom validations" do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
spec "basic passing validation" do
|
47
|
-
class VeryInvalid < Va::
|
47
|
+
class VeryInvalid < Va::Validator
|
48
48
|
validate do
|
49
49
|
false
|
50
50
|
end
|
@@ -55,7 +55,7 @@ scope "custom validations" do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
scope "A range validation" do
|
58
|
-
class MyRange < Va::
|
58
|
+
class MyRange < Va::Validator
|
59
59
|
attribute :from
|
60
60
|
attribute :to
|
61
61
|
|
@@ -78,7 +78,7 @@ scope "custom validations" do
|
|
78
78
|
scope "can't validate" do
|
79
79
|
spec "invalid arguments" do
|
80
80
|
begin
|
81
|
-
class
|
81
|
+
class FaceLidator < Va::Validator
|
82
82
|
attribute :face
|
83
83
|
|
84
84
|
validate(:leg) do |leg|
|
@@ -92,8 +92,35 @@ scope "custom validations" do
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
+
scope "validate multiple" do
|
96
|
+
class Name < Va::Validator
|
97
|
+
attribute :first
|
98
|
+
attribute :last
|
99
|
+
|
100
|
+
validate_multiple(:first, :last) do |attr|
|
101
|
+
attr.size > 0
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
spec "passing" do
|
106
|
+
@va = Name.new(first: "Federico", last: "Iachetti")
|
107
|
+
@va.valid?
|
108
|
+
end
|
109
|
+
|
110
|
+
spec "all failing" do
|
111
|
+
@va = Name.new(first: "", last: "")
|
112
|
+
! @va.valid?
|
113
|
+
end
|
114
|
+
|
115
|
+
spec "one failing" do
|
116
|
+
@va = Name.new(first: "Federico", last: "")
|
117
|
+
! @va.valid?
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
95
122
|
scope "default values" do
|
96
|
-
class MyDefaults < Va::
|
123
|
+
class MyDefaults < Va::Validator
|
97
124
|
attribute :name, default: "N/A"
|
98
125
|
attribute :age
|
99
126
|
end
|
@@ -103,7 +130,7 @@ scope "default values" do
|
|
103
130
|
@attributes == {name: "N/A", age: 30}
|
104
131
|
end
|
105
132
|
|
106
|
-
class BooleanDefaults < Va::
|
133
|
+
class BooleanDefaults < Va::Validator
|
107
134
|
attribute :me_true, default: true
|
108
135
|
attribute :me_false, default: false
|
109
136
|
attribute :not_me
|
data/spec/validations_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require "./spec/spec_helper"
|
|
2
2
|
|
3
3
|
scope "basic validations" do
|
4
4
|
scope "non-blank" do
|
5
|
-
class ANonBlankAttribute < Va::
|
5
|
+
class ANonBlankAttribute < Va::Validator
|
6
6
|
attribute :name
|
7
7
|
attribute :age
|
8
8
|
validate_present(:name, :age)
|
@@ -28,4 +28,38 @@ scope "basic validations" do
|
|
28
28
|
! @va.valid?
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
scope "non-nil" do
|
33
|
+
class ANotNilAttribute < Va::Validator
|
34
|
+
attribute :name
|
35
|
+
attribute :age
|
36
|
+
validate_not_nil(:name, :age)
|
37
|
+
end
|
38
|
+
|
39
|
+
spec "passing" do
|
40
|
+
@va = ANotNilAttribute.new(name: "Fede", age: :of_ultron)
|
41
|
+
@va.valid?
|
42
|
+
end
|
43
|
+
|
44
|
+
spec "one empty string" do
|
45
|
+
@va = ANotNilAttribute.new(name: "", age: :of_ultron)
|
46
|
+
@va.valid?
|
47
|
+
end
|
48
|
+
|
49
|
+
spec "one nil" do
|
50
|
+
@va = ANotNilAttribute.new(name: "Fede", age: nil)
|
51
|
+
! @va.valid?
|
52
|
+
end
|
53
|
+
|
54
|
+
spec "both empty" do
|
55
|
+
@va = ANotNilAttribute.new(name: nil, age: "")
|
56
|
+
! @va.valid?
|
57
|
+
end
|
58
|
+
|
59
|
+
spec "both nil" do
|
60
|
+
@va = ANotNilAttribute.new(name: nil, age: nil)
|
61
|
+
! @va.valid?
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
31
65
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: va
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,9 +67,11 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- lib/va.rb
|
70
|
+
- lib/va/string_validations.rb
|
70
71
|
- lib/va/version.rb
|
71
72
|
- spec/errors_spec.rb
|
72
73
|
- spec/spec_helper.rb
|
74
|
+
- spec/string_validations_spec.rb
|
73
75
|
- spec/va_spec.rb
|
74
76
|
- spec/validations_spec.rb
|
75
77
|
- va.gemspec
|
@@ -100,5 +102,6 @@ summary: Minimalistic framework agnostic Validator.
|
|
100
102
|
test_files:
|
101
103
|
- spec/errors_spec.rb
|
102
104
|
- spec/spec_helper.rb
|
105
|
+
- spec/string_validations_spec.rb
|
103
106
|
- spec/va_spec.rb
|
104
107
|
- spec/validations_spec.rb
|