assent 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +38 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lang.yml +1 -0
- data/lib/assent/config/config.rb +43 -0
- data/lib/assent/config/lang.yml +4 -0
- data/lib/assent/exceptions/config_exception.rb +4 -0
- data/lib/assent/validations/errors.rb +28 -0
- data/lib/assent/validations/field.rb +24 -0
- data/lib/assent/validations/options.rb +21 -0
- data/lib/assent/validations/validation_error.rb +12 -0
- data/lib/assent/validations/validations_set.rb +14 -0
- data/lib/assent/validations/validator.rb +101 -0
- data/lib/assent/validator.rb +27 -0
- data/lib/assent/version.rb +3 -0
- data/lib/assent.rb +2 -0
- data/spec/config_spec.rb +52 -0
- data/spec/field_spec.rb +15 -0
- data/spec/helpers/config.yml +1 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/validator_spec.rb +78 -0
- data/spec/version_spec.rb +8 -0
- data/test.rb +19 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2b52124be104c1cc804575ecf7ccd64d10a19344
|
4
|
+
data.tar.gz: 35d85119557e5d1bd50d6e4e856104909dbb04d5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 22e0145cca7875097063146a46fcc3a816230c2ad2693bcb7ab1d040a403cd383e9457b50ad1411e9ae500fafcc69d8d0b56519785c54c239e77ce88ca1ccff4
|
7
|
+
data.tar.gz: 175aa27110bcfaf38311a65ec3eaa6cf94d079ee3a267438fbe139388cb7de1139185d0283a0c7a070ec2955f8a82a91a3218732c81ecc5020e4b987aa0eccaf
|
data/.gitignore
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalization:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
37
|
+
|
38
|
+
assent-*
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
assent (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.4.0)
|
12
|
+
rspec-core (~> 3.4.0)
|
13
|
+
rspec-expectations (~> 3.4.0)
|
14
|
+
rspec-mocks (~> 3.4.0)
|
15
|
+
rspec-core (3.4.4)
|
16
|
+
rspec-support (~> 3.4.0)
|
17
|
+
rspec-expectations (3.4.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.4.0)
|
20
|
+
rspec-mocks (3.4.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.4.0)
|
23
|
+
rspec-support (3.4.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
assent!
|
30
|
+
bundler (~> 1.11)
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.11.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Frank
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/frankieleef/assent.svg?branch=develop)](https://travis-ci.org/frankieleef/assent)
|
2
|
+
|
3
|
+
# Assent
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/assent`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
TODO: Delete this and the text above, and describe your gem
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'assent'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install assent
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
**NOTE:** This gem is still under heavy development.
|
28
|
+
|
29
|
+
#### Basic Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'assent'
|
33
|
+
|
34
|
+
class LoginValidator < Assent::Validator
|
35
|
+
|
36
|
+
field :email, presence: true
|
37
|
+
field :password, presence: true
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
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.
|
45
|
+
|
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).
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/frankieleef/assent.
|
51
|
+
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "assent"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lang.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
presence: The :field is required because I say so.
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'assent/exceptions/config_exception'
|
3
|
+
|
4
|
+
module Assent
|
5
|
+
module Config
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
if Config.config.empty?
|
9
|
+
lang = YAML.load_file(File.dirname(__FILE__) + '/lang.yml')
|
10
|
+
|
11
|
+
lang.each do |key, value|
|
12
|
+
Config.add(key, value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.config_file(path)
|
18
|
+
begin
|
19
|
+
lang = YAML.load_file(path)
|
20
|
+
lang.each do |key, value|
|
21
|
+
add(key, value)
|
22
|
+
end
|
23
|
+
rescue Errno::ENOENT
|
24
|
+
raise ConfigException.new "Config file could not be found."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.add(option, value)
|
29
|
+
config[option] = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.config
|
33
|
+
@@config ||= Hash.new
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def set_empty_config
|
39
|
+
@@config = Hash.new
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'assent/config/config'
|
2
|
+
|
3
|
+
module Assent
|
4
|
+
class Errors
|
5
|
+
include Config
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@errors = Hash.new
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(field, rule)
|
13
|
+
text = String.new(Config.config[rule.to_s])
|
14
|
+
replace(text, field)
|
15
|
+
@errors[field] ||= []
|
16
|
+
@errors[field].push(text)
|
17
|
+
end
|
18
|
+
|
19
|
+
def errors
|
20
|
+
@errors
|
21
|
+
end
|
22
|
+
|
23
|
+
def replace(text, field)
|
24
|
+
message = text.sub! ':field', field.to_s
|
25
|
+
return message
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'assent/validations/validations_set'
|
2
|
+
|
3
|
+
module Assent
|
4
|
+
module Field
|
5
|
+
include ValidationsSet
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_field(name, options)
|
12
|
+
add(name, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
include Field
|
17
|
+
|
18
|
+
def field(name, options = {}, &block)
|
19
|
+
add_field name, options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Assent
|
2
|
+
module Options
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.options
|
9
|
+
@options ||= Hash.new
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
include Options
|
14
|
+
|
15
|
+
def set(option, value)
|
16
|
+
Options.options[option] = value
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'assent/validations/options'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
module Assent
|
5
|
+
class Validate
|
6
|
+
include Options
|
7
|
+
|
8
|
+
def initialize(field, value, rules, errors)
|
9
|
+
@field = field
|
10
|
+
@value = value
|
11
|
+
@rules = rules
|
12
|
+
@errors = errors
|
13
|
+
end
|
14
|
+
# Validate method that checks all the validations for a single field
|
15
|
+
def validate
|
16
|
+
required
|
17
|
+
accepted
|
18
|
+
|
19
|
+
return if blank?
|
20
|
+
|
21
|
+
date
|
22
|
+
ip
|
23
|
+
|
24
|
+
@errors
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Validates the presence of a field
|
30
|
+
#
|
31
|
+
# Example:
|
32
|
+
#
|
33
|
+
# class LoginValidator < Assent::Validator
|
34
|
+
# field :email, presence: true
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# credentials = Hash.new
|
38
|
+
# credentials[:email] = nil
|
39
|
+
# LoginValidator.new.errors will contain errors because of a nil value
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# credentials[:email] = "test@example.com"
|
43
|
+
# This will pass
|
44
|
+
def required
|
45
|
+
validation(:presence) { !blank? }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Validates if a field has the value true, 1 or yes
|
49
|
+
#
|
50
|
+
# Example:
|
51
|
+
# class TermsValidator < Assent::Validator
|
52
|
+
# field :terms, accepted: true
|
53
|
+
# end
|
54
|
+
def accepted
|
55
|
+
validation(:accepted) { @value == true || @value == 1}
|
56
|
+
end
|
57
|
+
|
58
|
+
# Validates if a field is a valid date
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# class RegistrationValidator < Assent::validator
|
62
|
+
# field :date, date: true
|
63
|
+
# end
|
64
|
+
def date
|
65
|
+
validation(:date) do |validator|
|
66
|
+
date = Date.parse(@value) rescue nil
|
67
|
+
|
68
|
+
!date.nil?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Validates if a field is a valid ip-address
|
73
|
+
#
|
74
|
+
# Example:
|
75
|
+
# class IpValidator < Assent::Validator
|
76
|
+
# field :ip, ip: true
|
77
|
+
# end
|
78
|
+
def ip
|
79
|
+
validation(:ip) do |validator|
|
80
|
+
result = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/.match(@value.to_s)
|
81
|
+
|
82
|
+
!result.nil?
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Checks if the given value is nil or empty
|
87
|
+
def blank?
|
88
|
+
@value.nil? || @value.empty?
|
89
|
+
end
|
90
|
+
|
91
|
+
def validation(rule)
|
92
|
+
validator = @rules[rule]
|
93
|
+
|
94
|
+
if(validator) && (!yield validator)
|
95
|
+
@errors.add(@field, rule)
|
96
|
+
end
|
97
|
+
|
98
|
+
yield validator
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'assent/validations/field'
|
2
|
+
require 'assent/validations/options'
|
3
|
+
require 'assent/config/config'
|
4
|
+
require 'assent/validations/validator'
|
5
|
+
require 'assent/validations/errors'
|
6
|
+
|
7
|
+
module Assent
|
8
|
+
class Validator
|
9
|
+
include Field
|
10
|
+
include Options
|
11
|
+
include Config
|
12
|
+
|
13
|
+
def errors
|
14
|
+
@errors ||= Errors.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate(input)
|
18
|
+
@@validations.each do |field, validations|
|
19
|
+
value = input[field]
|
20
|
+
value = input[field].to_s if value.nil?
|
21
|
+
validate = Validate.new(field, value, validations, errors)
|
22
|
+
validate.validate
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/assent.rb
ADDED
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Assent::Config do
|
4
|
+
|
5
|
+
let(:config) { Class.new {
|
6
|
+
include Assent::Config
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(option, value)
|
13
|
+
Assent::Config.add(option, value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def config_file(path)
|
17
|
+
puts path
|
18
|
+
Assent::Config.config_file(path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def config
|
22
|
+
Assent::Config.config
|
23
|
+
end
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
|
28
|
+
describe '.config_file' do
|
29
|
+
it 'loads the default config when no config file is set' do
|
30
|
+
config_class = config.new
|
31
|
+
expect(config_class.config["presence"]).to eq("The :field is required.")
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'adds custom config to the config hash' do
|
35
|
+
config_class = config.new
|
36
|
+
config_class.config_file(File.dirname(__FILE__) + '/helpers/config.yml')
|
37
|
+
expect(config_class.config["presence"]).to eq("This is custom config for :field")
|
38
|
+
end
|
39
|
+
|
40
|
+
after(:all) do
|
41
|
+
config_class = Class.new{
|
42
|
+
include Assent::Config
|
43
|
+
|
44
|
+
def initialize
|
45
|
+
super
|
46
|
+
end
|
47
|
+
}
|
48
|
+
|
49
|
+
config_class.new.send(:set_empty_config)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/field_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Assent::Field do
|
4
|
+
describe '.field' do
|
5
|
+
let(:field) { Class.new{ include Assent::Field }}
|
6
|
+
|
7
|
+
it 'adds a field to a hash' do
|
8
|
+
rules = Hash.new
|
9
|
+
rules[:name] = {:presence=>true}
|
10
|
+
field.field :name, presence: true
|
11
|
+
expect(field.validations).to eq(rules)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
presence: This is custom config for :field
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Assent::Validate do
|
4
|
+
let(:error) { Assent::Errors.new { include Assent::Config }}
|
5
|
+
let(:validator) { Assent::Validate }
|
6
|
+
|
7
|
+
describe '.required' do
|
8
|
+
|
9
|
+
it 'checks if a field is not blank' do
|
10
|
+
validation = validator.new(:name, "Frank", {:presence=>true}, error)
|
11
|
+
expect(validation.send(:required)).to eq(true)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "adds an error when a field is required and empty" do
|
15
|
+
validation = validator.new(:name, nil, {:presence=>true}, error)
|
16
|
+
validation.send(:required)
|
17
|
+
expect(error.errors).to eq({:name => ["The name is required."]})
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.accepted' do
|
22
|
+
it 'checks if a field is accepted' do
|
23
|
+
validation = validator.new(:terms, true, {:accepted=>true}, error)
|
24
|
+
expect(validation.send(:accepted)).to eq(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'returns false when a field is not accepted' do
|
28
|
+
validation = validator.new(:terms, false, {:accepted=>true}, error)
|
29
|
+
expect(validation.send(:accepted)).to eq(false)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'adds an error when a field is not accepted' do
|
33
|
+
validation = validator.new(:terms, false, {:accepted=>true}, error)
|
34
|
+
validation.send(:accepted)
|
35
|
+
expect(error.errors).to eq({:terms => ["The terms is not accepted."]})
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '.date' do
|
40
|
+
it 'checks if a field is a valid date' do
|
41
|
+
validation = validator.new(:date, '20-11-1992', {:date=>true}, error)
|
42
|
+
expect(validation.send(:date)).to eq(true)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns false when a field is not a valid date' do
|
46
|
+
validation = validator.new(:date, '200-11-1992', {:date=>true}, error)
|
47
|
+
expect(validation.send(:date)).to eq(false)
|
48
|
+
validation2 = validator.new(:date, nil, {:date=>true}, error)
|
49
|
+
expect(validation2.send(:date)).to eq(false)
|
50
|
+
validation3 = validator.new(:date, "", {:date=>true}, error)
|
51
|
+
expect(validation3.send(:date)).to eq(false)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'adds an error when a field is not a date' do
|
55
|
+
validation = validator.new(:date, '200-11-1992', {:date=>true}, error)
|
56
|
+
validation.send(:date)
|
57
|
+
expect(error.errors).to eq({:date=> ["The date is not a valid date."]})
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '.ip' do
|
62
|
+
it 'checks if a field is a valid ip' do
|
63
|
+
validation = validator.new(:ip, '192.168.1.1', {:ip=>true}, error)
|
64
|
+
expect(validation.send(:ip)).to eq(true)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns false when a field is not a valid ip' do
|
68
|
+
validation = validator.new(:ip, '912.456.123.123', {:ip=>true}, error)
|
69
|
+
expect(validation.send(:ip)).to eq(false)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'adds an error when a field is not a valid ip' do
|
73
|
+
validation = validator.new(:ip, '912.456.123.123', {:ip=>true}, error)
|
74
|
+
validation.send(:ip)
|
75
|
+
expect(error.errors).to eq({:ip => ["The ip is not a valid ip."]})
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'assent'
|
2
|
+
|
3
|
+
class Valid < Assent::Validator
|
4
|
+
field :email, presence: true
|
5
|
+
field :testfield, presence: true
|
6
|
+
field :datetime, date: true
|
7
|
+
end
|
8
|
+
|
9
|
+
# Assent::Config.config_file 'lang.yml'
|
10
|
+
|
11
|
+
input = Hash.new
|
12
|
+
# input[:email] = "fr.levering@gmail.com"
|
13
|
+
# input[:name] = "test"
|
14
|
+
date = "20-11-1992"
|
15
|
+
Date.parse(date)
|
16
|
+
input[:datetime] = "20-11-1992"
|
17
|
+
valid = Valid.new
|
18
|
+
valid.validate(input)
|
19
|
+
puts valid.errors.errors
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frank
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A gem that handles your form and query validation in an elegant way.
|
56
|
+
email:
|
57
|
+
- fr.levering@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lang.yml
|
72
|
+
- lib/assent.rb
|
73
|
+
- lib/assent/config/config.rb
|
74
|
+
- lib/assent/config/lang.yml
|
75
|
+
- lib/assent/exceptions/config_exception.rb
|
76
|
+
- lib/assent/validations/errors.rb
|
77
|
+
- lib/assent/validations/field.rb
|
78
|
+
- lib/assent/validations/options.rb
|
79
|
+
- lib/assent/validations/validation_error.rb
|
80
|
+
- lib/assent/validations/validations_set.rb
|
81
|
+
- lib/assent/validations/validator.rb
|
82
|
+
- lib/assent/validator.rb
|
83
|
+
- lib/assent/version.rb
|
84
|
+
- spec/config_spec.rb
|
85
|
+
- spec/field_spec.rb
|
86
|
+
- spec/helpers/config.yml
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/validator_spec.rb
|
89
|
+
- spec/version_spec.rb
|
90
|
+
- test.rb
|
91
|
+
homepage: https://rubygems.org
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.5
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Assent is a simple validation library
|
116
|
+
test_files: []
|