rails_validation 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a389d0d19b46ee2decfcc88b2754df9cab1160fb
4
- data.tar.gz: 6efab23503029cbff7c90462084f82c5724ed476
3
+ metadata.gz: 0a6430d0457190675c4c8b225d4d27d96f3dd968
4
+ data.tar.gz: bf919202e41dd1af795a2fc11af48951ae7f89da
5
5
  SHA512:
6
- metadata.gz: 7b5bae3f292468b31726545577560ceb7fe6eb26a7a8446fb31906d414564b435dc5c85afce695ea42892d3bae631abcae2c98e3c5636e32a1c299f3401e954e
7
- data.tar.gz: 630efab572e2f88be0215a50fef010bddbdc4749782be8efbe1ceb0953ea7f4a021d1fa10e302dba41f431b1d479b90f51d326b34e0e4ff027af30a104ace1fd
6
+ metadata.gz: f533787c7776c1d5eca670be4b9b82089b30813086b3d904cb839fca35f50675ab8d52ebafc1b3abc271b7d7109c132e724b35ca13ef71b730e3be121a111038
7
+ data.tar.gz: 5b079f2626f910d8b119caf1a78f534d11125fa9512ad671170d26d06f32efb0670b13ec62e75ec612e68988661f919ccdd027ba1bf9c3936ae2a63a441970c4
@@ -0,0 +1,10 @@
1
+ en:
2
+ rails_validation:
3
+ email:
4
+ invalid: 'not a valid email address'
5
+ qq:
6
+ invalid: 'not a valid qq number'
7
+ phone:
8
+ invalid: 'not a valid phone number'
9
+ url_name:
10
+ invalid: 'url_name invalid'
@@ -0,0 +1,10 @@
1
+ zh-CN:
2
+ rails_validations:
3
+ email:
4
+ invalid: '邮箱格式不正确'
5
+ qq:
6
+ invalid: 'qq格式不正确'
7
+ phone:
8
+ invalid: '手机号码格式不正确'
9
+ url_name:
10
+ invalid: 'url_name格式不正确'
@@ -1,4 +1,7 @@
1
- require 'validators/email_validator'
1
+ require 'validations/email_validator'
2
+ require 'validations/phone_validator'
3
+ require 'validations/qq_validator'
4
+ require 'validations/url_name_validator'
2
5
  I18n.load_path += Dir["#{File.dirname(__FILE__)}/../config/locales/*.yml"]
3
6
  module RailsValidations
4
7
  end
@@ -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
@@ -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
+
metadata CHANGED
@@ -1,22 +1,86 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clark King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
12
- dependencies: []
13
- description: A Rails model validation
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
14
70
  email: wskongdesheng@gmail.com
15
71
  executables: []
16
72
  extensions: []
17
73
  extra_rdoc_files: []
18
74
  files:
75
+ - config/locales/en.yml
76
+ - config/locales/zh-CN.yml
19
77
  - lib/rails_validation.rb
78
+ - lib/validations/email_validator.rb
79
+ - lib/validations/phone_validator.rb
80
+ - lib/validations/qq_validator.rb
81
+ - lib/validations/url_name_validator.rb
82
+ - spec/spec_helper.rb
83
+ - spec/validations/email_spec.rb
20
84
  homepage: https://github.com/wskongdesheng/rails_validation
21
85
  licenses:
22
86
  - MIT
@@ -40,5 +104,7 @@ rubyforge_project:
40
104
  rubygems_version: 2.2.2
41
105
  signing_key:
42
106
  specification_version: 4
43
- summary: Rails validationa
44
- test_files: []
107
+ summary: Rails validation
108
+ test_files:
109
+ - spec/validations/email_spec.rb
110
+ - spec/spec_helper.rb