valid 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/validation.rb +14 -0
- data/lib/validation/rule/email.rb +45 -0
- data/lib/validation/rule/length.rb +29 -0
- data/lib/validation/rule/not_empty.rb +17 -0
- data/lib/validation/rule/numeric.rb +17 -0
- data/lib/validation/validator.rb +79 -0
- metadata +51 -0
data/lib/validation.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Validation
|
2
|
+
module Rule
|
3
|
+
class Email
|
4
|
+
|
5
|
+
# taken from: https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
|
6
|
+
EMAIL_ADDRESS = begin
|
7
|
+
letter = 'a-zA-Z'
|
8
|
+
digit = '0-9'
|
9
|
+
atext = "[#{letter}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
|
10
|
+
dot_atom_text = "#{atext}+([.]#{atext}*)+"
|
11
|
+
dot_atom = dot_atom_text
|
12
|
+
no_ws_ctl = '\x01-\x08\x11\x12\x14-\x1f\x7f'
|
13
|
+
qtext = "[^#{no_ws_ctl}\\x0d\\x22\\x5c]" # Non-whitespace, non-control character except for \ and "
|
14
|
+
text = '[\x01-\x09\x11\x12\x14-\x7f]'
|
15
|
+
quoted_pair = "(\\x5c#{text})"
|
16
|
+
qcontent = "(?:#{qtext}|#{quoted_pair})"
|
17
|
+
quoted_string = "[\"]#{qcontent}+[\"]"
|
18
|
+
atom = "#{atext}+"
|
19
|
+
word = "(?:#{atom}|#{quoted_string})"
|
20
|
+
obs_local_part = "#{word}([.]#{word})*"
|
21
|
+
local_part = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
|
22
|
+
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
23
|
+
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
24
|
+
domain_literal = "\\[#{dcontent}+\\]"
|
25
|
+
obs_domain = "#{atom}([.]#{atom})+"
|
26
|
+
domain = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
|
27
|
+
addr_spec = "#{local_part}\@#{domain}"
|
28
|
+
pattern = /\A#{addr_spec}\z/u
|
29
|
+
end
|
30
|
+
|
31
|
+
def valid_value?(value)
|
32
|
+
!!EMAIL_ADDRESS.match(value)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def error_key
|
37
|
+
:email
|
38
|
+
end
|
39
|
+
|
40
|
+
def params
|
41
|
+
{}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Validation
|
2
|
+
module Rule
|
3
|
+
class Length
|
4
|
+
def initialize(params)
|
5
|
+
@params = params
|
6
|
+
end
|
7
|
+
|
8
|
+
def params
|
9
|
+
@params
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid_value?(value)
|
13
|
+
valid = true
|
14
|
+
|
15
|
+
@params.each_pair do |key, param|
|
16
|
+
valid = false if key == :minimum && value.length < param
|
17
|
+
valid = false if key == :maximum && value.length > param
|
18
|
+
valid = false if key == :exact && value.length != param
|
19
|
+
end
|
20
|
+
|
21
|
+
valid
|
22
|
+
end
|
23
|
+
|
24
|
+
def error_key
|
25
|
+
:length
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Validation
|
2
|
+
module Rules
|
3
|
+
def rules
|
4
|
+
@rules ||= {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def errors
|
8
|
+
@errors ||= {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def rule(field, rule)
|
12
|
+
field = field.to_sym
|
13
|
+
if rules[field].nil?
|
14
|
+
rules[field] = []
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
if rule.respond_to?(:each_pair)
|
19
|
+
rule.each_pair do |key, value|
|
20
|
+
r = Validation::Rule.const_get(camelize(key))
|
21
|
+
rules[field] << r.new(value)
|
22
|
+
end
|
23
|
+
elsif rule.respond_to?(:each)
|
24
|
+
rule.each do |r|
|
25
|
+
r = Validation::Rule.const_get(camelize(r))
|
26
|
+
rules[field] << r.new
|
27
|
+
end
|
28
|
+
else
|
29
|
+
rule = Validation::Rule.const_get(camelize(rule))
|
30
|
+
rules[field] << rule.new
|
31
|
+
end
|
32
|
+
rescue NameError => e
|
33
|
+
raise InvalidRule
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid?
|
38
|
+
valid = true
|
39
|
+
|
40
|
+
rules.each_pair do |field, rules|
|
41
|
+
if ! @obj.respond_to?(field)
|
42
|
+
raise InvalidKey
|
43
|
+
end
|
44
|
+
|
45
|
+
rules.each do |r|
|
46
|
+
if ! r.valid_value?(@obj.send(field))
|
47
|
+
valid = false
|
48
|
+
errors[field] = {:rule => r.error_key, :params => r.params}
|
49
|
+
break
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
@valid = valid
|
55
|
+
end
|
56
|
+
|
57
|
+
protected
|
58
|
+
|
59
|
+
def camelize(term)
|
60
|
+
string = term.to_s
|
61
|
+
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
|
62
|
+
string.gsub(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }.gsub('/', '::')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Validator
|
67
|
+
include Validation::Rules
|
68
|
+
|
69
|
+
def initialize(obj)
|
70
|
+
@obj = obj
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class InvalidKey < RuntimeError
|
75
|
+
end
|
76
|
+
|
77
|
+
class InvalidRule < RuntimeError
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Bush
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
- contractfrombelow@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/validation.rb
|
22
|
+
- lib/validation/rule/email.rb
|
23
|
+
- lib/validation/rule/not_empty.rb
|
24
|
+
- lib/validation/rule/numeric.rb
|
25
|
+
- lib/validation/rule/length.rb
|
26
|
+
- lib/validation/validator.rb
|
27
|
+
homepage: https://github.com/zombor/validation
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.17
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: A standalone, generic object validator for ruby
|
51
|
+
test_files: []
|