validate 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 214d7269a6988fd34fe6097ea8ae7cf7fc8fc7ed
4
+ data.tar.gz: e804d91ebdfdd17544cf88baf7a6ad12d0f91495
5
+ SHA512:
6
+ metadata.gz: 65d8ec175b1271eb374f2c3d8134a071966f71dfb28f7de20fb4550db372924f59563f358633c154b50ef2943a4599e740aac656d34ebe972139b697ba377da2
7
+ data.tar.gz: bcdabb6bdc7b2dbae1d5d4a07cb87ae62265b56f0302b80c27eeb3a0232244bf944e9d1b2902a6f34cb4759a4b30356ae30459163788cd27e7a5675bd44b14c1
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ The Azure License
2
+
3
+ Copyright (c) 2013 Kenneth Ballenegger
4
+
5
+ Attribute to Kenneth Ballenegger - http://kswizz.com/
6
+
7
+ You (the licensee) are hereby granted permission, free of charge, to deal in
8
+ this software or source code (this "Software") without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish, distribute,
10
+ and/or sublicense this Software, subject to the following conditions:
11
+
12
+ You must give attribution to the party mentioned above, by name and by
13
+ hyperlink, in the about box, credits document and/or documentation of any
14
+ derivative work using a substantial portion of this Software.
15
+
16
+ You may not use the name of the copyright holder(s) to endorse or promote
17
+ products derived from this Software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THIS SOFTWARE OR THE USE OR OTHER DEALINGS IN THIS
25
+ SOFTWARE.
26
+
27
+ http://license.azuretalon.com/
@@ -32,17 +32,30 @@ module Validate
32
32
  # fetch reasons
33
33
  {reason: (v[:opts] || {})[:reason] || ValidationMethods.reason(v[:name])}.merge(v)
34
34
  end
35
- .map do |v|
35
+ .reduce({}) do |a,v|
36
+ # group validations by key
37
+ a[v[:fields]] ||= []
38
+ a[v[:fields]] << v
39
+ a
40
+ end
41
+ .map do |_, vs|
36
42
  # lastly, execute validation
37
- validator = if v[:validations]
38
- Validator.new(v[:validations])
39
- end
40
- success = ValidationMethods.new.send(v[:name], context_hash, v[:fields], v[:opts], validator)
41
- unless success
42
- reason = v[:reason].is_a?(Proc) ?
43
- ValidationMethods::ArgumentFailureBlockScope.new(context_hash, v[:fields], v[:opts], validator).instance_exec(&v[:reason]) :
44
- v[:reason]
45
- {v[:fields] => reason}
43
+ catch(:ValidateFailure) do
44
+ vs.each do |v|
45
+ validator = if v[:validations]
46
+ Validator.new(v[:validations])
47
+ end
48
+ success = ValidationMethods.new.send(v[:name], context_hash, v[:fields], v[:opts], validator)
49
+ unless success
50
+ reason = v[:reason].is_a?(Proc) ?
51
+ ValidationMethods::ArgumentFailureBlockScope.new(context_hash, v[:fields], v[:opts], validator).instance_exec(&v[:reason]) :
52
+ v[:reason]
53
+ # after one failure for a field, do not execute further
54
+ # validations
55
+ throw(:ValidateFailure, {v[:fields] => reason})
56
+ end
57
+ end
58
+ nil
46
59
  end
47
60
  end.select {|v| !v.nil? } # discard successes
48
61
 
@@ -1,3 +1,3 @@
1
1
  module Validate
2
- VERSION = '1.3.1'
2
+ VERSION = '1.3.2'
3
3
  end
@@ -451,7 +451,10 @@ describe Validate do
451
451
 
452
452
  validates_presence_of :secret
453
453
 
454
- validates_inclusion_of :type, in: %w(paid free)
454
+ validates_inclusion_of :type, in: %w(paid free),
455
+ reason: 'reason1'
456
+ validates :type, with: -> { $block_executed = true; self != 'free' },
457
+ reason: 'reason2'
455
458
 
456
459
  # validate a block in the data structure that must evaluate to true
457
460
  validates :block, with: -> { self.call == true }
@@ -543,6 +546,35 @@ describe Validate do
543
546
  test = TestClass.new(hash)
544
547
  test.validates?.should == false
545
548
  end
549
+
550
+ it 'executes multiple validations for a single field' do
551
+ $block_executed = false
552
+ hash = @valid_hash.dup
553
+ hash[:type] = 'paid'
554
+ test = TestClass.new(hash)
555
+ test.validates?.should == true
556
+ $block_executed.should == true
557
+ end
558
+
559
+ it 'does not execute subsequent validations for a field that already failed' do
560
+ $block_executed = false
561
+ hash = @valid_hash.dup
562
+ hash[:type] = 'bogus'
563
+ test = TestClass.new(hash)
564
+ test.validates?.should == false
565
+ test.failures.should == [{type: 'reason1'}]
566
+ $block_executed.should == false
567
+ end
568
+
569
+ it 'fails the first invalid block when there are multiple validations for one field' do
570
+ $block_executed = false
571
+ hash = @valid_hash.dup
572
+ hash[:type] = 'free'
573
+ test = TestClass.new(hash)
574
+ test.validates?.should == false
575
+ test.failures.should == [{type: 'reason2'}]
576
+ $block_executed.should == true
577
+ end
546
578
  end
547
579
 
548
580
 
metadata CHANGED
@@ -1,30 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
5
- prerelease:
4
+ version: 1.3.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Kenneth Ballenegger
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-27 00:00:00.000000000 Z
11
+ date: 2014-05-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  description: Validations is a library for validating data structures.
@@ -34,9 +31,9 @@ executables: []
34
31
  extensions: []
35
32
  extra_rdoc_files: []
36
33
  files:
37
- - .gitignore
34
+ - ".gitignore"
38
35
  - Gemfile
39
- - LICENSE.txt
36
+ - LICENSE
40
37
  - README.md
41
38
  - Rakefile
42
39
  - lib/validate.rb
@@ -51,27 +48,26 @@ files:
51
48
  - validate.gemspec
52
49
  homepage: ''
53
50
  licenses: []
51
+ metadata: {}
54
52
  post_install_message:
55
53
  rdoc_options: []
56
54
  require_paths:
57
55
  - lib
58
56
  required_ruby_version: !ruby/object:Gem::Requirement
59
- none: false
60
57
  requirements:
61
- - - ! '>='
58
+ - - ">="
62
59
  - !ruby/object:Gem::Version
63
60
  version: '0'
64
61
  required_rubygems_version: !ruby/object:Gem::Requirement
65
- none: false
66
62
  requirements:
67
- - - ! '>='
63
+ - - ">="
68
64
  - !ruby/object:Gem::Version
69
65
  version: '0'
70
66
  requirements: []
71
67
  rubyforge_project:
72
- rubygems_version: 1.8.24
68
+ rubygems_version: 2.2.2
73
69
  signing_key:
74
- specification_version: 3
70
+ specification_version: 4
75
71
  summary: Validations is a library for validating data structures.
76
72
  test_files:
77
73
  - spec/validate_spec.rb
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2013 Kenneth Ballenegger
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.