standalone_validator 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +12 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/lib/standalone_validator.rb +75 -0
- data/lib/standalone_validator/definitions.rb +58 -0
- data/lib/standalone_validator/named_validations.rb +17 -0
- data/lib/standalone_validator/named_validations/common_rails_options.rb +61 -0
- data/lib/standalone_validator/named_validations/validates_presence_of.rb +20 -0
- data/lib/standalone_validator/validation_result.rb +54 -0
- data/lib/standalone_validator/validation_result_builder.rb +57 -0
- data/lib/standalone_validator/version.rb +3 -0
- data/lib/standalone_validator/violation.rb +17 -0
- data/spec/integration_spec.rb +69 -0
- data/spec/named_validations/validates_presence_of_spec.rb +47 -0
- data/spec/spec_helper.rb +17 -0
- data/standalone_validator.gemspec +32 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bca879be6bf42bd2a02eae2221a168206782348c
|
4
|
+
data.tar.gz: a82ee314d96bf3d10b3dbc16538de68d6311fbbe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1eb6e22ab8bd654da4d87a0917b52d1ffa25a4ae81bf9e6ab30dc9c07460bd8a7de3022f4dc7a74ecee7c3259b69e75284009fe27c3cdfec07e45a88f57ba9cf
|
7
|
+
data.tar.gz: 97822fabfa6deb523853f135df9ce830b2b05b3f785a524e6f0f3fc02a1dce48df91353037d38711d5e94bf2519b321fe8b0991cdcb7015b182ec6a73e75c6c6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Renato Zannon
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# StandaloneValidator
|
2
|
+
[](http://travis-ci.org/riccieri/standalone_validator)
|
3
|
+
|
4
|
+
A library for creating PORO validators that are composable and can be used with ActiveRecord or standalone.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'standalone_validator'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install standalone_validator
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'standalone_validator'
|
24
|
+
|
25
|
+
class NameLengthValidator < StandaloneValidator
|
26
|
+
def initialize(min_length, attribute_name = :name)
|
27
|
+
@min_length = min_length
|
28
|
+
@attribute_name = attribute_name
|
29
|
+
end
|
30
|
+
|
31
|
+
include_validation do |object, result|
|
32
|
+
if object.send(attribute_name).length < @min_length
|
33
|
+
result.add_violation(attribute_name, 'should be bigger')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
attr_reader :attribute_name
|
40
|
+
end
|
41
|
+
|
42
|
+
class AllNamesValidator < StandaloneValidator
|
43
|
+
include_validation NameLengthValidator, 5
|
44
|
+
include_validation NameLengthValidator, 4, :last_name
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
Person = Struct.new(:name, :last_name)
|
49
|
+
validator = AllNamesValidator.new
|
50
|
+
|
51
|
+
person = Person.new("Renato", "Zannon")
|
52
|
+
puts validator.violations_of(person).any? # false
|
53
|
+
|
54
|
+
other_person = Person.new("Renato", "Foo")
|
55
|
+
puts validator.violations_of(other_person).any? # true
|
56
|
+
|
57
|
+
validator.violations_of(other_person).each do |violation|
|
58
|
+
puts violation.attribute.inspect # :last_name
|
59
|
+
puts violation.message.inspect # "should be bigger"
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'backports'
|
2
|
+
require 'hamster/set'
|
3
|
+
|
4
|
+
require 'standalone_validator/version'
|
5
|
+
|
6
|
+
require 'standalone_validator/definitions'
|
7
|
+
require 'standalone_validator/validation_result_builder'
|
8
|
+
|
9
|
+
class StandaloneValidator
|
10
|
+
class << self
|
11
|
+
Definitions.on_validation_registered do |name, validation|
|
12
|
+
define_method(name) do |*args, &block|
|
13
|
+
include_validation(validation, *args, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def create(&block)
|
18
|
+
Class.new(StandaloneValidator, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def register_as(name)
|
22
|
+
NamedValidations.create(name, self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def validations
|
26
|
+
return @validations if defined?(@validations)
|
27
|
+
|
28
|
+
if superclass.kind_of?(StandaloneValidator)
|
29
|
+
@validations = superclass.validations
|
30
|
+
else
|
31
|
+
@validations = Hamster.set
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def include_validation(*args, &block)
|
38
|
+
validation = Definitions.coerce_to_validation(*args, &block)
|
39
|
+
@validations = validations.add(validation)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
require 'standalone_validator/named_validations'
|
44
|
+
|
45
|
+
def violations_of(object)
|
46
|
+
builder = ValidationResultBuilder.new
|
47
|
+
builder.validated_object = object
|
48
|
+
|
49
|
+
validations.each do |validation|
|
50
|
+
if validation.respond_to?(:to_proc)
|
51
|
+
result = instance_exec(object, &validation)
|
52
|
+
else
|
53
|
+
result = validation.call(object)
|
54
|
+
end
|
55
|
+
|
56
|
+
builder.merge_result(result)
|
57
|
+
end
|
58
|
+
|
59
|
+
builder.result
|
60
|
+
end
|
61
|
+
|
62
|
+
alias_method :call, :violations_of
|
63
|
+
|
64
|
+
def add_errors_to(object)
|
65
|
+
validation_result = violations_of(object)
|
66
|
+
validation_result.add_errors_to(object.errors)
|
67
|
+
validation_result
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def validations
|
73
|
+
self.class.validations
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'hamster/hash'
|
2
|
+
require 'hamster/vector'
|
3
|
+
|
4
|
+
class StandaloneValidator
|
5
|
+
class ValidationNotFoundException < Exception
|
6
|
+
def initialize(validation_name)
|
7
|
+
super("No such validation: #{validation_name.inspect}")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Definitions
|
12
|
+
module_function
|
13
|
+
|
14
|
+
def register_validation_factory(name, validation_factory)
|
15
|
+
@registry = registry.put(name.to_sym, validation_factory)
|
16
|
+
listeners.each do |listener|
|
17
|
+
listener.call(name, validation_factory)
|
18
|
+
end
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def lookup_validation_factory(name)
|
23
|
+
registry[name.to_sym]
|
24
|
+
end
|
25
|
+
|
26
|
+
def on_validation_registered(callable = nil, &block)
|
27
|
+
if block_given?
|
28
|
+
@listeners = listeners.add(block)
|
29
|
+
else
|
30
|
+
@listeners = listeners.add(block)
|
31
|
+
end
|
32
|
+
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def coerce_to_validation(validation_or_validation_class = nil, *args, &block)
|
37
|
+
if validation_or_validation_class.respond_to?(:new)
|
38
|
+
validation_or_validation_class.new(*args, &block)
|
39
|
+
elsif validation_or_validation_class.nil? && block_given?
|
40
|
+
lambda { |object|
|
41
|
+
ValidationResult.build_for(object) do |result|
|
42
|
+
instance_exec(object, result, &block)
|
43
|
+
end
|
44
|
+
}
|
45
|
+
else
|
46
|
+
validation_or_validation_class
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def registry
|
51
|
+
@registry ||= Hamster.hash
|
52
|
+
end
|
53
|
+
|
54
|
+
def listeners
|
55
|
+
@listeners ||= Hamster.vector
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'standalone_validator'
|
2
|
+
require 'standalone_validator/definitions'
|
3
|
+
|
4
|
+
class StandaloneValidator
|
5
|
+
module NamedValidations
|
6
|
+
def self.create(name, klass)
|
7
|
+
Definitions.register_validation_factory(name, klass)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'pathname'
|
13
|
+
validations = Pathname(__FILE__).parent + "named_validations" + "*.rb"
|
14
|
+
|
15
|
+
Dir[validations].each do |validation_file|
|
16
|
+
require validation_file
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class StandaloneValidator
|
2
|
+
module NamedValidations
|
3
|
+
module CommonRailsOptions
|
4
|
+
def self.extract_options!(array)
|
5
|
+
if array.last.is_a?(Hash)
|
6
|
+
array.pop
|
7
|
+
else
|
8
|
+
{}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
ALWAYS_TRUE = Proc.new { true }
|
13
|
+
|
14
|
+
def self.condition_for(options)
|
15
|
+
if options.has_key?(:if)
|
16
|
+
options[:if].to_proc
|
17
|
+
elsif options.has_key?(:unless)
|
18
|
+
reverse_condition = options[:unless].to_proc
|
19
|
+
Proc.new { |*args| not reverse_condition.call(*args) }
|
20
|
+
else
|
21
|
+
ALWAYS_TRUE
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.extend(ClassMethods)
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def include_validation(&block)
|
31
|
+
super do |object, result|
|
32
|
+
condition = CommonRailsOptions.condition_for(options)
|
33
|
+
|
34
|
+
if condition.call(object)
|
35
|
+
instance_exec(object, result, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(*names)
|
42
|
+
options = CommonRailsOptions.extract_options!(names)
|
43
|
+
|
44
|
+
@attributes = names
|
45
|
+
@options = options
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
attr_reader :attributes, :options
|
50
|
+
|
51
|
+
def each_validated_attribute_on(object)
|
52
|
+
return to_enum(:each_validated_attribute, object) unless block_given?
|
53
|
+
|
54
|
+
attributes.each do |attribute_name|
|
55
|
+
value = object.send(attribute_name)
|
56
|
+
yield(attribute_name, value)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'standalone_validator'
|
2
|
+
require_relative 'common_rails_options'
|
3
|
+
|
4
|
+
class StandaloneValidator
|
5
|
+
module NamedValidations
|
6
|
+
class ValidatesPresenceOf < StandaloneValidator
|
7
|
+
register_as :validates_presence_of
|
8
|
+
|
9
|
+
include CommonRailsOptions
|
10
|
+
|
11
|
+
include_validation do |object, result|
|
12
|
+
each_validated_attribute_on(object) do |attribute_name, value|
|
13
|
+
if value.blank?
|
14
|
+
result.add_violation(attribute_name, :blank)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'standalone_validator/validation_result_builder'
|
2
|
+
require 'hamster/list'
|
3
|
+
|
4
|
+
class StandaloneValidator
|
5
|
+
class ValidationResult
|
6
|
+
def self.build_for(object, &block)
|
7
|
+
builder = ValidationResultBuilder.new
|
8
|
+
builder.validated_object = object
|
9
|
+
|
10
|
+
if block.arity == 1
|
11
|
+
block.call(builder)
|
12
|
+
else
|
13
|
+
builder.instance_eval(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
builder.result
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :validated_object, :violations
|
20
|
+
|
21
|
+
def initialize(attributes)
|
22
|
+
@validated_object = attributes.fetch(:validated_object) { nil }
|
23
|
+
|
24
|
+
violations = attributes.fetch(:violations) { Hamster::EmptyList }
|
25
|
+
@violations = violations.to_list
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_errors_to(errors_object)
|
29
|
+
violations.each do |violation|
|
30
|
+
violation.add_to(errors_object)
|
31
|
+
end
|
32
|
+
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
OK = new({})
|
37
|
+
|
38
|
+
include Enumerable
|
39
|
+
|
40
|
+
def each(&block)
|
41
|
+
violations.each(&block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def empty
|
45
|
+
violations.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
def ok?
|
49
|
+
violations.empty?
|
50
|
+
end
|
51
|
+
|
52
|
+
alias_method :valid?, :ok?
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'standalone_validator/validation_result'
|
2
|
+
require 'standalone_validator/violation'
|
3
|
+
|
4
|
+
class StandaloneValidator
|
5
|
+
class ValidationResultBuilder
|
6
|
+
attr_writer :validated_object
|
7
|
+
|
8
|
+
def merge_result(result)
|
9
|
+
combined_results << result
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_violation(attribute_name, violation_type_or_message)
|
14
|
+
creation_attributes = {
|
15
|
+
:attribute => attribute_name.to_sym,
|
16
|
+
:source_object => validated_object
|
17
|
+
}
|
18
|
+
|
19
|
+
if violation_type_or_message.kind_of?(Symbol)
|
20
|
+
creation_attributes[:type] = violation_type_or_message
|
21
|
+
else
|
22
|
+
creation_attributes[:message] = violation_type_or_message
|
23
|
+
end
|
24
|
+
|
25
|
+
push_violation(Violation.new(creation_attributes))
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def result
|
30
|
+
ValidationResult.new(
|
31
|
+
:validated_object => validated_object,
|
32
|
+
:violations => all_violations
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
attr_reader :validated_object
|
38
|
+
|
39
|
+
def all_violations
|
40
|
+
combined_results.inject(violations) do |violations_list, subresult|
|
41
|
+
violations_list.append(subresult.violations)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def combined_results
|
46
|
+
@combined_results ||= []
|
47
|
+
end
|
48
|
+
|
49
|
+
def push_violation(violation)
|
50
|
+
@violations = violations.cons(violation)
|
51
|
+
end
|
52
|
+
|
53
|
+
def violations
|
54
|
+
@violations ||= Hamster.list
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
|
3
|
+
class StandaloneValidator
|
4
|
+
class Violation
|
5
|
+
include Virtus::ValueObject
|
6
|
+
|
7
|
+
attribute :source_object, Object
|
8
|
+
attribute :attribute, Symbol, :default => :base
|
9
|
+
attribute :type, Symbol, :default => :invalid
|
10
|
+
attribute :message, String, :default => nil
|
11
|
+
|
12
|
+
def add_to(errors_object)
|
13
|
+
errors_object.add(attribute, message || type)
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'standalone_validator'
|
3
|
+
|
4
|
+
describe "core features" do
|
5
|
+
let(:valid_object) { stub(:valid? => true) }
|
6
|
+
let(:invalid_object) { stub(:valid? => false) }
|
7
|
+
|
8
|
+
it "accepts inline lambdas via 'include_validation'" do
|
9
|
+
validator_class = StandaloneValidator.create do
|
10
|
+
include_validation do |object, result|
|
11
|
+
unless object.valid?
|
12
|
+
result.add_violation(:base, "is invalid")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
validator = validator_class.new
|
18
|
+
|
19
|
+
result = validator.violations_of(valid_object)
|
20
|
+
expect(result).to be_ok
|
21
|
+
|
22
|
+
result = validator.violations_of(invalid_object)
|
23
|
+
expect(result).to_not be_ok
|
24
|
+
end
|
25
|
+
|
26
|
+
specify "the methods added to the class are accessible inside the lambda" do
|
27
|
+
validator_class = StandaloneValidator.create do
|
28
|
+
include_validation do |object, result|
|
29
|
+
unless valid?(object)
|
30
|
+
result.add_violation(:base, "is invalid")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid?(object)
|
35
|
+
object.valid?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
validator = validator_class.new
|
40
|
+
|
41
|
+
result = validator.violations_of(valid_object)
|
42
|
+
expect(result).to be_ok
|
43
|
+
|
44
|
+
result = validator.violations_of(invalid_object)
|
45
|
+
expect(result).to_not be_ok
|
46
|
+
end
|
47
|
+
|
48
|
+
specify "initialization options can be accepted by defining a constructor" do
|
49
|
+
validator_class = StandaloneValidator.create do
|
50
|
+
def initialize(bias)
|
51
|
+
@bias = bias
|
52
|
+
end
|
53
|
+
|
54
|
+
include_validation do |object, result|
|
55
|
+
unless @bias
|
56
|
+
result.add_violation(:base, "is invalid")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
biased_validator = validator_class.new(true)
|
62
|
+
result = biased_validator.violations_of(invalid_object)
|
63
|
+
expect(result).to be_ok
|
64
|
+
|
65
|
+
biased_validator = validator_class.new(false)
|
66
|
+
result = biased_validator.violations_of(valid_object)
|
67
|
+
expect(result).to_not be_ok
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "standalone_validator"
|
3
|
+
|
4
|
+
module StandaloneValidator::NamedValidations
|
5
|
+
describe "validates_presence_of" do
|
6
|
+
let(:blank_value) { stub(:blank? => true) }
|
7
|
+
let(:non_blank_value) { stub(:blank? => false) }
|
8
|
+
|
9
|
+
it "adds a 'blank' violation if the attribute is blank" do
|
10
|
+
validator = ValidatesPresenceOf.new(:foo)
|
11
|
+
result = validator.violations_of(stub(:foo => blank_value))
|
12
|
+
expect(result).to_not be_ok
|
13
|
+
end
|
14
|
+
|
15
|
+
it "doesn't add the violation if the attribute isn't blank" do
|
16
|
+
validator = ValidatesPresenceOf.new(:foo)
|
17
|
+
result = validator.violations_of(stub(:foo => non_blank_value))
|
18
|
+
expect(result).to be_ok
|
19
|
+
end
|
20
|
+
|
21
|
+
it "accepts an :if condition that can block the validation" do
|
22
|
+
validator = ValidatesPresenceOf.new :foo, :if => :bar
|
23
|
+
|
24
|
+
triggers = stub(:foo => blank_value, :bar => true)
|
25
|
+
doesnt_trigger = stub(:foo => blank_value, :bar => false)
|
26
|
+
|
27
|
+
result = validator.violations_of(triggers)
|
28
|
+
expect(result).to_not be_ok
|
29
|
+
|
30
|
+
result = validator.violations_of(doesnt_trigger)
|
31
|
+
expect(result).to be_ok
|
32
|
+
end
|
33
|
+
|
34
|
+
it "accepts an :unless condition that can block the validation" do
|
35
|
+
validator = ValidatesPresenceOf.new :foo, :unless => :bar
|
36
|
+
|
37
|
+
triggers = stub(:foo => blank_value, :bar => false)
|
38
|
+
doesnt_trigger = stub(:foo => blank_value, :bar => true)
|
39
|
+
|
40
|
+
result = validator.violations_of(triggers)
|
41
|
+
expect(result).to_not be_ok
|
42
|
+
|
43
|
+
result = validator.violations_of(doesnt_trigger)
|
44
|
+
expect(result).to be_ok
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'standalone_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "standalone_validator"
|
8
|
+
spec.version = StandaloneValidator::VERSION
|
9
|
+
spec.authors = ["Renato Zannon"]
|
10
|
+
spec.email = ["renato.riccieri@gmail.com"]
|
11
|
+
spec.description = <<-DESCRIPTION
|
12
|
+
A library for creating PORO validators that are composable and can be used
|
13
|
+
with ActiveRecord or standalone.
|
14
|
+
DESCRIPTION
|
15
|
+
|
16
|
+
spec.summary = %q{PORO standalone validators compatible with ActiveRecord}
|
17
|
+
spec.homepage = "http://github.com/riccieri/standalone_validator"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files`.split($/)
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "backports"
|
26
|
+
spec.add_dependency "hamster", "~> 0.4"
|
27
|
+
spec.add_dependency "virtus", "~> 0.5"
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
30
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
31
|
+
spec.add_development_dependency "rake"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: standalone_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Renato Zannon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: backports
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hamster
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: virtus
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.13'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.13'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: |2
|
98
|
+
A library for creating PORO validators that are composable and can be used
|
99
|
+
with ActiveRecord or standalone.
|
100
|
+
email:
|
101
|
+
- renato.riccieri@gmail.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- .gitignore
|
107
|
+
- .rspec
|
108
|
+
- .travis.yml
|
109
|
+
- Gemfile
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- lib/standalone_validator.rb
|
114
|
+
- lib/standalone_validator/definitions.rb
|
115
|
+
- lib/standalone_validator/named_validations.rb
|
116
|
+
- lib/standalone_validator/named_validations/common_rails_options.rb
|
117
|
+
- lib/standalone_validator/named_validations/validates_presence_of.rb
|
118
|
+
- lib/standalone_validator/validation_result.rb
|
119
|
+
- lib/standalone_validator/validation_result_builder.rb
|
120
|
+
- lib/standalone_validator/version.rb
|
121
|
+
- lib/standalone_validator/violation.rb
|
122
|
+
- spec/integration_spec.rb
|
123
|
+
- spec/named_validations/validates_presence_of_spec.rb
|
124
|
+
- spec/spec_helper.rb
|
125
|
+
- standalone_validator.gemspec
|
126
|
+
homepage: http://github.com/riccieri/standalone_validator
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.0.0
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: PORO standalone validators compatible with ActiveRecord
|
150
|
+
test_files:
|
151
|
+
- spec/integration_spec.rb
|
152
|
+
- spec/named_validations/validates_presence_of_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
has_rdoc:
|