validate 1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/validate/hash.rb +21 -0
- data/lib/validate/parser.rb +7 -3
- data/lib/validate/validations.rb +6 -0
- data/lib/validate/version.rb +1 -1
- data/lib/validate.rb +1 -1
- data/spec/validate_spec.rb +31 -0
- metadata +3 -2
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
require 'validate'
|
3
|
+
|
4
|
+
class Hash
|
5
|
+
|
6
|
+
# Validates the hash using either a pre-compiled validation, or by parsing
|
7
|
+
# the block passed.
|
8
|
+
#
|
9
|
+
def validates?(validations=nil, &block)
|
10
|
+
validations = ::Validate::Parser.parse(&block) unless validations
|
11
|
+
@validator = ::Validate::Validator.new(validations)
|
12
|
+
@validator.validates?(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns any failures from the last run validations.
|
16
|
+
#
|
17
|
+
def failures
|
18
|
+
return [] unless @validator
|
19
|
+
@validator.failures
|
20
|
+
end
|
21
|
+
end
|
data/lib/validate/parser.rb
CHANGED
@@ -15,7 +15,7 @@ module Validate
|
|
15
15
|
# validates_type_of :tier, is: Numeric
|
16
16
|
# end
|
17
17
|
#
|
18
|
-
class
|
18
|
+
class Parser
|
19
19
|
|
20
20
|
def self.parse(&block)
|
21
21
|
# execute block, return array of validation methods called
|
@@ -23,6 +23,10 @@ module Validate
|
|
23
23
|
context.instance_exec(&block)
|
24
24
|
context.validations
|
25
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
class BlockParsingContext
|
26
30
|
|
27
31
|
def initialize
|
28
32
|
@validations = []
|
@@ -35,7 +39,7 @@ module Validate
|
|
35
39
|
raise NoMethodError.new("Undefined validation method: #{method}...") unless ValidationMethods.new.respond_to?(method)
|
36
40
|
opts = args.pop if args.last.is_a?(::Hash)
|
37
41
|
children = if block
|
38
|
-
|
42
|
+
Parser.parse(&block)
|
39
43
|
end
|
40
44
|
@validations << {
|
41
45
|
name: method,
|
@@ -52,7 +56,7 @@ module Validate
|
|
52
56
|
# end
|
53
57
|
#
|
54
58
|
def run_when(condition, &block)
|
55
|
-
validations =
|
59
|
+
validations = Parser.parse(&block)
|
56
60
|
validations.map do |v|
|
57
61
|
v[:opts] ||= {}
|
58
62
|
v[:opts][:when] = condition
|
data/lib/validate/validations.rb
CHANGED
@@ -46,6 +46,7 @@ module Validate
|
|
46
46
|
#
|
47
47
|
# validates_inclusion_of :type, in: %w(paid free)
|
48
48
|
#
|
49
|
+
fails_because_key { "was not of in #{opts[:in].inspect}." }
|
49
50
|
def validates_inclusion_of(obj, field, opts, validator)
|
50
51
|
opts[:in].include?(obj[field])
|
51
52
|
end
|
@@ -54,6 +55,7 @@ module Validate
|
|
54
55
|
#
|
55
56
|
# validates_numericality_of :amount
|
56
57
|
#
|
58
|
+
fails_because_key 'was not Numeric.'
|
57
59
|
def validates_numericality_of(obj, field, opts, validator)
|
58
60
|
obj[field].is_a?(Numeric)
|
59
61
|
end
|
@@ -62,6 +64,7 @@ module Validate
|
|
62
64
|
#
|
63
65
|
# validates_value_of :field, is: 'something'
|
64
66
|
#
|
67
|
+
fails_because_key { "was not of in #{opts[:is].inspect}." }
|
65
68
|
def validates_value_of(obj, field, opts, validator)
|
66
69
|
obj.include?(field) && obj[field] == opts[:is]
|
67
70
|
end
|
@@ -73,6 +76,7 @@ module Validate
|
|
73
76
|
# validates_numericality_of :amount
|
74
77
|
# end
|
75
78
|
#
|
79
|
+
fails_because_key { ['failed validation with errors:', validator.failures] }
|
76
80
|
def validates_child_hash(obj, field, opts, validator)
|
77
81
|
return false unless obj[field].respond_to?(:to_hash)
|
78
82
|
hash = obj[field].to_hash
|
@@ -89,6 +93,7 @@ module Validate
|
|
89
93
|
# validates_type_of :self, is: String
|
90
94
|
# end
|
91
95
|
#
|
96
|
+
fails_because_key 'contained at least one element which failed validation.'
|
92
97
|
def validates_array(obj, field, opts, validator)
|
93
98
|
return false unless obj[field].respond_to?(:to_a)
|
94
99
|
array = obj[field].to_a
|
@@ -101,6 +106,7 @@ module Validate
|
|
101
106
|
#
|
102
107
|
# validates_regex :field, matches: /^hello/
|
103
108
|
#
|
109
|
+
fails_because_key { "did not match regex #{opts[:matches].inspect}." }
|
104
110
|
def validates_regex(obj, field, opts, validator)
|
105
111
|
return false unless obj[field].respond_to?(:=~)
|
106
112
|
0 == (obj[field] =~ opts[:matches])
|
data/lib/validate/version.rb
CHANGED
data/lib/validate.rb
CHANGED
data/spec/validate_spec.rb
CHANGED
@@ -513,6 +513,37 @@ describe Validate do
|
|
513
513
|
test.validates?.should == false
|
514
514
|
end
|
515
515
|
end
|
516
|
+
|
517
|
+
|
518
|
+
context Hash do
|
519
|
+
|
520
|
+
before do
|
521
|
+
require 'validate/hash'
|
522
|
+
@compiled_validation = Validate::Parser.parse do
|
523
|
+
validates_presence_of 'name', when: -> { true }
|
524
|
+
end
|
525
|
+
end
|
526
|
+
|
527
|
+
it 'should validate a valid object' do
|
528
|
+
hash = {"name" => :hello}
|
529
|
+
hash.validates?(@compiled_validation).should == true
|
530
|
+
hash.failures.should == []
|
531
|
+
end
|
532
|
+
|
533
|
+
it 'should fail to validate an invalid object' do
|
534
|
+
hash = {"not_name" => :hello}
|
535
|
+
hash.validates?(@compiled_validation).should == false
|
536
|
+
hash.failures.should == [{"name"=>"was not present."}]
|
537
|
+
end
|
538
|
+
|
539
|
+
it 'should validate a valid object, when compiling on the fly' do
|
540
|
+
hash = {"name" => :hello}
|
541
|
+
hash.validates? do
|
542
|
+
validates_presence_of 'name', when: -> { true }
|
543
|
+
end.should == true
|
544
|
+
hash.failures.should == []
|
545
|
+
end
|
546
|
+
end
|
516
547
|
end
|
517
548
|
|
518
549
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- README.md
|
41
41
|
- Rakefile
|
42
42
|
- lib/validate.rb
|
43
|
+
- lib/validate/hash.rb
|
43
44
|
- lib/validate/kongo.rb
|
44
45
|
- lib/validate/parser.rb
|
45
46
|
- lib/validate/validations.rb
|