assent 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b52124be104c1cc804575ecf7ccd64d10a19344
4
- data.tar.gz: 35d85119557e5d1bd50d6e4e856104909dbb04d5
3
+ metadata.gz: 9c0c29bf9a81d516278a384e5ecbda23491a59c3
4
+ data.tar.gz: d364f35bc2511854c46d8a2daf3ddad6d44d6b9f
5
5
  SHA512:
6
- metadata.gz: 22e0145cca7875097063146a46fcc3a816230c2ad2693bcb7ab1d040a403cd383e9457b50ad1411e9ae500fafcc69d8d0b56519785c54c239e77ce88ca1ccff4
7
- data.tar.gz: 175aa27110bcfaf38311a65ec3eaa6cf94d079ee3a267438fbe139388cb7de1139185d0283a0c7a070ec2955f8a82a91a3218732c81ecc5020e4b987aa0eccaf
6
+ metadata.gz: 3f486a3fb8eae727e2df5fa63daaf23cb55687c37338044af86c886702a4acfbbfcc875358ae429583d97d424873198fce28e3f44800ac0b60c7704c8f2fca97
7
+ data.tar.gz: 224178c48484dac53302fe4800aff637f1d417a7954ba015b0ae1a659c24661f2c3f76fd07e195d32cfe3e5b0b125f3b4e2d8056347342eaee85ae186da22d74
data/README.md CHANGED
@@ -26,24 +26,88 @@ Or install it yourself as:
26
26
 
27
27
  **NOTE:** This gem is still under heavy development.
28
28
 
29
- #### Basic Usage
29
+ ### Basic Usage
30
30
 
31
31
  ```ruby
32
32
  require 'assent'
33
33
 
34
+ # Define a validator that inherits from the assent validator
34
35
  class LoginValidator < Assent::Validator
35
36
 
36
37
  field :email, presence: true
37
38
  field :password, presence: true
38
39
  end
40
+
41
+ # instantiate a new instance of the LoginValidator, f.e. in your controller
42
+ class LoginController < ApplicationController
43
+ def index
44
+ validator = LoginValidator.new
45
+ if validator.validate(params) #this will return a boolean
46
+ # render something
47
+ else
48
+ #handle validation errors
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ As you can see above, the ```validate``` method accepts the input. Right now, it only accepts a ```Hash```.
55
+
56
+ #### Supported validations
57
+
58
+ * presence
59
+ * accepted
60
+ * date
61
+ * ip
62
+
63
+ ### Accessing errors
64
+
65
+ If the validation failed, you might want to show what went wrong. In that case you can access the errors as follows:
66
+
67
+ ```ruby
68
+
69
+ class LoginValidator < Assent::Validator
70
+
71
+ field :email, presence: true
72
+ field :password, presence: true
73
+ end
74
+
75
+ input = Hash.new
76
+ input[:email] = ''
77
+
78
+ validator = LoginValidator.new
79
+ validator.validate(input)
80
+ validator.errors # This will return {:email => ['The email is required']}
39
81
  ```
40
82
 
41
- ## Development
42
83
 
43
- **TODO**
44
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+ ### Setting custom error messages
85
+
86
+ You might want to create your own error messages and luckily you can override the defaults. All you need to do is creating a YAML language file and tell us where to find it.
87
+
88
+ Inside the YAML file you have access to the ```field``` variable. This variable will automatically be replaced with the field the validation is for.
89
+
90
+ The YAML file should look like this:
91
+
92
+ ```yaml
93
+ presence: This is my custom message for :field
94
+ ```
45
95
 
46
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
96
+ Supported overrides are:
97
+
98
+ * presence
99
+ * accepted
100
+ * date
101
+ * ip
102
+
103
+ To load your configuration file, just use the ```config_file``` method on ```Assent::Config```:
104
+
105
+ ```ruby
106
+ #some place where your configuration lives
107
+
108
+ Assent::Config.config_file 'path/to/yaml/file'
109
+
110
+ ```
47
111
 
48
112
  ## Contributing
49
113
 
@@ -10,17 +10,25 @@ module Assent
10
10
  include Options
11
11
  include Config
12
12
 
13
- def errors
13
+ def errors_class
14
14
  @errors ||= Errors.new
15
15
  end
16
16
 
17
+ def errors
18
+ errors_class.errors
19
+ end
20
+
21
+ # This validate method will validate the input against the rules. At the end it returns true or false.
22
+ # If the errors are empty, it means the validation passed
17
23
  def validate(input)
18
24
  @@validations.each do |field, validations|
19
25
  value = input[field]
20
26
  value = input[field].to_s if value.nil?
21
- validate = Validate.new(field, value, validations, errors)
27
+ validate = Validate.new(field, value, validations, errors_class)
22
28
  validate.validate
23
29
  end
30
+
31
+ errors.empty?
24
32
  end
25
33
 
26
34
  end
@@ -1,3 +1,3 @@
1
1
  module Assent
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,5 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class LoginValidator < Assent::Validator
4
+ field :name, presence: true
5
+ end
6
+
7
+ describe Assent::Validator do
8
+ let(:validator) { LoginValidator.new }
9
+
10
+ describe '.validate' do
11
+ it 'validates the input against the rules and returns true when valid' do
12
+ input = Hash.new
13
+ input[:name] = 'Frank'
14
+ expect(validator.validate(input)).to eq(true)
15
+ end
16
+
17
+ it 'returns false when the input is not valid' do
18
+ input = Hash.new
19
+ expect(validator.validate(input)).to eq(false)
20
+ end
21
+
22
+ it 'fills the errors when the input is not valid' do
23
+ input = Hash.new
24
+ validator.validate(input)
25
+ expect(validator.errors).not_to be_empty
26
+ expect(validator.errors).to eq({:name => ["The name is required."]})
27
+ end
28
+ end
29
+ end
30
+
3
31
  describe Assent::Validate do
4
32
  let(:error) { Assent::Errors.new { include Assent::Config }}
5
33
  let(:validator) { Assent::Validate }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank
@@ -88,7 +88,7 @@ files:
88
88
  - spec/validator_spec.rb
89
89
  - spec/version_spec.rb
90
90
  - test.rb
91
- homepage: https://rubygems.org
91
+ homepage: https://www.github.com/frankieleef/assent
92
92
  licenses:
93
93
  - MIT
94
94
  metadata: