veto 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +561 -0
  6. data/Rakefile +1 -0
  7. data/lib/veto/attribute_validator_factory.rb +32 -0
  8. data/lib/veto/builder.rb +65 -0
  9. data/lib/veto/conditions.rb +23 -0
  10. data/lib/veto/conditions_evaluator.rb +30 -0
  11. data/lib/veto/configuration.rb +42 -0
  12. data/lib/veto/errors.rb +35 -0
  13. data/lib/veto/exceptions.rb +5 -0
  14. data/lib/veto/model.rb +37 -0
  15. data/lib/veto/validator.rb +150 -0
  16. data/lib/veto/validators/abstract_validator.rb +15 -0
  17. data/lib/veto/validators/attribute_validator.rb +22 -0
  18. data/lib/veto/validators/custom_method_validator.rb +19 -0
  19. data/lib/veto/validators/exact_length_validator.rb +15 -0
  20. data/lib/veto/validators/format_validator.rb +15 -0
  21. data/lib/veto/validators/inclusion_validator.rb +16 -0
  22. data/lib/veto/validators/integer_validator.rb +17 -0
  23. data/lib/veto/validators/length_range_validator.rb +16 -0
  24. data/lib/veto/validators/max_length_validator.rb +15 -0
  25. data/lib/veto/validators/min_length_validator.rb +15 -0
  26. data/lib/veto/validators/not_null_validator.rb +14 -0
  27. data/lib/veto/validators/numeric_validator.rb +17 -0
  28. data/lib/veto/validators/presence_validator.rb +15 -0
  29. data/lib/veto/version.rb +3 -0
  30. data/lib/veto.rb +53 -0
  31. data/spec/attribute_validator_factory_spec.rb +72 -0
  32. data/spec/builder_spec.rb +38 -0
  33. data/spec/conditions_evaluator_spec.rb +90 -0
  34. data/spec/conditions_spec.rb +16 -0
  35. data/spec/configuration/message_spec.rb +30 -0
  36. data/spec/configuration_spec.rb +6 -0
  37. data/spec/errors_spec.rb +22 -0
  38. data/spec/model_spec.rb +67 -0
  39. data/spec/spec_helper.rb +6 -0
  40. data/spec/system/validator_spec.rb +380 -0
  41. data/spec/validator_spec.rb +119 -0
  42. data/spec/validators/exact_length_validator_spec.rb +37 -0
  43. data/spec/validators/format_validator_spec.rb +32 -0
  44. data/spec/validators/inclusion_validator_spec.rb +45 -0
  45. data/spec/validators/integer_validator_spec.rb +42 -0
  46. data/spec/validators/length_range_validator_spec.rb +55 -0
  47. data/spec/validators/max_length_validator_spec.rb +32 -0
  48. data/spec/validators/min_length_validator_spec.rb +32 -0
  49. data/spec/validators/not_null_validator_spec.rb +27 -0
  50. data/spec/validators/numeric_validator_spec.rb +42 -0
  51. data/spec/validators/presence_validator_spec.rb +47 -0
  52. data/spec/veto_spec.rb +24 -0
  53. data/veto.gemspec +24 -0
  54. metadata +161 -0
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'veto/validators/max_length_validator'
3
+ require 'veto/errors'
4
+
5
+ describe Veto::MaxLengthValidator do
6
+ let(:errors){ Veto::Errors.new }
7
+ let(:entity) { Object.new }
8
+ let(:attribute) { :title }
9
+ let(:value) { 'blahblah' }
10
+ let(:options) {{:with => 10}}
11
+ let(:validator){ Veto::MaxLengthValidator.new(stub, stub) }
12
+ let(:result){ validator.validate(entity, attribute, value, errors, options) }
13
+
14
+ describe '#validate' do
15
+ before { result }
16
+
17
+ context 'when value length is less than max' do
18
+ let(:value) { 'abc' }
19
+ it { errors[:title].must_be_nil }
20
+ end
21
+
22
+ context 'when value is too long' do
23
+ let(:value) { 'abcdefghijklmnop' }
24
+ it { errors[:title].must_equal ["is longer than 10 characters"] }
25
+ end
26
+
27
+ context 'when value is nil' do
28
+ let(:value) { nil }
29
+ it { errors[:title].must_equal ["is longer than 10 characters"] }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'veto/validators/min_length_validator'
3
+ require 'veto/errors'
4
+
5
+ describe Veto::MinLengthValidator do
6
+ let(:errors){ Veto::Errors.new }
7
+ let(:entity) { Object.new }
8
+ let(:attribute) { :title }
9
+ let(:value) { 'blahblah' }
10
+ let(:options) {{:with => 5}}
11
+ let(:validator){ Veto::MinLengthValidator.new(stub, stub) }
12
+ let(:result){ validator.validate(entity, attribute, value, errors, options) }
13
+
14
+ describe '#validate' do
15
+ before { result }
16
+
17
+ context 'when value length is greater than min' do
18
+ let(:value) { 'abcdefg' }
19
+ it { errors[:title].must_be_nil }
20
+ end
21
+
22
+ context 'when value is too short' do
23
+ let(:value) { 'abcd' }
24
+ it { errors[:title].must_equal ["is shorter than 5 characters"] }
25
+ end
26
+
27
+ context 'when value is nil' do
28
+ let(:value) { nil }
29
+ it { errors[:title].must_equal ["is shorter than 5 characters"] }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'veto/validators/not_null_validator'
3
+ require 'veto/errors'
4
+
5
+ describe Veto::NotNullValidator do
6
+ let(:errors){ Veto::Errors.new }
7
+ let(:entity) { Object.new }
8
+ let(:attribute) { :title }
9
+ let(:value) { 'blahblah' }
10
+ let(:options) {{:with => {}}}
11
+ let(:validator){ Veto::NotNullValidator.new(stub, stub) }
12
+ let(:result){ validator.validate(entity, attribute, value, errors, options) }
13
+
14
+ describe '#validate' do
15
+ before { result }
16
+
17
+ context 'when value is not null' do
18
+ let(:value) { 123 }
19
+ it { errors[:title].must_be_nil }
20
+ end
21
+
22
+ context 'when value is nil' do
23
+ let(:value) { nil }
24
+ it { errors[:title].must_equal ["is not present"]}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'veto/validators/numeric_validator'
3
+ require 'veto/errors'
4
+
5
+ describe Veto::NumericValidator do
6
+ let(:errors){ Veto::Errors.new }
7
+ let(:entity) { Object.new }
8
+ let(:attribute) { :title }
9
+ let(:value) { 'blahblah' }
10
+ let(:options) {{:with => {}}}
11
+ let(:validator){ Veto::NumericValidator.new(stub, stub) }
12
+ let(:result){ validator.validate(entity, attribute, value, errors, options) }
13
+
14
+ describe '#validate' do
15
+ before { result }
16
+
17
+ context 'when value is integer' do
18
+ let(:value) { 123 }
19
+ it { errors[:title].must_be_nil }
20
+ end
21
+
22
+ context 'when value is float' do
23
+ let(:value) { 123.4 }
24
+ it { errors[:title].must_be_nil }
25
+ end
26
+
27
+ context 'when value is string' do
28
+ let(:value) { 'abc' }
29
+ it { errors[:title].must_equal ["is not a number"]}
30
+ end
31
+
32
+ context 'when value is nil' do
33
+ let(:value) { nil }
34
+ it { errors[:title].must_equal ["is not a number"]}
35
+ end
36
+
37
+ context 'when value is everything else' do
38
+ let(:value) { ['array'] }
39
+ it { errors[:title].must_equal ["is not a number"]}
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'veto/validators/presence_validator'
3
+ require 'veto/errors'
4
+
5
+ describe Veto::PresenceValidator do
6
+ let(:errors) { Veto::Errors.new }
7
+ let(:entity) { Object.new }
8
+ let(:attribute) { :title }
9
+ let(:value) { 'blahblah' }
10
+ let(:options) {{:with => {}}}
11
+ let(:validator) { Veto::PresenceValidator.new(stub, stub) }
12
+ let(:result) { validator.validate(entity, attribute, value, errors, options) }
13
+
14
+ describe '#validate' do
15
+ before { result }
16
+
17
+ context 'when value is not null' do
18
+ let(:value) { 123 }
19
+ it { errors[:title].must_be_nil }
20
+ end
21
+
22
+ context 'when value is nil' do
23
+ let(:value) { nil }
24
+ it { errors[:title].must_equal ["is not present"]}
25
+ end
26
+
27
+ context 'when value is empty string' do
28
+ let(:value) { '' }
29
+ it { errors[:title].must_equal ["is not present"]}
30
+ end
31
+
32
+ context 'when value is string of whitespace' do
33
+ let(:value) { ' ' }
34
+ it { errors[:title].must_equal ["is not present"]}
35
+ end
36
+
37
+ context 'when value is empty array' do
38
+ let(:value) { [] }
39
+ it { errors[:title].must_equal ["is not present"]}
40
+ end
41
+
42
+ context 'when value is empty hash' do
43
+ let(:value) { {} }
44
+ it { errors[:title].must_equal ["is not present"]}
45
+ end
46
+ end
47
+ end
data/spec/veto_spec.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'veto'
3
+
4
+ describe Veto do
5
+ describe '::model' do
6
+ let(:klass){ Class.new{ include Veto.model('validator class') } }
7
+
8
+ it 'includes model module to class' do
9
+ klass.included_modules.must_include Veto::Model
10
+ end
11
+
12
+ it 'sets model validator' do
13
+ klass.validator.must_equal 'validator class'
14
+ end
15
+ end
16
+
17
+ describe '::validator' do
18
+ let(:klass){ Class.new{ include Veto.validator } }
19
+
20
+ it 'includes validator module to class' do
21
+ klass.included_modules.must_include Veto::Validator
22
+ end
23
+ end
24
+ end
data/veto.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'veto/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "veto"
8
+ spec.version = Veto::VERSION
9
+ spec.authors = ["Erik Lott"]
10
+ spec.email = ["erik.lott@kodio.com"]
11
+ spec.description = %q{Lightweight validations for plain ruby objects}
12
+ spec.summary = %q{Validations for plain old ruby objects}
13
+ spec.homepage = "http://github.com/kodio/veto"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "mocha", "~> 0.14.0"
24
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: veto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Erik Lott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-06 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.14.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.14.0
55
+ description: Lightweight validations for plain ruby objects
56
+ email:
57
+ - erik.lott@kodio.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/veto.rb
68
+ - lib/veto/attribute_validator_factory.rb
69
+ - lib/veto/builder.rb
70
+ - lib/veto/conditions.rb
71
+ - lib/veto/conditions_evaluator.rb
72
+ - lib/veto/configuration.rb
73
+ - lib/veto/errors.rb
74
+ - lib/veto/exceptions.rb
75
+ - lib/veto/model.rb
76
+ - lib/veto/validator.rb
77
+ - lib/veto/validators/abstract_validator.rb
78
+ - lib/veto/validators/attribute_validator.rb
79
+ - lib/veto/validators/custom_method_validator.rb
80
+ - lib/veto/validators/exact_length_validator.rb
81
+ - lib/veto/validators/format_validator.rb
82
+ - lib/veto/validators/inclusion_validator.rb
83
+ - lib/veto/validators/integer_validator.rb
84
+ - lib/veto/validators/length_range_validator.rb
85
+ - lib/veto/validators/max_length_validator.rb
86
+ - lib/veto/validators/min_length_validator.rb
87
+ - lib/veto/validators/not_null_validator.rb
88
+ - lib/veto/validators/numeric_validator.rb
89
+ - lib/veto/validators/presence_validator.rb
90
+ - lib/veto/version.rb
91
+ - spec/attribute_validator_factory_spec.rb
92
+ - spec/builder_spec.rb
93
+ - spec/conditions_evaluator_spec.rb
94
+ - spec/conditions_spec.rb
95
+ - spec/configuration/message_spec.rb
96
+ - spec/configuration_spec.rb
97
+ - spec/errors_spec.rb
98
+ - spec/model_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/system/validator_spec.rb
101
+ - spec/validator_spec.rb
102
+ - spec/validators/exact_length_validator_spec.rb
103
+ - spec/validators/format_validator_spec.rb
104
+ - spec/validators/inclusion_validator_spec.rb
105
+ - spec/validators/integer_validator_spec.rb
106
+ - spec/validators/length_range_validator_spec.rb
107
+ - spec/validators/max_length_validator_spec.rb
108
+ - spec/validators/min_length_validator_spec.rb
109
+ - spec/validators/not_null_validator_spec.rb
110
+ - spec/validators/numeric_validator_spec.rb
111
+ - spec/validators/presence_validator_spec.rb
112
+ - spec/veto_spec.rb
113
+ - veto.gemspec
114
+ homepage: http://github.com/kodio/veto
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.0.7
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Validations for plain old ruby objects
138
+ test_files:
139
+ - spec/attribute_validator_factory_spec.rb
140
+ - spec/builder_spec.rb
141
+ - spec/conditions_evaluator_spec.rb
142
+ - spec/conditions_spec.rb
143
+ - spec/configuration/message_spec.rb
144
+ - spec/configuration_spec.rb
145
+ - spec/errors_spec.rb
146
+ - spec/model_spec.rb
147
+ - spec/spec_helper.rb
148
+ - spec/system/validator_spec.rb
149
+ - spec/validator_spec.rb
150
+ - spec/validators/exact_length_validator_spec.rb
151
+ - spec/validators/format_validator_spec.rb
152
+ - spec/validators/inclusion_validator_spec.rb
153
+ - spec/validators/integer_validator_spec.rb
154
+ - spec/validators/length_range_validator_spec.rb
155
+ - spec/validators/max_length_validator_spec.rb
156
+ - spec/validators/min_length_validator_spec.rb
157
+ - spec/validators/not_null_validator_spec.rb
158
+ - spec/validators/numeric_validator_spec.rb
159
+ - spec/validators/presence_validator_spec.rb
160
+ - spec/veto_spec.rb
161
+ has_rdoc: