validates_simple 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README +6 -0
- data/lib/validates_simple.rb +7 -0
- data/lib/validation/error.rb +14 -0
- data/lib/validation/rules.rb +107 -0
- data/lib/validation/validator.rb +29 -0
- data/spec/validation_spec.rb +122 -0
- data/validates_simple.gemspec +20 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a12fbab49198f92c3f5eab8f302e400d771e4616
|
4
|
+
data.tar.gz: 6b2e4c39e820eb88d2862229efa6998805f2b10c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e84515bf443900ac131c3e81114f9eb21817019e2e6841b01af92e5d09547ed730295de60ec8ec5f51abc8c98393cdc90bfb855288d3d8a5ffab1d534999042
|
7
|
+
data.tar.gz: 7cf54ba34a1ce76ba79598664449dfc77f2e47d76cd35c19a16e0aeb8318c1353b4fde144011a9bbd764f6003155e6afed0884db8f9702b4a9d79a3dafa61b10
|
data/README
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
module Validation
|
2
|
+
module Rules
|
3
|
+
def validates_presence_of(field, message='')
|
4
|
+
callback = lambda do |data|
|
5
|
+
if data.has_key? field
|
6
|
+
true
|
7
|
+
else
|
8
|
+
false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
12
|
+
end
|
13
|
+
|
14
|
+
def validates_confirmation_of(field, message='')
|
15
|
+
validates_presence_of(field, message)
|
16
|
+
|
17
|
+
callback = lambda do |data|
|
18
|
+
field_confirmation = data.fetch("#{field}_confirmation", nil)
|
19
|
+
if field_confirmation == data[field]
|
20
|
+
true
|
21
|
+
else
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
26
|
+
end
|
27
|
+
|
28
|
+
def validates_format_of(field, regex, message='')
|
29
|
+
validates_presence_of(field, message)
|
30
|
+
|
31
|
+
callback = lambda do |data|
|
32
|
+
if data[field].to_s =~ regex
|
33
|
+
true
|
34
|
+
else
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
39
|
+
end
|
40
|
+
|
41
|
+
def validates_numericality_of(field, message='')
|
42
|
+
validates_presence_of(field, message)
|
43
|
+
validates_format_of(field, /^\d+(\.\d+)?$/, message)
|
44
|
+
end
|
45
|
+
|
46
|
+
def validates_greather_then(field, number, message='')
|
47
|
+
validates_numericality_of(field, message)
|
48
|
+
callback = lambda do |data|
|
49
|
+
if data[field] > number
|
50
|
+
true
|
51
|
+
else
|
52
|
+
false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
56
|
+
end
|
57
|
+
|
58
|
+
def validates_greather_or_equal_then(field, number, message='')
|
59
|
+
validates_numericality_of(field, message)
|
60
|
+
callback = lambda do |data|
|
61
|
+
if data[field] >= number
|
62
|
+
true
|
63
|
+
else
|
64
|
+
false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
68
|
+
end
|
69
|
+
|
70
|
+
def validates_less_then(field, number, message='')
|
71
|
+
validates_numericality_of(field, message)
|
72
|
+
callback = lambda do |data|
|
73
|
+
if data[field] < number
|
74
|
+
true
|
75
|
+
else
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
80
|
+
end
|
81
|
+
|
82
|
+
def validates_less_or_equal_then(field, number, message='')
|
83
|
+
validates_numericality_of(field, message)
|
84
|
+
callback = lambda do |data|
|
85
|
+
if data[field] <= number
|
86
|
+
true
|
87
|
+
else
|
88
|
+
false
|
89
|
+
end
|
90
|
+
end
|
91
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
92
|
+
end
|
93
|
+
|
94
|
+
def validates_length_of_within(field, min, max, message='')
|
95
|
+
validates_presence_of(field, message)
|
96
|
+
callback = lambda do |data|
|
97
|
+
length = data[field].length
|
98
|
+
if length >= min && length <= max
|
99
|
+
true
|
100
|
+
else
|
101
|
+
false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Validation
|
2
|
+
class Validator
|
3
|
+
include Rules
|
4
|
+
|
5
|
+
attr_reader :errors
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@rules = []
|
9
|
+
@errors = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def validates(field, callback, message)
|
13
|
+
@rules << {:callback => callback, :message => Error.new(field, message)}
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate data
|
17
|
+
@errors = {}
|
18
|
+
@rules.each do |rule|
|
19
|
+
next if @errors[rule[:message].field]
|
20
|
+
@errors[rule[:message].field] = rule[:message] unless rule[:callback].call(data)
|
21
|
+
end
|
22
|
+
valid?
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid?
|
26
|
+
@errors.empty?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'validates_simple'
|
2
|
+
|
3
|
+
describe Validation do
|
4
|
+
let(:validator) { Validation::Validator.new }
|
5
|
+
|
6
|
+
describe "#validates" do
|
7
|
+
it "should support inline rule injection" do
|
8
|
+
validator.validates('fieldname', lambda {|data| data.has_key?('fieldname')}, ':field cant be blank!')
|
9
|
+
validator.validate({'other' => 42}).should_not be_true
|
10
|
+
validator.errors['fieldname'].message.should == 'fieldname cant be blank!'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#validates presence of" do
|
15
|
+
it "fails if field is not inside of the hash" do
|
16
|
+
validator.validates_presence_of('fieldname', ":field is required")
|
17
|
+
validator.validate({'some_other_field' => 'some value'}).should be_false
|
18
|
+
validator.should_not be_valid
|
19
|
+
end
|
20
|
+
|
21
|
+
it "succeeds if field is inside the hash" do
|
22
|
+
validator.validates_presence_of('fieldname', ':field is required')
|
23
|
+
validator.validate({'fieldname' => ''}).should be_true
|
24
|
+
validator.should be_valid
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns the correct error message" do
|
28
|
+
validator.validates_presence_of('fieldname', ":field is required")
|
29
|
+
validator.validate({'some_other_field' => 'some value'})
|
30
|
+
validator.errors['fieldname'].message.should == 'fieldname is required'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#validates_confirmation_of' do
|
35
|
+
it 'validates true if confirmation is present' do
|
36
|
+
validator.validates_confirmation_of('password', 'Password confirmation failed')
|
37
|
+
validator.validate({'password' => '123', 'password_confirmation' => '123'}).should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'validates false if confirmation is different or absent' do
|
41
|
+
validator.validates_confirmation_of('password', 'Password confirmation failed')
|
42
|
+
validator.validate({'somefield' => 'other'}).should be_false
|
43
|
+
validator.validate({'password' => '123', 'password_confirmation' => '1234'}).should be_false
|
44
|
+
validator.validate({'password' => '123'}).should be_false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#validates_format_of' do
|
49
|
+
it "ensures that the field is in the right format" do
|
50
|
+
validator.validates_format_of('email', /.+@.+\.(com|org|net)/, ':field is not in the right format')
|
51
|
+
validator.validate({'email' => 'someemail@example.com'}).should be_true
|
52
|
+
validator.validate({'email' => 'someemail'}).should be_false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#validates_numericality_of' do
|
57
|
+
it 'ensures that the field is a number' do
|
58
|
+
validator.validates_numericality_of('age', 'Age must be a number')
|
59
|
+
validator.validate({'age' => '2323'}).should be_true
|
60
|
+
validator.validate({'age' => '1223asdasd'}).should be_false
|
61
|
+
validator.validate({'something' => 'otherthing'}).should be_false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#validates_greather_then' do
|
66
|
+
it "checks if the field is greather then some number" do
|
67
|
+
validator.validates_greather_then('age', 18, 'Age must be greather then 18')
|
68
|
+
validator.validate({'age' => 19}).should be_true
|
69
|
+
validator.validate({'age' => 18}).should be_false
|
70
|
+
validator.validate({'age' => 17}).should be_false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#validates_greather_or_equal_then' do
|
75
|
+
it "checks if the field is greather or equal then some number" do
|
76
|
+
validator.validates_greather_or_equal_then('age', 18, 'Age must be greather or equal then 18')
|
77
|
+
validator.validate({'age' => 19}).should be_true
|
78
|
+
validator.validate({'age' => 18}).should be_true
|
79
|
+
validator.validate({'age' => 17}).should be_false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#validates_less_then' do
|
84
|
+
it "checks if the field is less then some number" do
|
85
|
+
validator.validates_less_then('age', 18, 'Age must be under 18')
|
86
|
+
validator.validate({'age' => 17}).should be_true
|
87
|
+
validator.validate({'age' => 18}).should be_false
|
88
|
+
validator.validate({'age' => 19}).should be_false
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#validates_less_or_equal_then' do
|
93
|
+
it "checks if the field is less or equal then some number" do
|
94
|
+
validator.validates_less_or_equal_then('age', 18, 'Age must be under 18')
|
95
|
+
validator.validate({'age' => 17}).should be_true
|
96
|
+
validator.validate({'age' => 18}).should be_true
|
97
|
+
validator.validate({'age' => 19}).should be_false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe '#validates_lenght_of_within' do
|
102
|
+
it 'checks if the length is within some bounderies' do
|
103
|
+
validator.validates_length_of_within('username', 3, 10, 'Length should be between 3 and 10 chars')
|
104
|
+
validator.validate({'username' => 'u' * 3}).should be_true
|
105
|
+
validator.validate({'username' => 'u' * 5}).should be_true
|
106
|
+
validator.validate({'username' => 'u' * 10}).should be_true
|
107
|
+
validator.validate({'username' => 'u'}).should be_false
|
108
|
+
validator.validate({'username' => 'u' * 11}).should be_false
|
109
|
+
validator.validate({'nousername' => true}).should be_false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe 'a simple sample of successfull validation' do
|
114
|
+
it "validates successfully" do
|
115
|
+
validator.validates_presence_of('username', 'Username is required')
|
116
|
+
validator.validates_presence_of('email', 'Email is required')
|
117
|
+
validator.validates_presence_of('password', 'Password is required')
|
118
|
+
validator.validates_confirmation_of('password', 'Password is not confirmed')
|
119
|
+
validator.validate({'username' => 'username', 'email' => 'email', 'password' => 123, 'password_confirmation' => 123}).should be_true
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'validates_simple'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'validates_simple'
|
6
|
+
s.version = Validation::VERSION
|
7
|
+
s.date = '2013-12-15'
|
8
|
+
s.summary = 'makes validation simple'
|
9
|
+
s.description = 'provides a simple way to validate hashes(params, json etc)'
|
10
|
+
s.authors = ['Filip Kostovski']
|
11
|
+
s.email = 'github.sirfilip@gmail.com'
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.homepage = 'https://github.com/sirfilip/validates_simple'
|
15
|
+
s.license = 'MIT'
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_development_dependency 'rspec'
|
19
|
+
end
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Filip Kostovski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: provides a simple way to validate hashes(params, json etc)
|
28
|
+
email: github.sirfilip@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- README
|
34
|
+
- lib/validates_simple.rb
|
35
|
+
- lib/validation/error.rb
|
36
|
+
- lib/validation/rules.rb
|
37
|
+
- lib/validation/validator.rb
|
38
|
+
- spec/validation_spec.rb
|
39
|
+
- validates_simple.gemspec
|
40
|
+
homepage: https://github.com/sirfilip/validates_simple
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.1.11
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: makes validation simple
|
64
|
+
test_files: []
|