rails_validations 1.0 → 1.1

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: df7a1d3faea070ed50a78256af355989bc2f7703
4
- data.tar.gz: 5f0817c861fc63425c5d1f2bf2b45fc50a7c6588
3
+ metadata.gz: 1dbffdfff5db9e79c428f0f63703e7823f48ad06
4
+ data.tar.gz: 20b3a0d8aa8c014eb0bf13d145e856dc229dcdd3
5
5
  SHA512:
6
- metadata.gz: a3ec178acd0a6744f6b57c8e2e4d276180b96aa45c71a7a2c2640a1ebcdb38df1cb196534583b449f58bb423d11b89062a4f4a78be3dc81c8be75e612d11f8bd
7
- data.tar.gz: f5dedfc45b7943e5b820b56822ddd5686b5dbc5d9f3598156c195ee9e06cda3b18ddd9af6274611426a8571b373e5577a4c8e651073d2a8d96d2ce29a4b9a3e4
6
+ metadata.gz: 375251bca3b2ced7f504d983fdcccbe7687b9f5d9513cf6ae1b8483e831e7f813010e4d4039ee04b43e0b483bebf60af5ff9eeaa47af2b83b0848bad17c0dbfc
7
+ data.tar.gz: 49e34e36be5bf2f9818b3b4e34c54ce3c92917b24ab78a708e6ed4ada6c5f1faed0a32c19a65410ade7b0581e528dcc720d70f94b4d7625daf0851f299637d65
data/README.markdown CHANGED
@@ -1,10 +1,89 @@
1
- Extra validations for rails:
1
+ A few extra validations for rails.
2
2
 
3
- - date
4
- - domain
5
- - email
6
- - iban
7
- - phone
8
- - postal\_code
3
+ [RubyGems.org entry](http://rubygems.org/gems/rails_validations)
9
4
 
10
- This needs to be documented...
5
+ date
6
+ ----
7
+ Validate if a column is a valid date, and if it's before or after another date.
8
+
9
+ validates :date_column, date: true
10
+ validates :date_column, date: { after: Date.today }
11
+ validates :date_column, date: { after_or_equal_to: Date.today }
12
+ validates :date_column, date: { equal_to: Date.today }
13
+ validates :date_column, date: { before: Date.today }
14
+ validates :date_column, date: { before_or_equal_to: Date.today }
15
+
16
+ # Check if the column `enddate` is after the value of the column `begindate`
17
+ validates :begindate, date: true
18
+ validates :enddate, date: { after: :begindate }
19
+
20
+
21
+ domain
22
+ ------
23
+ Validate if a string is a valid domain. This should work with [IDN](idn).
24
+
25
+ validates :domain_column, domain: true
26
+
27
+ # Set a minimum/maximum number of domain parts (aka. labels)
28
+ validates :domain_column, domain: { min_domain_parts: 2 }
29
+ validates :domain_column, domain: { max_domain_parts: 2 }
30
+
31
+
32
+ email
33
+ -----
34
+ Do a basic checks for emails. Not that this check is *not* very strict, it is
35
+ nigh-impossible to check if an email address is valid (and even if it is, it's
36
+ no guarantee if emails actually gets delivered to this address). This should
37
+ work with unicode addresses ([RFC 6531][rfc6531], [IDN][idn]).
38
+
39
+ validates :email_column, email: true
40
+
41
+
42
+ iban
43
+ ----
44
+ Check if this is a valid IBAN account number. This uses the
45
+ [iban-tools][iban-tools] gem.
46
+
47
+ validates :iban_column, iban: true
48
+
49
+
50
+ phone
51
+ -----
52
+ Check if this is a valid phone number. As with the Email check, this isn't
53
+ particularly strict; conventions for writing phone numbers vary a lot.
54
+
55
+ validates :phone_column, phone: true
56
+
57
+
58
+ postal\_code
59
+ ------------
60
+ Check if this is a valid postal code (or zip code for the states).
61
+
62
+ validates :postal_code_column, postal_code: { country: :nl }
63
+
64
+ # Country defaults to I18n.locale
65
+ validates :postal_code_column, postal_code: true
66
+
67
+
68
+ Currently implemented countries:
69
+
70
+ - `nl` - The Netherlands
71
+
72
+
73
+ ChangeLog
74
+ =========
75
+
76
+ version 1.1, 20141003
77
+ ---------------------
78
+ - Make the date validation work if the column it points to is `nil`.
79
+ - Add documentation.
80
+
81
+
82
+ version 1.0, 20140905
83
+ ---------------------
84
+ - Initial release.
85
+
86
+
87
+ [idn]: http://en.wikipedia.org/wiki/Internationalized_domain_name).
88
+ [rfc6531]: https://tools.ietf.org/html/rfc6531
89
+ [iban-tools]: https://github.com/iulianu/iban-tools
@@ -23,7 +23,7 @@ class DateValidator < ActiveModel::EachValidator
23
23
  elsif raw_value.respond_to? :to_date
24
24
  raw_value.to_date
25
25
  else
26
- record.errors.add attribute, I18n.t('rails_validations.date.invalid')
26
+ false
27
27
  end
28
28
 
29
29
  #elsif raw_value.is_a?(Symbol) || raw_value.is_a?(String)
@@ -48,6 +48,11 @@ class DateValidator < ActiveModel::EachValidator
48
48
  raise ArgumentError
49
49
  end
50
50
 
51
+ unless option_value
52
+ record.errors.add attribute, I18n.t('rails_validations.date.invalid')
53
+ return
54
+ end
55
+
51
56
  unless value.send CHECKS[option], option_value
52
57
  record.errors.add attribute, I18n.t("rails_validations.date.#{option}", date: I18n.l(option_value))
53
58
  end
data/spec/spec_helper.rb CHANGED
@@ -45,11 +45,13 @@ module ValidationsSpecHelper
45
45
 
46
46
  def v; @value end
47
47
  def v= v; @value = v end
48
+ def v2; @value2 end
49
+ def v2= v; @value2 = v end
48
50
  end
49
51
 
50
52
 
51
- def model v
52
- described_class.new v: v
53
+ def model v, v2=nil
54
+ described_class.new v: v, v2: v2
53
55
  end
54
56
 
55
57
 
@@ -15,7 +15,6 @@ describe ValidationsSpecHelper::Date do
15
15
  with_validation 'date: true' do
16
16
  expect(model(Date.today).valid?).to eq(true)
17
17
  expect(model(Time.now).valid?).to eq(true)
18
-
19
18
  end
20
19
  end
21
20
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Tournoij
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-05 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: Extra validations for rails
69
+ description: 'Extra validations for rails: date, domain, email, iban, phone, postal_code'
70
70
  email:
71
71
  - martin@lico.nl
72
72
  executables: []