validates 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in validates.gemspec
4
4
  gemspec
5
+
6
+ gem 'minitest'
7
+ gem 'turn'
8
+ gem 'rake'
data/README.md CHANGED
@@ -1,41 +1,63 @@
1
1
  # Validates
2
2
 
3
- ## Summary
3
+ [![Build Status](https://travis-ci.org/kaize/validates.png)](https://travis-ci.org/kaize/validates)
4
4
 
5
5
  Simple format validators for Rails 3
6
6
 
7
- ## Installing
7
+ ## Installation
8
8
 
9
- Add this to your `Gemfile`:
9
+ Add this line to your application's Gemfile:
10
10
 
11
- gem "validates"
11
+ gem 'validates'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install 'validates'
12
20
 
13
21
  ## Usage
14
22
 
15
- model User < ActiveRecord::Base
23
+ Availables validators: AssociationLength, Email, Existence, Slug, Url, Money
24
+
25
+ class User < ActiveRecord::Base
16
26
  validates :email, :email => true
17
27
  validates :site, :url => true, :allow_blank => true
18
28
  validates :inn, :inn => true
19
29
  end
20
30
 
21
- model Page < ActiveRecord::Base
22
- validates :slug, :slug => true
23
- end
31
+ class Company < ActiveRecord::Base
32
+ # note AssociationLengthValidator is inherited from ActiveModel::Validations::LengthValidator
33
+ # http://api.rubyonrails.org/classes/ActiveModel/Validations/LengthValidator.html
34
+ # so you can easily use standard options like :is, :minimum, :maximum, etc.
24
35
 
36
+ validates :employees,
37
+ :association_length => {
38
+ :minimum => 1,
39
+ :select => ->(employee) { employee.name.in? ["Mike", "John"] }
40
+ }
25
41
 
26
- ## Availables validators
42
+ validates :employees, :association_length => { :minimum => 1, :select => :employees_filter }
27
43
 
28
- Email, Slug, Url, Money
44
+ def employees_filter(employees)
45
+ employees.select { |employee| employee.name.in? ["Mike", "John"] }
46
+ end
47
+ end
29
48
 
30
- ## Relise notes
49
+ class Page < ActiveRecord::Base
50
+ validates :slug, :slug => true
51
+ end
31
52
 
32
- 0.0.3 - Added Money validator
53
+ ## Contributing
33
54
 
34
- 0.0.4 - Add inn validator.
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
35
60
 
36
- ## Similar
61
+ run tests:
37
62
 
38
- * http://github.com/balexand/email_validator
39
- * http://github.com/cesario/activevalidators
40
- * https://rubygems.org/gems/date_validator
41
- * and more...
63
+ turn -Itest test/lib
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/lib/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ class AssociationLengthValidator < ActiveModel::Validations::LengthValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ value = value.reject(&:marked_for_destruction?)
5
+ value = select_items(record, value, options[:select]) if options[:select]
6
+
7
+ super(record, attribute, value)
8
+ end
9
+
10
+ private
11
+
12
+ def select_items(record, items, select_expr)
13
+ case select_expr
14
+ when Symbol, String
15
+ if record.respond_to?(select_expr)
16
+ record.send(select_expr, items)
17
+ else
18
+ raise ArgumentError, "Missing instance method #{record.class.name}##{select_expr}"
19
+ end
20
+ when Proc
21
+ items.select(&select_expr)
22
+ else
23
+ items
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,76 @@
1
+ class EmailValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless self.class.valid?(value)
5
+ record.errors.add(attribute, :email, options.merge(:value => value))
6
+ end
7
+ end
8
+
9
+ class << self
10
+ MAX_EMAIL_LENGTH = 254
11
+
12
+ # Maximum domain length
13
+ MAX_DOMAIN_LENGTH = 255
14
+ MAX_DOMAIN_PART_LENGTH = 64
15
+
16
+ # Maximum user length
17
+ MAX_USER_LENGTH = 64
18
+
19
+ # User allowed chars
20
+ LOCAL_ALLOWED_CHARS = '(?:[a-z0-9A-Z\!\#\$\%\&\'\*\-\/\=\?\+\-\^\_\`\{\|\}\~]|(?<!(?:^|\.))\.(?!$))'
21
+
22
+ def valid?(value)
23
+ email_format_valid?(value)
24
+ end
25
+
26
+ private
27
+
28
+ # Validate email format
29
+ # @params [String] email
30
+ # @return [true,false,nil]
31
+ def email_format_valid?(email)
32
+
33
+ domain, local= email.reverse.split('@', 2)
34
+
35
+ return if email.length > MAX_EMAIL_LENGTH
36
+
37
+ return if domain.nil? or local.nil? or domain.empty? or local.empty?
38
+
39
+ domain.reverse!
40
+ local.reverse!
41
+
42
+ return if domain.length > MAX_DOMAIN_LENGTH or local.length > MAX_USER_LENGTH
43
+
44
+ return unless email_domain_syntax_valid?(domain) and email_local_syntax_valid?(local)
45
+
46
+ true
47
+ end
48
+
49
+ # Validate emails domain syntax
50
+ # @params [String] domain
51
+ # @return [true, false]
52
+ def email_domain_syntax_valid?(domain)
53
+ parts = domain.downcase.gsub(/(?:^\[(.+)\]$)/,'\1').split('.', -1)
54
+
55
+ return false unless parts.all? { |part| part =~ /^(?!\-)[[:alnum:]\-]+(?<!\-)$/ && part.length < MAX_DOMAIN_PART_LENGTH}
56
+
57
+ return true if parts.length == 4 and parts.all? { |part| part =~ /\A[0-9]+\Z/ and part.to_i.between?(0, 255) }
58
+
59
+ return false if parts[-1] =~ /^\d+$/
60
+
61
+ true
62
+ end
63
+
64
+ # Validate emails locale syntax
65
+ def email_local_syntax_valid?(local)
66
+ return if not email_local_syntax_regexp =~ local
67
+ true
68
+ end
69
+
70
+ # Return emails local part syntax regexp
71
+ # @return [Regexp]
72
+ def email_local_syntax_regexp
73
+ Regexp.new("^(?:\"(?:[\\\\]?#{LOCAL_ALLOWED_CHARS}|\\\\[\"\\s\\\\]|[@])*\"|#{LOCAL_ALLOWED_CHARS}*)$")
74
+ end
75
+ end
76
+ end
@@ -3,5 +3,5 @@ class UrlValidator < ActiveModel::EachValidator
3
3
  unless value =~ URI::regexp(%w(http https))
4
4
  record.errors.add(attribute, :url, options.merge(:value => value))
5
5
  end
6
- end
6
+ end
7
7
  end
data/lib/validates.rb CHANGED
@@ -1,7 +1,8 @@
1
- require "validates/version"
2
- require "validates/email_regexp"
3
- require "validates/email_validator"
4
- require "validates/slug_validator"
5
- require "validates/url_validator"
6
- require "validates/money_validator"
7
- require "validates/inn_validator"
1
+ [:email, :existence, :inn, :money, :slug, :url].each do |name|
2
+ autoload :"#{name.capitalize}Validator", "#{name}_validator"
3
+ end
4
+
5
+ require 'active_model' # why i need do it?
6
+
7
+ module Validates
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Validates
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,105 @@
1
+ require 'test_helper'
2
+
3
+ class EmailValidatorTest < Test::Unit::TestCase
4
+ def test_valid
5
+ valid_emails = [
6
+ 'user@example.com',
7
+ 'user.mail@example.com',
8
+ 'user@example-domain.com',
9
+ 'user@example-d-a.com',
10
+ 'user@example.domain.com',
11
+ 'user_mail@example.com',
12
+ 'user+mail@example.com',
13
+ 'user123@example.com',
14
+ 'user@example123.com',
15
+ 'user@example.domain',
16
+ 'UseR@example.com',
17
+ 'user-mail@example.com',
18
+ 'user@example',
19
+ 'u@example.com',
20
+ '!#$%&`*+/=?^`{|}~@example.com',
21
+ '123@example.com',
22
+ 'user@123.com',
23
+ 'user@255.255.255.255',
24
+ 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@iana.org',
25
+ 'test@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.com',
26
+ 'user@dom--ain.com',
27
+ 'user@example.dom-ain',
28
+ 'a@a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v',
29
+ 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi',
30
+ '"user"@domain.com',
31
+ '""@domain.com',
32
+ '"\a"@domain.com',
33
+ '"\\""@domain.com',
34
+ '"\\\\"@domain.com',
35
+ '1234567890123456789012345678901234567890123456789012345678901234@iana.org',
36
+ '"user\ mail"@domain.com',
37
+ '"Joe.\\Blow"@domain.com',
38
+ '"user@maik"@domain.com',
39
+ 'user/mail=test@domain.com',
40
+ '$U12345@domain.com',
41
+ '!def!xyz%abc@iana.org',
42
+ '_user@domain.com',
43
+ '~@iana.org',
44
+ 'user@[127.0.0.1]',
45
+ 'foobar@192.168.0.1',
46
+ 'test@xn--example.com'
47
+ ]
48
+
49
+ valid_emails.each do |email|
50
+ assert EmailValidator.valid?(email), "#{email} not valid"
51
+ end
52
+ end
53
+
54
+ def test_invalid
55
+ invalid_emails = [
56
+ '',
57
+ 'example_email',
58
+ '@',
59
+ 'user@',
60
+ '@domain',
61
+ 'use"r@example.com',
62
+ 'user@example_domain.com',
63
+ 'user@domain.12',
64
+ 'user@domain..com',
65
+ 'user@.domain.com',
66
+ 'user@-domain.com',
67
+ 'user@domain-.com',
68
+ 'user@domain.-a.com',
69
+ 'user mail@example.com',
70
+ 'us[er@example.com',
71
+ 'us\er@example.com',
72
+ '.user@domain.com',
73
+ 'user.@domain.com',
74
+ 'user..mail@domain.com',
75
+ 'user_at-domain.com',
76
+ 'test\@example@domain.com',
77
+ 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklmn@iana.org',
78
+ 'test@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm.com',
79
+ 'user@.domain.com',
80
+ 'user@domain.com.',
81
+ 'user@domain..com',
82
+ '123456789012345678901234567890123456789012345678901234567890@12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345.iana.org',
83
+ 'a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.hij',
84
+ 'a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.hijk',
85
+ '"""@domain.com',
86
+ '"\"@domain.com',
87
+ 'user"@domain.com',
88
+ '"user@domain.com',
89
+ '"user"test@domain.com',
90
+ 'user"test"@domain.com',
91
+ '"user""test"@domain.com',
92
+ '"user"."test"@domain.com',
93
+ 'first.last@sub.do,com',
94
+ 'user\@domain.com',
95
+ 'x@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456',
96
+ '()[]\;:,><@iana.org',
97
+ 'first(middle)last@iana.org'
98
+ ]
99
+
100
+ invalid_emails.each do |email|
101
+ assert !EmailValidator.valid?(email), "email #{email} valid"
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
data/validates.gemspec CHANGED
@@ -5,8 +5,8 @@ require "validates/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "validates"
7
7
  s.version = Validates::VERSION
8
- s.authors = ["Mikhail Stolbov","Anton Taraev","Konstantin Kosmatov"]
9
- s.email = ["mstolbov@gmail.com","anti191@gmail.com","key@kosmatov.su"]
8
+ s.authors = ["Mikhail Stolbov","Anton Taraev","Konstantin Kosmatov","Andrey Subbota"]
9
+ s.email = ["mstolbov@gmail.com","anti191@gmail.com","key@kosmatov.su","subbota@gmail.com"]
10
10
  s.homepage = "https://github.com/kaize/validates"
11
11
  s.summary = "Collection of simple validators for Rails 3"
12
12
  s.description = "Email, Slug, Url, Money, INN validators for Rails 3"
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mikhail Stolbov
9
9
  - Anton Taraev
10
10
  - Konstantin Kosmatov
11
+ - Andrey Subbota
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2012-09-06 00:00:00.000000000 Z
15
+ date: 2012-10-23 00:00:00.000000000 Z
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
17
18
  name: activemodel
@@ -34,6 +35,7 @@ email:
34
35
  - mstolbov@gmail.com
35
36
  - anti191@gmail.com
36
37
  - key@kosmatov.su
38
+ - subbota@gmail.com
37
39
  executables: []
38
40
  extensions: []
39
41
  extra_rdoc_files: []
@@ -42,15 +44,16 @@ files:
42
44
  - Gemfile
43
45
  - README.md
44
46
  - Rakefile
47
+ - lib/association_length_validator.rb
48
+ - lib/email_validator.rb
49
+ - lib/inn_validator.rb
50
+ - lib/money_validator.rb
51
+ - lib/slug_validator.rb
52
+ - lib/url_validator.rb
45
53
  - lib/validates.rb
46
- - lib/validates/email_regexp.rb
47
- - lib/validates/email_validator.rb
48
- - lib/validates/inn_validator.rb
49
- - lib/validates/money_validator.rb
50
- - lib/validates/slug_validator.rb
51
- - lib/validates/url_validator.rb
52
54
  - lib/validates/version.rb
53
- - test/email_regexp_test.rb
55
+ - test/lib/email_validator_test.rb
56
+ - test/test_helper.rb
54
57
  - validates.gemspec
55
58
  homepage: https://github.com/kaize/validates
56
59
  licenses: []
@@ -1,7 +0,0 @@
1
- module EmailRegexp
2
- def regexp_compare(value)
3
- value =~ /\A[-.\w+]+@([a-z\d][-a-z\d]+\.?[a-z\d]+)+\.[a-z]{2,}\z/i
4
- end
5
-
6
- end
7
-
@@ -1,9 +0,0 @@
1
- class EmailValidator < ActiveModel::EachValidator
2
- include EmailRegexp
3
-
4
- def validate_each(record, attribute, value)
5
- unless regexp_compare(value)
6
- record.errors.add(attribute, :email, options.merge(:value => value))
7
- end
8
- end
9
- end
@@ -1,53 +0,0 @@
1
- require 'test/unit'
2
- require File.expand_path(File.dirname(__FILE__) + '/../lib/validates/email_regexp')
3
-
4
- class EmailRegexpTest < Test::Unit::TestCase
5
- include EmailRegexp
6
-
7
- def test_valid
8
- valid_emails = [
9
- 'user@example.com',
10
- 'user.mail@example.com',
11
- 'user@example-domain.com',
12
- 'user@example-d-a.com',
13
- 'user@example.domain.com',
14
- 'user_mail@example.com',
15
- 'user+mail@example.com',
16
- 'user123@example.com',
17
- 'user@example123.com',
18
- 'user@example.domain',
19
- 'UseR@example.com',
20
- 'user-mail@example.com'
21
- ]
22
-
23
- valid_emails.each do |email|
24
- assert regexp_compare(email), "#{email} not valid"
25
- end
26
- end
27
-
28
- def test_invalid
29
- invalid_emails = [
30
- 'example_email',
31
- 'us`er@example.com',
32
- 'use"r@example.com',
33
- 'user@example_domain.com',
34
- 'user@domain.12',
35
- 'user@domain',
36
- 'user@domain..com',
37
- 'user@.domain.com',
38
- 'user@-domain.com',
39
- 'user@domain-.com',
40
- 'user@domain.-a.com',
41
- 'user@domain-com',
42
- 'user mail@example.com',
43
- 'us[er@example.com',
44
- 'us\'er@example.com',
45
- 'us\er@example.com'
46
- ]
47
-
48
- invalid_emails.each do |email|
49
- assert !regexp_compare(email), "#{email} valid"
50
- end
51
- end
52
-
53
- end