va 0.0.5 → 0.1.0
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/.travis.yml +8 -0
- data/README.md +96 -2
- data/lib/va.rb +8 -0
- data/lib/va/version.rb +1 -1
- data/spec/va_spec.rb +2 -1
- data/spec/validations_spec.rb +30 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed2280383b5979b1328c2ad7c89e8b957d2aaea5
|
4
|
+
data.tar.gz: a1b6a523e79f3bfd85c039b3797bc3c65b262b3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cf04a3feeac74cc8b7c666c07a348704d6720b4d615cef8993f3d6477bdafce054b86f80e83fed8af7a80e8f9c33488819cdda93f391fb73fdc29b5623daf92
|
7
|
+
data.tar.gz: 2654139e9d6b4c614089c3de4950c3fe0ee928ad830fbace45256bddd719a4b3a14d9d8d82fe97ac332c265f5d857209255c2d800f75fb5350bf5288c2e325db
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,12 +1,106 @@
|
|
1
1
|
# Va
|
2
2
|
|
3
|
-
Va is a minimalistic
|
3
|
+
Va is a minimalistic validation library.
|
4
4
|
|
5
5
|
It is meant to be used as a first line of defense from external imputs.
|
6
6
|
|
7
|
+
Va doesnn't care what framework you're using, it just provides a good way to set and validate attributes in a model.
|
8
|
+
|
7
9
|
## Usage
|
8
10
|
|
9
|
-
|
11
|
+
### Attributes
|
12
|
+
|
13
|
+
Say you need a signup form validator that only allows an email, a password and the password confirmation.
|
14
|
+
The first thing to do is to create the corresponding model
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
class Signup < Va::Model
|
18
|
+
attribute :email
|
19
|
+
attribute :password
|
20
|
+
attribute :password_confirmation
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
And then you can instantiate it like this:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
s = Signup.new(email: "fede@example.com", password: "123456", password_confirmation: "123456")
|
28
|
+
# => #<Signup:0x00000001cc90d8
|
29
|
+
# @attributes=
|
30
|
+
# {:email=>"fede@example.com",
|
31
|
+
# :password=>"123456",
|
32
|
+
# :password_confirmation=>"123456"}>
|
33
|
+
```
|
34
|
+
|
35
|
+
As you can see, you can use either Strings or Symbols as keys (to be fair, it allows any object that responds to the `#to_sym` method)
|
36
|
+
|
37
|
+
You can check the values of the attributes using the `#attributes` method.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
s.attributes
|
41
|
+
# => {:email=>"fede@example.com",
|
42
|
+
# :password=>"123456",
|
43
|
+
# :password_confirmation=>"123456"}
|
44
|
+
```
|
45
|
+
|
46
|
+
If you miss an argument, it won't appear on the attributes list:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
s = Signup.new(email: "fede@example.com", "password_confirmation" => "123456")
|
50
|
+
s.attributes
|
51
|
+
# => {:email=>"fede@example.com",
|
52
|
+
# :password_confirmation=>"123456"}
|
53
|
+
```
|
54
|
+
|
55
|
+
And if you pass a non-declared attribute, it will be ignored:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
s = Signup.new(email: "fede@example.com", phone_number: "987654321")
|
59
|
+
# => #<Signup:0x000000015f8dd0 @attributes={:email=>"fede@example.com"}>
|
60
|
+
```
|
61
|
+
|
62
|
+
### Custom Validations
|
63
|
+
|
64
|
+
|
65
|
+
Up until here, we haven't talked about validations.
|
66
|
+
|
67
|
+
Va allows you to write generic validations.
|
68
|
+
|
69
|
+
For example, if we need the email to be present and the password and password validation to match, we can do it as it follows:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
s = Signup.new(email: "fede@example.com", password: "a", password_confirmation: "a")
|
73
|
+
s.valid?
|
74
|
+
# => true
|
75
|
+
|
76
|
+
t = Signup.new(password: "a", password_confirmation: "a")
|
77
|
+
t.valid?
|
78
|
+
# => false
|
79
|
+
|
80
|
+
u = Signup.new(email: "fede@example.com", password: "a", password_confirmation: "b")
|
81
|
+
u.valid?
|
82
|
+
# => false
|
83
|
+
```
|
84
|
+
|
85
|
+
### Predefined Validations
|
86
|
+
|
87
|
+
#### Blank
|
88
|
+
|
89
|
+
If you want to validate that a field is not blank, you can just say:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class Person < Va::Model
|
93
|
+
attribute :name
|
94
|
+
attribute :age
|
95
|
+
validate_present(:name, :age)
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
And if any of those attributes is either nil or an empty string, the validation will fail.
|
100
|
+
|
101
|
+
#### More Validations
|
102
|
+
|
103
|
+
TODO: Add more validations
|
10
104
|
|
11
105
|
## Installation
|
12
106
|
|
data/lib/va.rb
CHANGED
data/lib/va/version.rb
CHANGED
data/spec/va_spec.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "./spec/spec_helper"
|
2
|
+
|
3
|
+
scope "basic validations" do
|
4
|
+
scope "non-blank" do
|
5
|
+
class ANonBlankAttribute < Va::Model
|
6
|
+
attribute :name
|
7
|
+
attribute :age
|
8
|
+
validate_present(:name, :age)
|
9
|
+
end
|
10
|
+
test "passing" do
|
11
|
+
va = ANonBlankAttribute.new(name: "Fede", age: :of_ultron)
|
12
|
+
assert_equal va.valid?, true
|
13
|
+
end
|
14
|
+
|
15
|
+
test "one empty string" do
|
16
|
+
va = ANonBlankAttribute.new(name: "", age: :of_ultron)
|
17
|
+
assert_equal va.valid?, false
|
18
|
+
end
|
19
|
+
|
20
|
+
test "one nil" do
|
21
|
+
va = ANonBlankAttribute.new(name: "Fede", age: nil)
|
22
|
+
assert_equal va.valid?, false
|
23
|
+
end
|
24
|
+
|
25
|
+
test "both empty" do
|
26
|
+
va = ANonBlankAttribute.new(name: nil, age: "")
|
27
|
+
assert_equal va.valid?, false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: va
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
@@ -61,6 +61,7 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
|
+
- ".travis.yml"
|
64
65
|
- Gemfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
@@ -69,6 +70,7 @@ files:
|
|
69
70
|
- lib/va/version.rb
|
70
71
|
- spec/spec_helper.rb
|
71
72
|
- spec/va_spec.rb
|
73
|
+
- spec/validations_spec.rb
|
72
74
|
- va.gemspec
|
73
75
|
homepage: ''
|
74
76
|
licenses:
|
@@ -97,3 +99,4 @@ summary: Minimalistic framework agnostic Validator.
|
|
97
99
|
test_files:
|
98
100
|
- spec/spec_helper.rb
|
99
101
|
- spec/va_spec.rb
|
102
|
+
- spec/validations_spec.rb
|