format_validator 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7d32692c4abc76830fa5812fae6c2cf9f89e5fe
4
- data.tar.gz: 82f63c5032a0d3c089ec17b5dff088eaab32fd76
3
+ metadata.gz: c12190d354629682f5b6170ee284421d864abe59
4
+ data.tar.gz: 5d9bc02e416b14c2644cef9d2c6f5d6b105648e7
5
5
  SHA512:
6
- metadata.gz: 70f0341d45b93a701a98102e8c12df7be9216b1c373c89a2fffd280269f2d00642de8ccbff26d35e7f05e2bf4b24df97b3166a15c27b3bfee606bfc6cf3a09c8
7
- data.tar.gz: 22b9f9645a9c1bda090dbf875bd83dc7a4732da3fa698a128cbd1a07fa96844b88efb4cf93760aae5b6f66f47d64a4dd01e034932233a5cc4c4ee7d45f3caa8a
6
+ metadata.gz: e10d8ea27a8cda27fde3897b63c7234777280002ccfc8e5fcdd73d8e01f20144526dbb87203e26d3506df5d96146878839533b800731f3bab1f507fa8ac31654
7
+ data.tar.gz: 8425a246aae184a2fe81b1e2b98ce1f55d75eda5ea02220c75abf1e8dca6074ef4585a47423e79bca8b8b9fed96a25c16acf1b9250c15b815f37d8dd68f494de
data/CHANGELOG CHANGED
@@ -9,3 +9,9 @@
9
9
  ## v0.0.3
10
10
 
11
11
  * Minor fix for future date validator
12
+
13
+ ## v0.0.4
14
+
15
+ * Validate hostname
16
+ * Update README
17
+ * Minor fix for url validator regex
data/README.md CHANGED
@@ -12,17 +12,12 @@ And then execute:
12
12
 
13
13
  $ bundle
14
14
 
15
- Or install it yourself as:
16
-
17
- $ gem install format_validator
18
-
19
15
  ## Usage
20
16
 
21
- As of today the gem validates only email, url, and zip_code.
22
-
23
17
  ``` ruby
24
18
  validates :email, email_format: true
25
19
  validates :url, url_format: true
20
+ validates :hostname, hostname_format: true
26
21
  validates :zip_code, zip_code_format: true
27
22
  validates :expiration_date, future_date: true
28
23
  ```
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = FormatValidator::VERSION
9
9
  spec.authors = ['Jamal El Milahi']
10
10
  spec.email = ['jamal@elmilahi.com']
11
- spec.description = %q{Active Model missing format validators}
11
+ spec.description = %q{Active Model's missing format validators}
12
12
  spec.summary = %q{format_validator is a gem that adds the missing format validators to Active Model}
13
- spec.homepage = ''
13
+ spec.homepage = 'https://github.com/bloc40/format_validator'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -1,5 +1,10 @@
1
1
  require 'format_validator/version'
2
2
  require 'format_validator/email_format_validator'
3
3
  require 'format_validator/url_format_validator'
4
+ require 'format_validator/hostname_format_validator'
4
5
  require 'format_validator/zip_code_format_validator'
5
6
  require 'format_validator/future_date_validator'
7
+
8
+ module FormatValidator
9
+ I18n.load_path << File.expand_path('../locales/en.yml', __FILE__)
10
+ end
@@ -3,7 +3,7 @@ module ActiveModel
3
3
  class EmailFormatValidator < ActiveModel::EachValidator
4
4
  def validate_each(object, attribute, value)
5
5
  unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
6
- object.errors[attribute] << (options[:message] || 'is not valid')
6
+ object.errors[attribute] << (options[:message] || I18n.t('form.errors.email'))
7
7
  end
8
8
  end
9
9
  end
@@ -3,7 +3,7 @@ module ActiveModel
3
3
  class FutureDateValidator < ActiveModel::EachValidator
4
4
  def validate_each(object, attribute, value)
5
5
  if value.to_i <= Time.now.to_i
6
- object.errors[attribute] << (options[:message] || 'must be in the future')
6
+ object.errors[attribute] << (options[:message] || I18n.t('form.errors.date'))
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,11 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class HostnameFormatValidator < ActiveModel::EachValidator
4
+ def validate_each(object, attribute, value)
5
+ unless value =~ /\A([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\z/i
6
+ object.errors[attribute] << (options[:message] || I18n.t('form.errors.hostname'))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -2,8 +2,8 @@ module ActiveModel
2
2
  module Validations
3
3
  class UrlFormatValidator < ActiveModel::EachValidator
4
4
  def validate_each(object, attribute, value)
5
- unless value =~ /\Ahttps?:\/\/([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\Z/i
6
- object.errors[attribute] << (options[:message] || 'is not valid')
5
+ unless value =~ /\Ahttps?:\/\/([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\z/i
6
+ object.errors[attribute] << (options[:message] || I18n.t('form.errors.url'))
7
7
  end
8
8
  end
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module FormatValidator
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -3,7 +3,7 @@ module ActiveModel
3
3
  class ZipCodeFormatValidator < ActiveModel::EachValidator
4
4
  def validate_each(object, attribute, value)
5
5
  unless value =~ /^\d{5}(-\d{4})?$/
6
- object.errors[attribute] << (options[:message] || 'is not valid')
6
+ object.errors[attribute] << (options[:message] || I18n.t('form.errors.zip_code'))
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,8 @@
1
+ en:
2
+ form:
3
+ errors:
4
+ date: must be in the future
5
+ email: is invalid
6
+ url: is invalid
7
+ hostname: is invalid
8
+ zip_code: is invalid
@@ -26,7 +26,7 @@ describe 'EmailFormatValidator' do
26
26
  subject.email = invalid_email
27
27
  subject.valid?.must_equal false
28
28
  subject.errors.messages.size.must_equal 1
29
- subject.errors.messages[:email].must_equal ['is not valid']
29
+ subject.errors.messages[:email].must_equal ['is invalid']
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,36 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'HostnameFormatValidator' do
4
+ describe '#validate_each' do
5
+ let(:klass) do
6
+ Class.new do
7
+ include ActiveModel::Validations
8
+ attr_accessor :hostname
9
+ validates :hostname, hostname_format: true
10
+ end
11
+ end
12
+
13
+ subject { klass.new }
14
+
15
+ describe 'valid' do
16
+ %w[www.example.com example.com foo.example.com].each do |_hostname|
17
+ it "should not add any error when the hostname is #{_hostname}" do
18
+ subject.hostname = _hostname
19
+ subject.valid?.must_equal true
20
+ subject.errors.messages.must_be_empty
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'invalid' do
26
+ ['example', 'http://example.com', 'example. com'].each do |_hostname|
27
+ it "should add error when hostname is (#{_hostname})" do
28
+ subject.hostname = _hostname
29
+ subject.valid?.must_equal false
30
+ subject.errors.messages.size.must_equal 1
31
+ subject.errors.messages[:hostname].must_equal ['is invalid']
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -28,7 +28,7 @@ describe 'UrlFormatValidator' do
28
28
  subject.url = _url
29
29
  subject.valid?.must_equal false
30
30
  subject.errors.messages.size.must_equal 1
31
- subject.errors.messages[:url].must_equal ['is not valid']
31
+ subject.errors.messages[:url].must_equal ['is invalid']
32
32
  end
33
33
  end
34
34
  end
@@ -28,7 +28,7 @@ describe 'ZipCodeFormatValidator' do
28
28
  subject.zip_code = invalid_zip_code
29
29
  subject.valid?.must_equal false
30
30
  subject.errors.messages.size.must_equal 1
31
- subject.errors.messages[:zip_code].must_equal ['is not valid']
31
+ subject.errors.messages[:zip_code].must_equal ['is invalid']
32
32
  end
33
33
  end
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: format_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamal El Milahi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-16 00:00:00.000000000 Z
11
+ date: 2013-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Active Model missing format validators
55
+ description: Active Model's missing format validators
56
56
  email:
57
57
  - jamal@elmilahi.com
58
58
  executables: []
@@ -70,15 +70,18 @@ files:
70
70
  - lib/format_validator.rb
71
71
  - lib/format_validator/email_format_validator.rb
72
72
  - lib/format_validator/future_date_validator.rb
73
+ - lib/format_validator/hostname_format_validator.rb
73
74
  - lib/format_validator/url_format_validator.rb
74
75
  - lib/format_validator/version.rb
75
76
  - lib/format_validator/zip_code_format_validator.rb
77
+ - lib/locales/en.yml
76
78
  - spec/format_validator/email_format_validator_spec.rb
77
79
  - spec/format_validator/future_date_validator_spec.rb
80
+ - spec/format_validator/hostname_format_validator_spec.rb
78
81
  - spec/format_validator/url_format_validator_spec.rb
79
82
  - spec/format_validator/zip_code_format_validator_spec.rb
80
83
  - spec/spec_helper.rb
81
- homepage: ''
84
+ homepage: https://github.com/bloc40/format_validator
82
85
  licenses:
83
86
  - MIT
84
87
  metadata: {}
@@ -106,6 +109,7 @@ summary: format_validator is a gem that adds the missing format validators to Ac
106
109
  test_files:
107
110
  - spec/format_validator/email_format_validator_spec.rb
108
111
  - spec/format_validator/future_date_validator_spec.rb
112
+ - spec/format_validator/hostname_format_validator_spec.rb
109
113
  - spec/format_validator/url_format_validator_spec.rb
110
114
  - spec/format_validator/zip_code_format_validator_spec.rb
111
115
  - spec/spec_helper.rb