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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9ebf46848bd833c96256fac55089f6bdce797d3
4
- data.tar.gz: bb14d921880db063a2f08083fd35fe5ee56e5b63
3
+ metadata.gz: ed2280383b5979b1328c2ad7c89e8b957d2aaea5
4
+ data.tar.gz: a1b6a523e79f3bfd85c039b3797bc3c65b262b3e
5
5
  SHA512:
6
- metadata.gz: 3a86ef475bc99f931a54fef45562f01873cc69f5dceb309137cd15581e8269eda0f6d8e9fb8f2e84dc52603ebaafcf8b004e7596cbc5eb89fd8d5bc099bbbda4
7
- data.tar.gz: 46e078c1071d3b8c4d7155d0a8f6fa311782f410c602e2ab7c5a60e563313e70a0534408b735ef7619556ff7bf7b7d314d0a823b39456b1abd5c0fe496df3dca
6
+ metadata.gz: 5cf04a3feeac74cc8b7c666c07a348704d6720b4d615cef8993f3d6477bdafce054b86f80e83fed8af7a80e8f9c33488819cdda93f391fb73fdc29b5623daf92
7
+ data.tar.gz: 2654139e9d6b4c614089c3de4950c3fe0ee928ad830fbace45256bddd719a4b3a14d9d8d82fe97ac332c265f5d857209255c2d800f75fb5350bf5288c2e325db
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.1.0
6
+ - 2.1.1
7
+ - 2.1.2
8
+ - ruby-head
data/README.md CHANGED
@@ -1,12 +1,106 @@
1
1
  # Va
2
2
 
3
- Va is a minimalistic, framework agnostic, validation library.
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
- TODO: Write usage instructions here
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
@@ -30,6 +30,14 @@ module Va
30
30
  validations << [attrs, block]
31
31
  end
32
32
 
33
+ def self.validate_present(*attrs)
34
+ attrs.each do |attr|
35
+ validate(attr) do |a|
36
+ a && a != ""
37
+ end
38
+ end
39
+ end
40
+
33
41
  def self.keys
34
42
  @keys ||= []
35
43
  end
@@ -1,3 +1,3 @@
1
1
  module Va
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -29,7 +29,7 @@ scope do
29
29
  end
30
30
  end
31
31
  end
32
- scope "validations" do
32
+ scope "custom validations" do
33
33
  test "basic passing validation" do
34
34
  class VeryValid < Va::Model
35
35
  validate do
@@ -90,3 +90,4 @@ scope "validations" do
90
90
  end
91
91
  end
92
92
  end
93
+
@@ -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.5
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