X_validator 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.markdown +44 -0
- data/config/locales/en.yml +10 -0
- data/config/locales/zh-CN.yml +10 -0
- data/lib/rails_validation.rb +7 -0
- data/lib/validations/email_validator.rb +22 -0
- data/lib/validations/phone_validator.rb +16 -0
- data/lib/validations/qq_validator.rb +16 -0
- data/lib/validations/url_name_validator.rb +16 -0
- data/spec/spec_helper.rb +91 -0
- data/spec/validations/email_spec.rb +48 -0
- data/spec/validations/phone_spec.rb +39 -0
- data/spec/validations/qq_spec.rb +40 -0
- data/spec/validations/url_name_spec.rb +40 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37ccb125e5ad3c69860b5f7835fa7b3f120056fe
|
4
|
+
data.tar.gz: 91d51a3684dd67d700113fa9f29059f5c7c2ef59
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8a7f9b3072f6974f4b542305a20e502037645e12119e870a3ba4bdb522ac7ba2527e0074336e70480409b6404c161352d607b75bc5b2edf83a608f8af878fe97
|
7
|
+
data.tar.gz: 27619240ab222b5b90accb279f7fb171b1274c154cfa7ffc02baa2365a094a9e44af1a67734c0221e26533d08d7adfe91620d436fb541a2d2f2e4571c490448e
|
data/README.markdown
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
A few basic extra validations for Ruby on Rails.
|
3
|
+
|
4
|
+
|
5
|
+
I18N
|
6
|
+
====
|
7
|
+
The gem includes English and Chinese error messages. If you want to translate it
|
8
|
+
to another language then copy [`config/locales/en.yml`][tr] to your project.
|
9
|
+
Please send us your translations so we can add them!
|
10
|
+
|
11
|
+
|
12
|
+
Available validations
|
13
|
+
=====================
|
14
|
+
|
15
|
+
email
|
16
|
+
-----
|
17
|
+
Validate if a string looks like an email address. This should work with unicode
|
18
|
+
addresses ([RFC 6531][rfc6531], [IDN][idn]).
|
19
|
+
|
20
|
+
Accepts `martin@lico.nl`, but rejects `martinlico.nl` or `martin@lico`:
|
21
|
+
|
22
|
+
validates :email_column, email: true
|
23
|
+
validates_email_of :email_column
|
24
|
+
|
25
|
+
phone
|
26
|
+
-----
|
27
|
+
Check if this is a valid phone number. This should work with most, if not all.
|
28
|
+
|
29
|
+
validates :phone_number_column, phone: true
|
30
|
+
validates_phone_of :phone_number_column
|
31
|
+
|
32
|
+
qq
|
33
|
+
-----
|
34
|
+
Check if this is a valid qq number.
|
35
|
+
|
36
|
+
validates :qq_column, qq: true
|
37
|
+
validates_qq_of :qq_column
|
38
|
+
|
39
|
+
url_name
|
40
|
+
-----
|
41
|
+
Check if this is a valid url_name.
|
42
|
+
|
43
|
+
validates :url_name_column, url_name: true
|
44
|
+
validates_url_name_of :url_name_column
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Validate email, works with UTF-8
|
2
|
+
# The validation is intentionally simply, you can never be sure an email is
|
3
|
+
# valid anyway (a user can mistype gmail as gmaill, for example).
|
4
|
+
#
|
5
|
+
# - User part: one or more alphabet character or number or _ or -.
|
6
|
+
# - Domain part: one or more alphabet character or number or _ or -.
|
7
|
+
# - tld part: 2 or more alphabet characters, numbers, or -
|
8
|
+
class EmailValidator < ActiveModel::EachValidator
|
9
|
+
REGEXP = /^([a-zA-Z0-9_-])+@(([a-zA-Z0-9_-])+)((\.([a-zA-Z0-9_-])+)*)((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validation.email.invalid')) unless value =~ REGEXP
|
12
|
+
end
|
13
|
+
end
|
14
|
+
module ActiveModel
|
15
|
+
module Validations
|
16
|
+
module HelperMethods
|
17
|
+
def validates_email_of(*attr_names)
|
18
|
+
validates_with EmailValidator, _merge_attributes(attr_names)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# basic phone format validator
|
2
|
+
class PhoneValidator < ActiveModel::EachValidator
|
3
|
+
REGEXP = /^1\d{10}$/
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validation.phone.invalid')) unless value =~ REGEXP
|
6
|
+
end
|
7
|
+
end
|
8
|
+
module ActiveModel
|
9
|
+
module Validations
|
10
|
+
module HelperMethods
|
11
|
+
def validates_phone_of(*attr_names)
|
12
|
+
validates_with PhoneValidator, _merge_attributes(attr_names)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# qq format validation, works with qq format
|
2
|
+
class QqValidator < ActiveModel::EachValidator
|
3
|
+
REGEXP = /^[1-9]*[1-9][0-9]*$/
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validation.qq.invalid')) unless value =~ REGEXP
|
6
|
+
end
|
7
|
+
end
|
8
|
+
module ActiveModel
|
9
|
+
module Validations
|
10
|
+
module HelperMethods
|
11
|
+
def validates_qq_of(*attr_names)
|
12
|
+
validates_with QqValidator, _merge_attributes(attr_names)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#zhe800 url_name validator
|
2
|
+
class UrlNameValidator < ActiveModel::EachValidator
|
3
|
+
REGEXP = /^\w+_\d+$/
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validation.url_name.invalid')) unless value =~ REGEXP
|
6
|
+
end
|
7
|
+
end
|
8
|
+
module ActiveModel
|
9
|
+
module Validations
|
10
|
+
module HelperMethods
|
11
|
+
def validates_url_name_of(*attr_names)
|
12
|
+
validates_with UrlNameValidator, _merge_attributes(attr_names)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_dispatch'
|
7
|
+
require 'action_pack'
|
8
|
+
require 'action_view'
|
9
|
+
require 'active_model'
|
10
|
+
require 'active_record'
|
11
|
+
require 'active_support'
|
12
|
+
require 'active_support/all'
|
13
|
+
|
14
|
+
|
15
|
+
# Include our validators
|
16
|
+
Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].sort.each { |f| require f }
|
17
|
+
|
18
|
+
# Make sure I18N works
|
19
|
+
I18n.enforce_available_locales = false if I18n.respond_to?(:enforce_available_locales)
|
20
|
+
I18n.load_path += Dir["#{File.dirname(__FILE__)}/../config/locales/*.yml"]
|
21
|
+
|
22
|
+
|
23
|
+
module ValidationsSpecHelper
|
24
|
+
include ActiveSupport
|
25
|
+
include ActionPack
|
26
|
+
include ActionView::Context if defined?(ActionView::Context)
|
27
|
+
include ActionController::RecordIdentifier if defined?(ActionController::RecordIdentifier)
|
28
|
+
include ActionView::RecordIdentifier if defined?(ActionView::RecordIdentifier)
|
29
|
+
|
30
|
+
|
31
|
+
class Record
|
32
|
+
extend ActiveModel::Naming
|
33
|
+
include ActiveModel::Conversion
|
34
|
+
include ActiveModel::Validations
|
35
|
+
|
36
|
+
def initialize attr={}
|
37
|
+
attr.each { |k, v| send("#{k}=", v) }
|
38
|
+
super()
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def v; @value end
|
43
|
+
def v= v; @value = v end
|
44
|
+
def v2; @value2 end
|
45
|
+
def v2= v; @value2 = v end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def model v, v2=nil
|
50
|
+
described_class.new v: v, v2: v2
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def model_errors v
|
55
|
+
m = model v
|
56
|
+
m.valid?
|
57
|
+
return m.errors[:v]
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def with_validation validate_string
|
62
|
+
described_class.clear_validators!
|
63
|
+
described_class.class_eval "validates :v, #{validate_string}"
|
64
|
+
|
65
|
+
yield
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
RSpec::Matchers.define :be_valid do
|
71
|
+
match { |record | record.valid? == true && record.errors.blank? }
|
72
|
+
end
|
73
|
+
|
74
|
+
RSpec::Matchers.define :be_invalid do |i18n_key, i18n_params={}|
|
75
|
+
match do |record|
|
76
|
+
record.valid? == false &&
|
77
|
+
record.errors.first.present? &&
|
78
|
+
record.errors.first[1] == I18n.t("rails_validation.#{i18n_key}", **i18n_params)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
shared_examples :validation do |validation|
|
84
|
+
it 'accepts a custom error message' do
|
85
|
+
with_validation "#{validation}: { message: 'foobar' }" do
|
86
|
+
m = described_class.new v: 'not even remotely valid'
|
87
|
+
expect(m.valid?).to eq(false)
|
88
|
+
expect(m.errors[:v].first).to eq('foobar')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class Email < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Email do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
it_behaves_like :validation, 'email'
|
13
|
+
|
14
|
+
it 'gives an error on invalid emails' do
|
15
|
+
with_validation 'email: true' do
|
16
|
+
%w{
|
17
|
+
not\ valid
|
18
|
+
invalid@example.
|
19
|
+
invalid@@example.com
|
20
|
+
invalid@example..com
|
21
|
+
@example.com
|
22
|
+
invalid@.com
|
23
|
+
invalid@example.c
|
24
|
+
in\ valid@example.com
|
25
|
+
invalid@exam\ ple.com
|
26
|
+
}.each do |v|
|
27
|
+
expect(model(v)).to be_invalid('email.invalid')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
it 'works for valid emails' do
|
34
|
+
with_validation 'email: true' do
|
35
|
+
%w{
|
36
|
+
valid@example.com
|
37
|
+
valid112@example.com
|
38
|
+
valid-example@example.com
|
39
|
+
valid_address@example.com
|
40
|
+
valid@more.than.one.example.com
|
41
|
+
valid@example.anewtld.com
|
42
|
+
}.each do |v|
|
43
|
+
expect(model(v)).to be_valid
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class Phone < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Phone do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
it_behaves_like :validation, 'phone'
|
13
|
+
|
14
|
+
it 'gives an error on invalid phones' do
|
15
|
+
with_validation 'phone: true' do
|
16
|
+
%w{
|
17
|
+
1212
|
18
|
+
1221223333333333
|
19
|
+
23508080808
|
20
|
+
12dfadf1dds
|
21
|
+
\122fddfafds
|
22
|
+
}.each do |v|
|
23
|
+
expect(model(v)).to be_invalid('phone.invalid')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
it 'works for valid phones' do
|
30
|
+
with_validation 'phone: true' do
|
31
|
+
%w{
|
32
|
+
13333333333
|
33
|
+
}.each do |v|
|
34
|
+
expect(model(v)).to be_valid
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class Qq < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Qq do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
it_behaves_like :validation, 'qq'
|
13
|
+
|
14
|
+
it 'gives an error on invalid qq' do
|
15
|
+
with_validation 'qq: true' do
|
16
|
+
%w{
|
17
|
+
01212331
|
18
|
+
fdafdad
|
19
|
+
1sadss
|
20
|
+
-12fdsf
|
21
|
+
}.each do |v|
|
22
|
+
expect(model(v)).to be_invalid('qq.invalid')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
it 'works for valid qq' do
|
29
|
+
with_validation 'qq: true' do
|
30
|
+
%w{
|
31
|
+
22222
|
32
|
+
233233
|
33
|
+
3433333
|
34
|
+
}.each do |v|
|
35
|
+
expect(model(v)).to be_valid
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class UrlName < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::UrlName do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
it_behaves_like :validation, 'url_name'
|
13
|
+
|
14
|
+
it 'gives an error on invalid url_name' do
|
15
|
+
with_validation 'url_name: true' do
|
16
|
+
%w{
|
17
|
+
1212
|
18
|
+
xxssd
|
19
|
+
12ds
|
20
|
+
wed1212
|
21
|
+
dsd-12
|
22
|
+
}.each do |v|
|
23
|
+
expect(model(v)).to be_invalid('url_name.invalid')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
it 'works for valid url_name' do
|
30
|
+
with_validation 'url_name: true' do
|
31
|
+
%w{
|
32
|
+
ads_121
|
33
|
+
ad_12
|
34
|
+
}.each do |v|
|
35
|
+
expect(model(v)).to be_valid
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: X_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Clark King
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: iban-tools
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: A Rails model validation, my first gem, just a practice work
|
70
|
+
email: wskongdesheng@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- README.markdown
|
76
|
+
- config/locales/en.yml
|
77
|
+
- config/locales/zh-CN.yml
|
78
|
+
- lib/rails_validation.rb
|
79
|
+
- lib/validations/email_validator.rb
|
80
|
+
- lib/validations/phone_validator.rb
|
81
|
+
- lib/validations/qq_validator.rb
|
82
|
+
- lib/validations/url_name_validator.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/validations/email_spec.rb
|
85
|
+
- spec/validations/phone_spec.rb
|
86
|
+
- spec/validations/qq_spec.rb
|
87
|
+
- spec/validations/url_name_spec.rb
|
88
|
+
homepage: https://github.com/wskongdesheng/rails_validation
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '2.0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Rails validation
|
112
|
+
test_files:
|
113
|
+
- spec/validations/url_name_spec.rb
|
114
|
+
- spec/validations/phone_spec.rb
|
115
|
+
- spec/validations/qq_spec.rb
|
116
|
+
- spec/validations/email_spec.rb
|
117
|
+
- spec/spec_helper.rb
|