common_validators 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Guardfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +19 -0
- data/Rakefile +29 -0
- data/app/validators/date_format_validator.rb +19 -0
- data/app/validators/email_format_validator.rb +18 -0
- data/app/validators/money_format_validator.rb +37 -0
- data/app/validators/phone_number_format_validator.rb +20 -0
- data/app/validators/slug_format_validator.rb +17 -0
- data/app/validators/url_format_validator.rb +19 -0
- data/common_validators.gemspec +27 -0
- data/config/locales/en.yml +9 -0
- data/lib/common_validators/engine.rb +5 -0
- data/lib/common_validators/railtie.rb +9 -0
- data/lib/common_validators/version.rb +3 -0
- data/lib/common_validators.rb +2 -0
- data/test/date_format_validator_test.rb +39 -0
- data/test/email_format_validator_test.rb +53 -0
- data/test/money_format_validator_test.rb +47 -0
- data/test/phone_number_format_validator_test.rb +41 -0
- data/test/slug_format_validator_test.rb +42 -0
- data/test/test_helper.rb +15 -0
- data/test/url_format_validator_test.rb +40 -0
- metadata +162 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :test do
|
5
|
+
watch(%r{^test/.+_test\.rb$})
|
6
|
+
watch('test/test_helper.rb') { "test" }
|
7
|
+
|
8
|
+
watch(%r{^app/models/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
|
9
|
+
watch(%r{^app/validators/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
|
10
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Venture Media Labs Inc.
|
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,19 @@
|
|
1
|
+
# CommonValidators
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/gshaw/common_validators.png?branch=master)](https://travis-ci.org/gshaw/common_validators)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/common_validators.png)](http://badge.fury.io/rb/common_validators)
|
5
|
+
|
6
|
+
Collection of common validators for Rails applications.
|
7
|
+
|
8
|
+
[RubyDoc Documentation](http://rubydoc.info/github/gshaw/common_validators/master/frames)
|
9
|
+
|
10
|
+
### Examples
|
11
|
+
|
12
|
+
```
|
13
|
+
validates :email, email_format: true, presence: true
|
14
|
+
validates :date, date_format: true
|
15
|
+
validates :amount, money_format: { exclude_cents: true }
|
16
|
+
validates :phone_number, phone_number_format: true
|
17
|
+
validates :slug, slug_format: true
|
18
|
+
validates :url, url_format: true
|
19
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
rescue LoadError
|
6
|
+
raise 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'CommonValidators'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc_main = 'README.md'
|
16
|
+
rdoc.rdoc_files.include('README.md')
|
17
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
|
22
|
+
Rake::TestTask.new(:test) do |t|
|
23
|
+
t.libs << 'lib'
|
24
|
+
t.libs << 'test'
|
25
|
+
t.pattern = 'test/**/*_test.rb'
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task :default => :test
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Validate fields to be parseable dates
|
2
|
+
#
|
3
|
+
# A blank date is considered valid (use presence validator to check for that)
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
# validates :date, date_format: true # optional
|
7
|
+
# validates :date, date_format: true, presence: true # required
|
8
|
+
class DateFormatValidator < ActiveModel::EachValidator
|
9
|
+
def validate_each(record, attr_name, value)
|
10
|
+
return if value.blank?
|
11
|
+
return if value.class == Date # if value is already Date it must be valid
|
12
|
+
|
13
|
+
begin
|
14
|
+
Date.parse(value)
|
15
|
+
rescue
|
16
|
+
record.errors.add(attr_name, :date_format, options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Validate fields to look like emails
|
2
|
+
#
|
3
|
+
# Is not an exhaustive check but prevents emails that can cause Postfix to
|
4
|
+
# crash. Specifically emails with special characters in the local part.
|
5
|
+
#
|
6
|
+
# A blank email is considered valid (use presence validator to check for that)
|
7
|
+
#
|
8
|
+
# Examples
|
9
|
+
# validates :email, email_format: true # optional
|
10
|
+
# validates :email, email_format: true, presence: true # required
|
11
|
+
class EmailFormatValidator < ActiveModel::EachValidator
|
12
|
+
EMAIL_REGEX = /\A(([^(),:;<>@\[\\\]\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\z/i
|
13
|
+
|
14
|
+
def validate_each(record, attr_name, value)
|
15
|
+
return if value.blank?
|
16
|
+
record.errors.add(attr_name, :email_format, options) unless value =~ EMAIL_REGEX
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Validate fields to look like currency values
|
2
|
+
#
|
3
|
+
# NOTE: Currently limited to USD/CAD values.
|
4
|
+
#
|
5
|
+
# Includes an option to limit value to only whole dollar amounts.
|
6
|
+
#
|
7
|
+
# A blank value is considered valid (use presence validator to check for that)
|
8
|
+
#
|
9
|
+
# Examples
|
10
|
+
# validates :amount, money_format: true # optional
|
11
|
+
# validates :amount, money_format: { exclude_cents: true }
|
12
|
+
# validates :amount, money_format: true, presence: true # required
|
13
|
+
|
14
|
+
require 'bigdecimal'
|
15
|
+
|
16
|
+
class MoneyFormatValidator < ActiveModel::EachValidator
|
17
|
+
MONEY_REGEX = /\A[-+]?\d+(\.\d{1,2})?\z/
|
18
|
+
|
19
|
+
def validate_each(record, attr_name, value)
|
20
|
+
if defined?(ActiveRecord::Base) && record.kind_of?(ActiveRecord::Base)
|
21
|
+
# need to get the actual value being set because BigDecimal will convert
|
22
|
+
# "invalid" to 0.0 making it impossible to validate
|
23
|
+
value = record.send("#{attr_name}_before_type_cast".to_sym)
|
24
|
+
end
|
25
|
+
return if value.blank?
|
26
|
+
|
27
|
+
record.errors.add(attr_name, :money_format, options) unless value =~ MONEY_REGEX
|
28
|
+
|
29
|
+
if options[:exclude_cents] && value_has_cents?(value)
|
30
|
+
record.errors.add(attr_name, :money_format_whole_number)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def value_has_cents?(value)
|
35
|
+
BigDecimal.new(value.to_i) != BigDecimal.new(value)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Validate fields to look like phone numbers
|
2
|
+
#
|
3
|
+
# Is not an exhaustive check, works by stripping all non-digits out and looking
|
4
|
+
# for a string with at least 10 digits.
|
5
|
+
#
|
6
|
+
# A blank phone number is considered valid (use presence validator to check for that)
|
7
|
+
#
|
8
|
+
# Examples
|
9
|
+
# validates :phone_number, phone_number_format: true # optional
|
10
|
+
# validates :phone_number, phone_number_format: true, presence: true # required
|
11
|
+
|
12
|
+
class PhoneNumberFormatValidator < ActiveModel::EachValidator
|
13
|
+
# http://stackoverflow.com/a/123681/265940
|
14
|
+
PHONE_NUMBER_REGEX = /\A[ \d\+\-\(\)ext\.\/]{10,}\z/i
|
15
|
+
|
16
|
+
def validate_each(record, attr_name, value)
|
17
|
+
return if value.blank?
|
18
|
+
record.errors.add(attr_name, :phone_number_format, options) unless value =~ PHONE_NUMBER_REGEX
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Validate fields to be slugs
|
2
|
+
#
|
3
|
+
# A slug must only contain lowercase a-z, digits 0-9 or dashes -
|
4
|
+
#
|
5
|
+
# A blank slug is considered valid (use presence validator to check for that)
|
6
|
+
#
|
7
|
+
# Examples
|
8
|
+
# validates :slug, slug_format: true # optional
|
9
|
+
# validates :slug, slug_format: true, presence: true # required
|
10
|
+
class SlugFormatValidator < ActiveModel::EachValidator
|
11
|
+
SLUG_REGEX = /\A[a-z0-9-]+\z/
|
12
|
+
|
13
|
+
def validate_each(record, attr_name, value)
|
14
|
+
return if value.blank?
|
15
|
+
record.errors.add(attr_name, :slug_format, options) unless value =~ SLUG_REGEX
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Validate fields to look like URLs
|
2
|
+
#
|
3
|
+
# A blank URL is considered valid (use presence validator to check for that)
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
# validates :url, url_format: true # optional
|
7
|
+
# validates :url, url_format: true, presence: true # required
|
8
|
+
class UrlFormatValidator < ActiveModel::EachValidator
|
9
|
+
def validate_each(record, attr_name, value)
|
10
|
+
return if value.blank?
|
11
|
+
|
12
|
+
valid = begin
|
13
|
+
URI.parse(value).kind_of?(URI::HTTP)
|
14
|
+
rescue URI::InvalidURIError
|
15
|
+
false
|
16
|
+
end
|
17
|
+
record.errors.add(attr_name, :url_format, options) unless valid
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'common_validators/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "common_validators"
|
8
|
+
spec.version = CommonValidators::VERSION
|
9
|
+
spec.authors = ["Gerry Shaw"]
|
10
|
+
spec.email = ["gshaw@reinvent.com"]
|
11
|
+
spec.summary = %q{Common validators for Rails applications}
|
12
|
+
spec.homepage = "https://github.com/gshaw/common_validators"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.required_ruby_version = ">= 1.9"
|
21
|
+
spec.add_dependency 'rails', '~> 3.2'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "test-unit"
|
26
|
+
spec.add_development_dependency "guard-test"
|
27
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
date_format: "is invalid"
|
5
|
+
email_format: "is invalid"
|
6
|
+
money_format: "is invalid"
|
7
|
+
money_format_whole_dollar: "must be whole dollar amount"
|
8
|
+
slug_format: "is invalid, only lowercase letters and numbers allowed"
|
9
|
+
url_format: "is invalid"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require File.expand_path("../../app/validators/date_format_validator", __FILE__)
|
3
|
+
|
4
|
+
class TestModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
attr_accessor :date
|
7
|
+
|
8
|
+
validates :date, date_format: true
|
9
|
+
end
|
10
|
+
|
11
|
+
class DateFormatValidatorTest < ActiveSupport::TestCase
|
12
|
+
def assert_valid_date(date)
|
13
|
+
t = TestModel.new
|
14
|
+
t.date = date
|
15
|
+
assert t.valid?, "Date `#{date}` should be valid"
|
16
|
+
end
|
17
|
+
|
18
|
+
def assert_invalid_date(date)
|
19
|
+
t = TestModel.new
|
20
|
+
t.date = date
|
21
|
+
assert !t.valid?, "Date `#{date}` should be invalid"
|
22
|
+
end
|
23
|
+
|
24
|
+
test "invalid date" do
|
25
|
+
assert_invalid_date "a"
|
26
|
+
assert_invalid_date "today"
|
27
|
+
assert_invalid_date "2013-12-32"
|
28
|
+
assert_invalid_date "2013-02-29"
|
29
|
+
end
|
30
|
+
|
31
|
+
test "valid date" do
|
32
|
+
assert_valid_date ""
|
33
|
+
assert_valid_date "2013-01-01"
|
34
|
+
assert_valid_date "2012-02-29"
|
35
|
+
assert_valid_date "2013-12-31"
|
36
|
+
assert_valid_date "Jan 1, 2013"
|
37
|
+
assert_valid_date "December 31st, 2013"
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require File.expand_path("../../app/validators/email_format_validator", __FILE__)
|
3
|
+
|
4
|
+
class TestModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
attr_accessor :email
|
7
|
+
|
8
|
+
validates :email, email_format: true
|
9
|
+
end
|
10
|
+
|
11
|
+
class EmailFormatValidatorTest < ActiveSupport::TestCase
|
12
|
+
def assert_valid_email(email)
|
13
|
+
t = TestModel.new
|
14
|
+
t.email = email
|
15
|
+
assert t.valid?, "Email `#{email}` should be valid"
|
16
|
+
end
|
17
|
+
|
18
|
+
def assert_invalid_email(email)
|
19
|
+
t = TestModel.new
|
20
|
+
t.email = email
|
21
|
+
assert !t.valid?, "Email `#{email}` should be invalid"
|
22
|
+
end
|
23
|
+
|
24
|
+
test "invalid email" do
|
25
|
+
assert_invalid_email "foo"
|
26
|
+
assert_invalid_email "bar@"
|
27
|
+
assert_invalid_email " alice@example.com"
|
28
|
+
assert_invalid_email "alice@example.com "
|
29
|
+
assert_invalid_email "a lice@example.com"
|
30
|
+
assert_invalid_email "alice@e xample.com"
|
31
|
+
assert_invalid_email "alice@example.c om"
|
32
|
+
assert_invalid_email "alice@example.c"
|
33
|
+
|
34
|
+
# Technically these are valid email addresses but they cause Postfix to crash
|
35
|
+
assert_invalid_email "a(lice@example.com"
|
36
|
+
assert_invalid_email "a)lice@example.com"
|
37
|
+
assert_invalid_email "a,lice@example.com"
|
38
|
+
assert_invalid_email "a:lice@example.com"
|
39
|
+
assert_invalid_email "a;lice@example.com"
|
40
|
+
assert_invalid_email "a<lice@example.com"
|
41
|
+
assert_invalid_email "a>lice@example.com"
|
42
|
+
assert_invalid_email "a@lice@example.com"
|
43
|
+
assert_invalid_email "a[lice@example.com"
|
44
|
+
assert_invalid_email "a\\lice@example.com"
|
45
|
+
assert_invalid_email "a]lice@example.com"
|
46
|
+
end
|
47
|
+
|
48
|
+
test "valid email" do
|
49
|
+
assert_valid_email ""
|
50
|
+
assert_valid_email "alice@example.com"
|
51
|
+
assert_valid_email "alice@example.com"
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require File.expand_path("../../app/validators/money_format_validator", __FILE__)
|
3
|
+
|
4
|
+
class TestModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
attr_accessor :amount, :whole_amount
|
7
|
+
|
8
|
+
validates :amount, money_format: true
|
9
|
+
validates :whole_amount, money_format: { exclude_cents: true }
|
10
|
+
end
|
11
|
+
|
12
|
+
class MoneyFormatValidatorTest < ActiveSupport::TestCase
|
13
|
+
def assert_valid_amount(amount, field = :amount)
|
14
|
+
t = TestModel.new
|
15
|
+
t.send(:"#{field}=", amount)
|
16
|
+
assert t.valid?, "Amount `#{amount}` should be valid"
|
17
|
+
end
|
18
|
+
|
19
|
+
def assert_invalid_amount(amount, field = :amount)
|
20
|
+
t = TestModel.new
|
21
|
+
t.send(:"#{field}=", amount)
|
22
|
+
assert !t.valid?, "Amount `#{amount}` should be invalid"
|
23
|
+
end
|
24
|
+
|
25
|
+
test "invalid amount" do
|
26
|
+
assert_invalid_amount "a"
|
27
|
+
assert_invalid_amount "10 20"
|
28
|
+
assert_invalid_amount "10.123"
|
29
|
+
assert_invalid_amount "1,000.12"
|
30
|
+
|
31
|
+
assert_invalid_amount "100.99", :whole_amount
|
32
|
+
assert_invalid_amount "100.1", :whole_amount
|
33
|
+
end
|
34
|
+
|
35
|
+
test "valid amount" do
|
36
|
+
assert_valid_amount ""
|
37
|
+
assert_valid_amount "1"
|
38
|
+
assert_valid_amount "1.00"
|
39
|
+
assert_valid_amount "1.99"
|
40
|
+
assert_valid_amount "+1.99"
|
41
|
+
assert_valid_amount "-1.99"
|
42
|
+
assert_valid_amount "100000.99"
|
43
|
+
assert_valid_amount "100000.99"
|
44
|
+
|
45
|
+
assert_valid_amount "100.00", :whole_amount
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require File.expand_path("../../app/validators/phone_number_format_validator", __FILE__)
|
3
|
+
|
4
|
+
class TestModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
attr_accessor :phone_number
|
7
|
+
|
8
|
+
validates :phone_number, phone_number_format: true
|
9
|
+
end
|
10
|
+
|
11
|
+
class PhoneNumberFormatValidatorTest < ActiveSupport::TestCase
|
12
|
+
def assert_valid_phone_number(phone_number)
|
13
|
+
t = TestModel.new
|
14
|
+
t.phone_number = phone_number
|
15
|
+
assert t.valid?, "Phone number `#{phone_number}` should be valid"
|
16
|
+
end
|
17
|
+
|
18
|
+
def assert_invalid_phone_number(phone_number)
|
19
|
+
t = TestModel.new
|
20
|
+
t.phone_number = phone_number
|
21
|
+
assert !t.valid?, "Phone number `#{phone_number}` should be invalid"
|
22
|
+
end
|
23
|
+
|
24
|
+
test "invalid phone_number" do
|
25
|
+
assert_invalid_phone_number "a"
|
26
|
+
assert_invalid_phone_number "555-1212"
|
27
|
+
assert_invalid_phone_number "123456789"
|
28
|
+
end
|
29
|
+
|
30
|
+
test "valid phone_number" do
|
31
|
+
assert_valid_phone_number ""
|
32
|
+
assert_valid_phone_number "604-555-1212"
|
33
|
+
assert_valid_phone_number "+1 604-555-1212"
|
34
|
+
assert_valid_phone_number "(604) 555-1212 x1234"
|
35
|
+
assert_valid_phone_number "604-555-1212 ext. 1234"
|
36
|
+
assert_valid_phone_number "6045551212"
|
37
|
+
assert_valid_phone_number "1-800-555-1212"
|
38
|
+
assert_valid_phone_number "+1 234 567 8923"
|
39
|
+
assert_valid_phone_number "+ 1.234.567. 89 23"
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require File.expand_path("../../app/validators/slug_format_validator", __FILE__)
|
3
|
+
|
4
|
+
class TestModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
attr_accessor :slug
|
7
|
+
|
8
|
+
validates :slug, slug_format: true
|
9
|
+
end
|
10
|
+
|
11
|
+
class SlugFormatValidatorTest < ActiveSupport::TestCase
|
12
|
+
def assert_valid_slug(slug)
|
13
|
+
t = TestModel.new
|
14
|
+
t.slug = slug
|
15
|
+
assert t.valid?, "Slug `#{slug}` should be valid"
|
16
|
+
end
|
17
|
+
|
18
|
+
def assert_invalid_slug(slug)
|
19
|
+
t = TestModel.new
|
20
|
+
t.slug = slug
|
21
|
+
assert !t.valid?, "Slug `#{slug}` should be invalid"
|
22
|
+
end
|
23
|
+
|
24
|
+
test "invalid slug" do
|
25
|
+
assert_invalid_slug "A"
|
26
|
+
assert_invalid_slug "a b"
|
27
|
+
assert_invalid_slug "a$"
|
28
|
+
assert_invalid_slug "a^"
|
29
|
+
assert_invalid_slug "a^"
|
30
|
+
end
|
31
|
+
|
32
|
+
test "valid slug" do
|
33
|
+
assert_valid_slug ""
|
34
|
+
assert_valid_slug "a"
|
35
|
+
assert_valid_slug "1"
|
36
|
+
assert_valid_slug "a1"
|
37
|
+
assert_valid_slug "a-1"
|
38
|
+
assert_valid_slug "a--1"
|
39
|
+
assert_valid_slug "-ab"
|
40
|
+
assert_valid_slug "ab-"
|
41
|
+
end
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rails'
|
6
|
+
|
7
|
+
class FakeApplication < Rails::Application; end
|
8
|
+
|
9
|
+
Rails.application = FakeApplication
|
10
|
+
Rails.configuration.action_controller = ActiveSupport::OrderedOptions.new
|
11
|
+
|
12
|
+
require 'common_validators'
|
13
|
+
|
14
|
+
# Load support files
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require File.expand_path("../../app/validators/url_format_validator", __FILE__)
|
3
|
+
|
4
|
+
class TestModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
attr_accessor :url
|
7
|
+
|
8
|
+
validates :url, url_format: true
|
9
|
+
end
|
10
|
+
|
11
|
+
class UrlFormatValidatorTest < ActiveSupport::TestCase
|
12
|
+
def assert_valid_url(url)
|
13
|
+
t = TestModel.new
|
14
|
+
t.url = url
|
15
|
+
assert t.valid?, "Url `#{url}` should be valid"
|
16
|
+
end
|
17
|
+
|
18
|
+
def assert_invalid_url(url)
|
19
|
+
t = TestModel.new
|
20
|
+
t.url = url
|
21
|
+
assert !t.valid?, "Url `#{url}` should be invalid"
|
22
|
+
end
|
23
|
+
|
24
|
+
test "invalid url" do
|
25
|
+
assert_invalid_url "h ttp://example.com"
|
26
|
+
assert_invalid_url "http://examp\\"
|
27
|
+
assert_invalid_url "bad://example.com"
|
28
|
+
assert_invalid_url "://example.com"
|
29
|
+
assert_invalid_url "//example.com"
|
30
|
+
end
|
31
|
+
|
32
|
+
test "valid url" do
|
33
|
+
assert_valid_url ""
|
34
|
+
assert_valid_url "http://example.com"
|
35
|
+
assert_valid_url "HTTP://example.com"
|
36
|
+
assert_valid_url "https://example.com"
|
37
|
+
assert_valid_url "http://example.com/foo/bar"
|
38
|
+
assert_valid_url "http://example.com/foo/bar?a=b&c=d"
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: common_validators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gerry Shaw
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: test-unit
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-test
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description:
|
95
|
+
email:
|
96
|
+
- gshaw@reinvent.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .travis.yml
|
103
|
+
- Gemfile
|
104
|
+
- Guardfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- app/validators/date_format_validator.rb
|
109
|
+
- app/validators/email_format_validator.rb
|
110
|
+
- app/validators/money_format_validator.rb
|
111
|
+
- app/validators/phone_number_format_validator.rb
|
112
|
+
- app/validators/slug_format_validator.rb
|
113
|
+
- app/validators/url_format_validator.rb
|
114
|
+
- common_validators.gemspec
|
115
|
+
- config/locales/en.yml
|
116
|
+
- lib/common_validators.rb
|
117
|
+
- lib/common_validators/engine.rb
|
118
|
+
- lib/common_validators/railtie.rb
|
119
|
+
- lib/common_validators/version.rb
|
120
|
+
- test/date_format_validator_test.rb
|
121
|
+
- test/email_format_validator_test.rb
|
122
|
+
- test/money_format_validator_test.rb
|
123
|
+
- test/phone_number_format_validator_test.rb
|
124
|
+
- test/slug_format_validator_test.rb
|
125
|
+
- test/test_helper.rb
|
126
|
+
- test/url_format_validator_test.rb
|
127
|
+
homepage: https://github.com/gshaw/common_validators
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '1.9'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
hash: -2601123242860977824
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 1.8.23
|
152
|
+
signing_key:
|
153
|
+
specification_version: 3
|
154
|
+
summary: Common validators for Rails applications
|
155
|
+
test_files:
|
156
|
+
- test/date_format_validator_test.rb
|
157
|
+
- test/email_format_validator_test.rb
|
158
|
+
- test/money_format_validator_test.rb
|
159
|
+
- test/phone_number_format_validator_test.rb
|
160
|
+
- test/slug_format_validator_test.rb
|
161
|
+
- test/test_helper.rb
|
162
|
+
- test/url_format_validator_test.rb
|