validatable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +117 -0
- data/lib/base.rb +125 -0
- data/lib/errors.rb +23 -0
- data/lib/validatable.rb +9 -0
- data/lib/validates_acceptance_of.rb +7 -0
- data/lib/validates_confirmation_of.rb +7 -0
- data/lib/validates_format_of.rb +13 -0
- data/lib/validates_length_of.rb +18 -0
- data/lib/validates_presence_of.rb +7 -0
- data/lib/validation_base.rb +9 -0
- data/rakefile.rb +51 -0
- data/test/all_tests.rb +1 -0
- data/test/errors_test.rb +9 -0
- data/test/functional_test.rb +80 -0
- data/test/test_helper.rb +12 -0
- data/test/validatable_test.rb +44 -0
- data/test/validates_acceptance_of_test.rb +15 -0
- data/test/validates_confirmation_of_test.rb +15 -0
- data/test/validates_format_of_test.rb +13 -0
- data/test/validates_length_of_test.rb +23 -0
- data/test/validates_presence_of_test.rb +13 -0
- metadata +69 -0
data/README
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
= Validatable
|
2
|
+
|
3
|
+
Validatable is a library for adding validations.
|
4
|
+
|
5
|
+
by Jay[http://jayfields.blogspot.com] Fields[http://jayfields.blogspot.com]
|
6
|
+
|
7
|
+
== Download and Installation
|
8
|
+
|
9
|
+
You can download Validatable from here[http://rubyforge.org/projects/validatable] or install it with the following command.
|
10
|
+
|
11
|
+
$ gem install validatable
|
12
|
+
|
13
|
+
== License
|
14
|
+
|
15
|
+
You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt).
|
16
|
+
|
17
|
+
== Examples
|
18
|
+
|
19
|
+
See the tests for more examples
|
20
|
+
|
21
|
+
=== Test Helper
|
22
|
+
|
23
|
+
require 'test/unit'
|
24
|
+
require 'rubygems'
|
25
|
+
require 'mocha'
|
26
|
+
require File.dirname(__FILE__) + '/../lib/validatable'
|
27
|
+
|
28
|
+
class << Test::Unit::TestCase
|
29
|
+
def test(name, &block)
|
30
|
+
test_name = :"test_#{name.gsub(' ','_')}"
|
31
|
+
raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
|
32
|
+
define_method test_name, &block
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
=== Functional Tests
|
37
|
+
|
38
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
39
|
+
|
40
|
+
class FunctionalTest < Test::Unit::TestCase
|
41
|
+
test "given no name, when validated, then error is in the objects error collection" do
|
42
|
+
klass = Class.new do
|
43
|
+
include Validatable
|
44
|
+
attr_accessor :name
|
45
|
+
validates_presence_of :name
|
46
|
+
end
|
47
|
+
instance = klass.new
|
48
|
+
instance.valid?
|
49
|
+
assert_equal "can't be empty", instance.errors.on(:name)
|
50
|
+
end
|
51
|
+
|
52
|
+
test "given invalid name format, when validated, then error is in the objects error collection" do
|
53
|
+
klass = Class.new do
|
54
|
+
include Validatable
|
55
|
+
attr_accessor :name
|
56
|
+
validates_format_of :name, :with => /.+/
|
57
|
+
end
|
58
|
+
instance = klass.new
|
59
|
+
instance.valid?
|
60
|
+
assert_equal "is invalid", instance.errors.on(:name)
|
61
|
+
end
|
62
|
+
|
63
|
+
test "given no acceptance, when validated, then error is in the objects error collection" do
|
64
|
+
klass = Class.new do
|
65
|
+
include Validatable
|
66
|
+
attr_accessor :name
|
67
|
+
validates_acceptance_of :name
|
68
|
+
end
|
69
|
+
instance = klass.new
|
70
|
+
instance.valid?
|
71
|
+
assert_equal "must be accepted", instance.errors.on(:name)
|
72
|
+
end
|
73
|
+
|
74
|
+
test "given non matching attributes, when validated, then error is in the objects error collection" do
|
75
|
+
klass = Class.new do
|
76
|
+
include Validatable
|
77
|
+
attr_accessor :name, :name_confirmation
|
78
|
+
validates_confirmation_of :name
|
79
|
+
end
|
80
|
+
instance = klass.new
|
81
|
+
instance.name = "foo"
|
82
|
+
instance.name_confirmation = "bar"
|
83
|
+
instance.valid?
|
84
|
+
assert_equal "doesn't match confirmation", instance.errors.on(:name)
|
85
|
+
end
|
86
|
+
|
87
|
+
test "given short value, when validated, then error is in the objects error collection" do
|
88
|
+
klass = Class.new do
|
89
|
+
include Validatable
|
90
|
+
attr_accessor :name
|
91
|
+
validates_length_of :name, :minimum => 2
|
92
|
+
end
|
93
|
+
instance = klass.new
|
94
|
+
instance.valid?
|
95
|
+
assert_equal "is invalid", instance.errors.on(:name)
|
96
|
+
end
|
97
|
+
|
98
|
+
test "given a child class with validations, when parent class is validated, then the error is in the parent objects error collection" do
|
99
|
+
child_class = Class.new do
|
100
|
+
include Validatable
|
101
|
+
attr_accessor :name, :address
|
102
|
+
validates_presence_of :name
|
103
|
+
validates_format_of :address, :with => /.+/
|
104
|
+
end
|
105
|
+
klass = Class.new do
|
106
|
+
include Validatable
|
107
|
+
include_validations_for :child
|
108
|
+
define_method :child do
|
109
|
+
child_class.new
|
110
|
+
end
|
111
|
+
end
|
112
|
+
instance = klass.new
|
113
|
+
instance.valid?
|
114
|
+
assert_equal "is invalid", instance.errors.on(:address)
|
115
|
+
assert_equal "can't be empty", instance.errors.on(:name)
|
116
|
+
end
|
117
|
+
end
|
data/lib/base.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
module Validatable
|
2
|
+
module ClassMethods
|
3
|
+
# call-seq: validates_format_of(*args)
|
4
|
+
#
|
5
|
+
# Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided.
|
6
|
+
#
|
7
|
+
# class Person < ActiveRecord::Base
|
8
|
+
# validates_format_of :first_name, :with => /[ A-Za-z]/
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# A regular expression must be provided or else an exception will be raised.
|
12
|
+
def validates_format_of(*args)
|
13
|
+
validate_all(args) do |attribute, message, options|
|
14
|
+
self.validations << ValidatesFormatOf.new(attribute, options[:with], message || "is invalid")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def validates_length_of(*args)
|
19
|
+
validate_all(args) do |attribute, message, options|
|
20
|
+
self.validations << ValidatesLengthOf.new(attribute, options[:minimum], options[:maximum], message || "is invalid")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def validates_acceptance_of(*args)
|
25
|
+
validate_all(args) do |attribute, message, options|
|
26
|
+
self.validations << ValidatesAcceptanceOf.new(attribute, message || "must be accepted")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def validates_confirmation_of(*args)
|
31
|
+
validate_all(args) do |attribute, message, options|
|
32
|
+
self.validations << ValidatesConfirmationOf.new(attribute, message || "doesn't match confirmation")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
# call-seq: validates_presence_of(*args)
|
38
|
+
#
|
39
|
+
# Validates that the specified attributes are not nil or an empty string
|
40
|
+
#
|
41
|
+
# class Person
|
42
|
+
# include Validatable
|
43
|
+
# validates_presence_of :first_name
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# The first_name attribute must be in the object and it cannot be nil or empty.
|
47
|
+
def validates_presence_of(*args)
|
48
|
+
validate_all(args) do |attribute, message, options|
|
49
|
+
self.validations << ValidatesPresenceOf.new(attribute, message || "can't be empty")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# call-seq: include_validations_for(*args)
|
54
|
+
#
|
55
|
+
# Validates the specified attributes.
|
56
|
+
#
|
57
|
+
# class PersonPresenter
|
58
|
+
# include Validatable
|
59
|
+
# include_validations_for :person
|
60
|
+
# attr_accessor :person
|
61
|
+
#
|
62
|
+
# def initialize(person)
|
63
|
+
# @person = person
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# The person attribute will be validated. If person is invalid the errors will be added to the PersonPresenter errors collection.
|
68
|
+
def include_validations_for(*args)
|
69
|
+
children_to_validate.concat args
|
70
|
+
end
|
71
|
+
|
72
|
+
def validate(instance) #:nodoc:
|
73
|
+
self.validations.each do |validation|
|
74
|
+
instance.errors.add(validation.attribute, validation.message) unless validation.valid?(instance)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def validate_children(instance) #:nodoc:
|
79
|
+
self.children_to_validate.each do |child_name|
|
80
|
+
child = instance.send child_name
|
81
|
+
child.valid?
|
82
|
+
child.errors.each do |attribute, message|
|
83
|
+
instance.errors.add(attribute, message)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
def validate_all(args, &block) #:nodoc:
|
90
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
91
|
+
args.each do |attribute|
|
92
|
+
yield attribute, options[:message], options
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def children_to_validate #:nodoc:
|
97
|
+
@children_to_validate ||= []
|
98
|
+
end
|
99
|
+
|
100
|
+
def validations #:nodoc:
|
101
|
+
@validations ||= []
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.included(klass) #:nodoc:
|
106
|
+
klass.extend Validatable::ClassMethods
|
107
|
+
end
|
108
|
+
|
109
|
+
# call-seq: valid?
|
110
|
+
#
|
111
|
+
# Returns true if no errors were added otherwise false.
|
112
|
+
def valid?
|
113
|
+
errors.clear
|
114
|
+
self.class.validate(self)
|
115
|
+
self.class.validate_children(self)
|
116
|
+
errors.empty?
|
117
|
+
end
|
118
|
+
|
119
|
+
# call-seq: errors
|
120
|
+
#
|
121
|
+
# Returns the Errors object that holds all information about attribute error messages.
|
122
|
+
def errors
|
123
|
+
@errors ||= Validatable::Errors.new
|
124
|
+
end
|
125
|
+
end
|
data/lib/errors.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Validatable
|
2
|
+
class Errors
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def_delegators :errors, :empty?, :clear, :each
|
6
|
+
|
7
|
+
# call-seq: on(attribute)
|
8
|
+
#
|
9
|
+
# Returns nil, if no errors are associated with the specified attribute.
|
10
|
+
# Returns the error message, if one error is associated with the specified attribute.
|
11
|
+
def on(attribute)
|
12
|
+
errors[attribute.to_sym]
|
13
|
+
end
|
14
|
+
|
15
|
+
def add(attribute, message) #:nodoc:
|
16
|
+
errors[attribute.to_sym] = message
|
17
|
+
end
|
18
|
+
|
19
|
+
def errors #:nodoc:
|
20
|
+
@errors ||= {}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/validatable.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/errors')
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/base')
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/validation_base')
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + '/validates_format_of')
|
6
|
+
require File.expand_path(File.dirname(__FILE__) + '/validates_presence_of')
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + '/validates_acceptance_of')
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + '/validates_confirmation_of')
|
9
|
+
require File.expand_path(File.dirname(__FILE__) + '/validates_length_of')
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesFormatOf < ValidationBase #:nodoc:
|
3
|
+
attr_accessor :regex
|
4
|
+
def initialize(attribute, regex, message)
|
5
|
+
self.regex = regex
|
6
|
+
super attribute, message
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?(instance)
|
10
|
+
instance.send(self.attribute) =~ self.regex && true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesLengthOf < ValidationBase #:nodoc:
|
3
|
+
attr_accessor :attribute, :minimum, :maximum
|
4
|
+
def initialize(attribute, minimum, maximum, message)
|
5
|
+
self.minimum = minimum
|
6
|
+
self.maximum = maximum
|
7
|
+
super attribute, message
|
8
|
+
end
|
9
|
+
|
10
|
+
def valid?(instance)
|
11
|
+
valid = true
|
12
|
+
value = instance.send(self.attribute) || ""
|
13
|
+
valid &&= value.length <= maximum unless maximum.nil?
|
14
|
+
valid &&= value.length >= minimum unless minimum.nil?
|
15
|
+
valid
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/rakefile.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/contrib/sshpublisher'
|
5
|
+
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
task :test do
|
9
|
+
require File.dirname(__FILE__) + '/test/all_tests.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
desc 'Generate RDoc'
|
13
|
+
Rake::RDocTask.new do |task|
|
14
|
+
task.main = 'README'
|
15
|
+
task.title = 'Validatable'
|
16
|
+
task.rdoc_dir = 'doc'
|
17
|
+
task.options << "--line-numbers" << "--inline-source"
|
18
|
+
task.rdoc_files.include('README', 'lib/**/*.rb')
|
19
|
+
%x[erb README_TEMPLATE > README]
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Upload RDoc to RubyForge"
|
23
|
+
task :publish_rdoc => [:rdoc] do
|
24
|
+
Rake::SshDirPublisher.new("jaycfields@rubyforge.org", "/var/www/gforge-projects/validatable", "doc").upload
|
25
|
+
end
|
26
|
+
|
27
|
+
Gem::manage_gems
|
28
|
+
|
29
|
+
specification = Gem::Specification.new do |s|
|
30
|
+
s.name = "validatable"
|
31
|
+
s.summary = "Validatable is a library for adding validations."
|
32
|
+
s.version = "1.0.0"
|
33
|
+
s.author = 'Jay Fields'
|
34
|
+
s.description = "Validatable is a library for adding validations."
|
35
|
+
s.email = 'validatable-developer@rubyforge.org'
|
36
|
+
s.homepage = 'http://validatable.rubyforge.org'
|
37
|
+
s.rubyforge_project = 'validatable'
|
38
|
+
|
39
|
+
s.has_rdoc = true
|
40
|
+
s.extra_rdoc_files = ['README']
|
41
|
+
s.rdoc_options << '--title' << 'SQL DSL' << '--main' << 'README' << '--line-numbers'
|
42
|
+
|
43
|
+
s.autorequire = 'validatable'
|
44
|
+
s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*$', 'rakefile.rb'].to_a
|
45
|
+
s.test_file = "test/all_tests.rb"
|
46
|
+
end
|
47
|
+
|
48
|
+
Rake::GemPackageTask.new(specification) do |package|
|
49
|
+
package.need_zip = true
|
50
|
+
package.need_tar = true
|
51
|
+
end
|
data/test/all_tests.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir['**/*_test.rb'].each { |test_case| require test_case }
|
data/test/errors_test.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ErrorsTest < Test::Unit::TestCase
|
4
|
+
test "when an error is added, then it can be returned from on" do
|
5
|
+
errors = Validatable::Errors.new
|
6
|
+
errors.add(:attribute, "message")
|
7
|
+
assert_equal "message", errors.on(:attribute)
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class FunctionalTest < Test::Unit::TestCase
|
4
|
+
test "given no name, when validated, then error is in the objects error collection" do
|
5
|
+
klass = Class.new do
|
6
|
+
include Validatable
|
7
|
+
attr_accessor :name
|
8
|
+
validates_presence_of :name
|
9
|
+
end
|
10
|
+
instance = klass.new
|
11
|
+
instance.valid?
|
12
|
+
assert_equal "can't be empty", instance.errors.on(:name)
|
13
|
+
end
|
14
|
+
|
15
|
+
test "given invalid name format, when validated, then error is in the objects error collection" do
|
16
|
+
klass = Class.new do
|
17
|
+
include Validatable
|
18
|
+
attr_accessor :name
|
19
|
+
validates_format_of :name, :with => /.+/
|
20
|
+
end
|
21
|
+
instance = klass.new
|
22
|
+
instance.valid?
|
23
|
+
assert_equal "is invalid", instance.errors.on(:name)
|
24
|
+
end
|
25
|
+
|
26
|
+
test "given no acceptance, when validated, then error is in the objects error collection" do
|
27
|
+
klass = Class.new do
|
28
|
+
include Validatable
|
29
|
+
attr_accessor :name
|
30
|
+
validates_acceptance_of :name
|
31
|
+
end
|
32
|
+
instance = klass.new
|
33
|
+
instance.valid?
|
34
|
+
assert_equal "must be accepted", instance.errors.on(:name)
|
35
|
+
end
|
36
|
+
|
37
|
+
test "given non matching attributes, when validated, then error is in the objects error collection" do
|
38
|
+
klass = Class.new do
|
39
|
+
include Validatable
|
40
|
+
attr_accessor :name, :name_confirmation
|
41
|
+
validates_confirmation_of :name
|
42
|
+
end
|
43
|
+
instance = klass.new
|
44
|
+
instance.name = "foo"
|
45
|
+
instance.name_confirmation = "bar"
|
46
|
+
instance.valid?
|
47
|
+
assert_equal "doesn't match confirmation", instance.errors.on(:name)
|
48
|
+
end
|
49
|
+
|
50
|
+
test "given short value, when validated, then error is in the objects error collection" do
|
51
|
+
klass = Class.new do
|
52
|
+
include Validatable
|
53
|
+
attr_accessor :name
|
54
|
+
validates_length_of :name, :minimum => 2
|
55
|
+
end
|
56
|
+
instance = klass.new
|
57
|
+
instance.valid?
|
58
|
+
assert_equal "is invalid", instance.errors.on(:name)
|
59
|
+
end
|
60
|
+
|
61
|
+
test "given a child class with validations, when parent class is validated, then the error is in the parent objects error collection" do
|
62
|
+
child_class = Class.new do
|
63
|
+
include Validatable
|
64
|
+
attr_accessor :name, :address
|
65
|
+
validates_presence_of :name
|
66
|
+
validates_format_of :address, :with => /.+/
|
67
|
+
end
|
68
|
+
klass = Class.new do
|
69
|
+
include Validatable
|
70
|
+
include_validations_for :child
|
71
|
+
define_method :child do
|
72
|
+
child_class.new
|
73
|
+
end
|
74
|
+
end
|
75
|
+
instance = klass.new
|
76
|
+
instance.valid?
|
77
|
+
assert_equal "is invalid", instance.errors.on(:address)
|
78
|
+
assert_equal "can't be empty", instance.errors.on(:name)
|
79
|
+
end
|
80
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'mocha'
|
4
|
+
require File.dirname(__FILE__) + '/../lib/validatable'
|
5
|
+
|
6
|
+
class << Test::Unit::TestCase
|
7
|
+
def test(name, &block)
|
8
|
+
test_name = :"test_#{name.gsub(' ','_')}"
|
9
|
+
raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
|
10
|
+
define_method test_name, &block
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ValidatableTest < Test::Unit::TestCase
|
4
|
+
test "given a validation that returns false when object is validated then valid returns false" do
|
5
|
+
validation = stub(:valid? => false, :attribute => "attribute", :message => "message")
|
6
|
+
klass = Class.new do
|
7
|
+
include Validatable
|
8
|
+
validations << validation
|
9
|
+
end
|
10
|
+
assert_equal false, klass.new.valid?
|
11
|
+
end
|
12
|
+
|
13
|
+
test "when validations are included for a child, then the list is maintained as an array of args" do
|
14
|
+
klass = Class.new do
|
15
|
+
include Validatable
|
16
|
+
include_validations_for :anything, :else
|
17
|
+
end
|
18
|
+
assert_equal [:anything, :else], klass.send(:children_to_validate)
|
19
|
+
end
|
20
|
+
|
21
|
+
test "when validate is executed, then messages are added for each validation that fails" do
|
22
|
+
klass = Class.new do
|
23
|
+
include Validatable
|
24
|
+
end
|
25
|
+
klass.send(:validations) << stub(:valid? => false, :attribute => 'attribute', :message => 'message')
|
26
|
+
klass.send(:validations) << stub(:valid? => false, :attribute => 'attribute2', :message => 'message2')
|
27
|
+
instance=mock
|
28
|
+
instance.expects(:errors).returns(errors=mock).times 2
|
29
|
+
errors.expects(:add).with('attribute', 'message')
|
30
|
+
errors.expects(:add).with('attribute2', 'message2')
|
31
|
+
klass.validate(instance)
|
32
|
+
end
|
33
|
+
|
34
|
+
test "when valid is called, then the errors collection is cleared and reinitialized" do
|
35
|
+
klass = Class.new do
|
36
|
+
include Validatable
|
37
|
+
end
|
38
|
+
instance = klass.new
|
39
|
+
instance.errors.add(:attribute, "message")
|
40
|
+
instance.valid?
|
41
|
+
assert_equal true, instance.errors.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ValidatesAcceptanceOfTest < Test::Unit::TestCase
|
4
|
+
test "valid acceptance" do
|
5
|
+
validation = Validatable::ValidatesAcceptanceOf.new(:acceptance, :message)
|
6
|
+
instance = stub(:acceptance=>'true')
|
7
|
+
assert_equal true, validation.valid?(instance)
|
8
|
+
end
|
9
|
+
|
10
|
+
test "invalid acceptance" do
|
11
|
+
validation = Validatable::ValidatesAcceptanceOf.new(:acceptance, :message)
|
12
|
+
instance = stub(:acceptance=>'false')
|
13
|
+
assert_equal false, validation.valid?(instance)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ValidatesConfirmationOfTest < Test::Unit::TestCase
|
4
|
+
test "valid confirmation" do
|
5
|
+
validation = Validatable::ValidatesConfirmationOf.new(:username, :message)
|
6
|
+
instance = stub(:username=>"username", :username_confirmation=>"username")
|
7
|
+
assert_equal true, validation.valid?(instance)
|
8
|
+
end
|
9
|
+
|
10
|
+
test "invalid confirmation" do
|
11
|
+
validation = Validatable::ValidatesConfirmationOf.new(:username, :message)
|
12
|
+
instance = stub(:username=>"username", :username_confirmation=>"usessrname")
|
13
|
+
assert_equal false, validation.valid?(instance)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ValidatesFormatOfTest < Test::Unit::TestCase
|
4
|
+
test "when attribute value does not match the given regex, then valid is false" do
|
5
|
+
validation = Validatable::ValidatesFormatOf.new(:name, /book/, "message")
|
6
|
+
assert_equal false, validation.valid?(stub_everything)
|
7
|
+
end
|
8
|
+
|
9
|
+
test "when attribute value does match the given regex, then valid is true" do
|
10
|
+
validation = Validatable::ValidatesFormatOf.new(:name, /book/, "message")
|
11
|
+
assert_equal true, validation.valid?(stub(:name=>"book"))
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ValidatesLengthOfTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
test "max length" do
|
6
|
+
validation = Validatable::ValidatesLengthOf.new(:username, nil, 8, :message)
|
7
|
+
instance = stub(:username=>"usernamefdfd")
|
8
|
+
assert_equal false, validation.valid?(instance)
|
9
|
+
end
|
10
|
+
|
11
|
+
test "min length" do
|
12
|
+
validation = Validatable::ValidatesLengthOf.new(:username, 2, nil, :message)
|
13
|
+
instance = stub(:username=>"u")
|
14
|
+
assert_equal false, validation.valid?(instance)
|
15
|
+
end
|
16
|
+
|
17
|
+
test "valid length" do
|
18
|
+
validation = Validatable::ValidatesLengthOf.new(:username, 2, 8, :message)
|
19
|
+
instance = stub(:username=>"udfgdf")
|
20
|
+
assert_equal true, validation.valid?(instance)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ValidatesPresenceOfTest < Test::Unit::TestCase
|
4
|
+
test "when attribute value does not match the given regex, then valid is false" do
|
5
|
+
validation = Validatable::ValidatesPresenceOf.new(:name, "message")
|
6
|
+
assert_equal false, validation.valid?(stub_everything)
|
7
|
+
end
|
8
|
+
|
9
|
+
test "when attribute value does match the given regex, then valid is true" do
|
10
|
+
validation = Validatable::ValidatesPresenceOf.new(:name, "message")
|
11
|
+
assert_equal true, validation.valid?(stub(:name=>"book"))
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: validatable
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-02-03 00:00:00 -05:00
|
8
|
+
summary: Validatable is a library for adding validations.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: validatable-developer@rubyforge.org
|
12
|
+
homepage: http://validatable.rubyforge.org
|
13
|
+
rubyforge_project: validatable
|
14
|
+
description: Validatable is a library for adding validations.
|
15
|
+
autorequire: validatable
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Jay Fields
|
30
|
+
files:
|
31
|
+
- lib/base.rb
|
32
|
+
- lib/errors.rb
|
33
|
+
- lib/validatable.rb
|
34
|
+
- lib/validates_acceptance_of.rb
|
35
|
+
- lib/validates_confirmation_of.rb
|
36
|
+
- lib/validates_format_of.rb
|
37
|
+
- lib/validates_length_of.rb
|
38
|
+
- lib/validates_presence_of.rb
|
39
|
+
- lib/validation_base.rb
|
40
|
+
- test/all_tests.rb
|
41
|
+
- test/errors_test.rb
|
42
|
+
- test/functional_test.rb
|
43
|
+
- test/test_helper.rb
|
44
|
+
- test/validatable_test.rb
|
45
|
+
- test/validates_acceptance_of_test.rb
|
46
|
+
- test/validates_confirmation_of_test.rb
|
47
|
+
- test/validates_format_of_test.rb
|
48
|
+
- test/validates_length_of_test.rb
|
49
|
+
- test/validates_presence_of_test.rb
|
50
|
+
- rakefile.rb
|
51
|
+
- README
|
52
|
+
test_files:
|
53
|
+
- test/all_tests.rb
|
54
|
+
rdoc_options:
|
55
|
+
- --title
|
56
|
+
- SQL DSL
|
57
|
+
- --main
|
58
|
+
- README
|
59
|
+
- --line-numbers
|
60
|
+
extra_rdoc_files:
|
61
|
+
- README
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
dependencies: []
|
69
|
+
|