drexed-validators 0.0.6 → 0.0.7

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: b40746f0fd7043c303b120ac984c68029f5586da
4
- data.tar.gz: 5856476d02e38656a40f3deefb6a4bf981ea338d
3
+ metadata.gz: 2f314feca89c2256b04e440521356729b39b8053
4
+ data.tar.gz: 32628191163fcce1e2b04d5cd35afca1bcc65f60
5
5
  SHA512:
6
- metadata.gz: fb194eb1b9ddf99a276d470ef8ff893517999d7615e61edacba298a77300f1e48d524e055c024a05b2ab54e5f5e97d68e33b6211cda7671245a9322a8950203b
7
- data.tar.gz: 25c03220f0308d2db4e5f1f7a99b73b4ecba0f9a31fe1f550ed6e8f12fe1b01cff660aa7e2832f1c2e5b41e841b71e1b0820e3d90a3900a353f0296602306565
6
+ metadata.gz: aac895f1e0c55dde4f3f0c13e183b9b55963daad7013bd1be56118647c3c839d4f7dc224b10c7772e6a5075b6a062b09e3cc07d31ad15a2fb354848b583fe026
7
+ data.tar.gz: fc44d8de5c8f8e77ba0ce670935d88442c34bf6aa8faa42f32a8558ed4b17f301a9e600083bf0dc1d23e6873ef27b74651e3fdff25fff7b3f53ca0f5aea481e7
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Validate commonly used attributes easily with drexed-validators.
4
4
 
5
- Currently supported validators: email, url
5
+ Currently supported validators: currency, email, file_size, hex, HTML, IP, name, password, phone, slug, ssn, url, username, and zipcode
6
6
 
7
7
  ## Installation
8
8
 
@@ -0,0 +1,7 @@
1
+ class CurrencyValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^\d*+(\.\d{1,2})?$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class EmailValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /\A[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/i
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,71 @@
1
+ class FileSizeValidator < ActiveModel::EachValidator
2
+ MESSAGES = { is: :wrong_size, minimum: :size_too_small, maximum: :size_too_big }.freeze
3
+ CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
4
+
5
+ DEFAULT_TOKENIZER = lambda { |value| value.split(//) }
6
+ RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
7
+
8
+ def initialize(options)
9
+ if range = (options.delete(:in) || options.delete(:within))
10
+ raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
11
+ options[:minimum], options[:maximum] = range.begin, range.end
12
+ options[:maximum] -= 1 if range.exclude_end?
13
+ end
14
+
15
+ super
16
+ end
17
+
18
+ def check_validity!
19
+ keys = CHECKS.keys & options.keys
20
+
21
+ if keys.empty?
22
+ raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
23
+ end
24
+
25
+ keys.each do |key|
26
+ value = options[key]
27
+
28
+ unless (value.is_a?(Integer) && value >= 0) || value.is_a?(Proc)
29
+ raise ArgumentError, ":#{key} must be a nonnegative Integer or Proc"
30
+ end
31
+ end
32
+ end
33
+
34
+ def validate_each(record, attribute, value)
35
+ unless value.kind_of? CarrierWave::Uploader::Base
36
+ raise ArgumentError, "A CarrierWave::Uploader::Base object was expected"
37
+ end
38
+
39
+ if value.kind_of?(String)
40
+ value = (options[:tokenizer] || DEFAULT_TOKENIZER).call(value)
41
+ end
42
+
43
+ CHECKS.each do |key, validity_check|
44
+ next unless check_value = options[key]
45
+
46
+ check_value = check_value.call(record) if check_value.is_a?(Proc)
47
+
48
+ value ||= [] if key == :maximum
49
+
50
+ value_size = value.size
51
+ next if value_size.send(validity_check, check_value)
52
+
53
+ errors_options = options.except(*RESERVED_OPTIONS)
54
+ errors_options[:file_size] = help.number_to_human_size check_value
55
+
56
+ default_message = options[MESSAGES[key]]
57
+ errors_options[:message] ||= default_message if default_message
58
+
59
+ record.errors.add(attribute, MESSAGES[key], errors_options)
60
+ end
61
+ end
62
+
63
+ def help
64
+ Helper.instance
65
+ end
66
+
67
+ class Helper
68
+ include Singleton
69
+ include ActionView::Helpers::NumberHelper
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ class HexValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class HtmlValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class IpValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class NameValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /\A([a-zA-Z'-]+\s+){0,4}[a-zA-Z'-]*\z/i
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class PasswordValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^[a-z0-9_-]{6,18}$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ class PhoneFormatValidator < ActiveModel::EachValidator
2
+ def validate_each record, attribute, value
3
+ region = options[:region] || :north_america
4
+
5
+ formatted_value = value.gsub(/[\s\.\+\(\)-]*/, "")
6
+ is_number = !(formatted_value =~ /^[-+]?[0-9]+$/).nil?
7
+ unless is_number && validate_by_region(region, formatted_value)
8
+ message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
9
+ record.errors[attribute] << (options[:message] || message )
10
+ end
11
+ end
12
+
13
+ def validate_by_region(region, formatted_value)
14
+ if region == :north_america
15
+ valid_eleven_digit?(formatted_value) || valid_ten_digit?(formatted_value) || valid_seven_digit?(formatted_value)
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ def valid_seven_digit?(input)
22
+ input.length == 7 && input[0] != "0" && input[0] != "1"
23
+ end
24
+
25
+ def valid_ten_digit?(input)
26
+ input.length == 10 && input[0] != "0" && input[0] != "1" && valid_seven_digit?(input.slice(-7, 7))
27
+ end
28
+
29
+ def valid_eleven_digit?(input)
30
+ input.length == 11 && input[0] == "1" && valid_ten_digit?(input.slice(-10, 10))
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ class SlugValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^[a-z0-9-]+$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class UrlValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^\A([\d]{3}\-[\d]{2}\-[\d]{4}|[\d]{9})\Z$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class UrlValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class UsernameValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^[a-z0-9_-]{3,16}$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class ZipcodeValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^\A[\d]{5}(?:[-|\s][\d]{4})?\Z$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly")
5
+ end
6
+ end
7
+ end
@@ -1,6 +1,4 @@
1
1
  require "drexed/validators/version"
2
- require 'active_model/validations/email_validator'
3
- require 'active_model/validations/url_validator'
4
2
 
5
3
  module Drexed
6
4
  module Validators
@@ -1,5 +1,5 @@
1
1
  module Drexed
2
2
  module Validators
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drexed-validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-04 00:00:00.000000000 Z
11
+ date: 2013-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -50,9 +50,21 @@ files:
50
50
  - LICENSE.txt
51
51
  - README.md
52
52
  - Rakefile
53
+ - app/validators/currency_validator.rb
54
+ - app/validators/email_validator.rb
55
+ - app/validators/file_size_validator.rb
56
+ - app/validators/hex_validator.rb
57
+ - app/validators/html_validator.rb
58
+ - app/validators/ip_validator.rb
59
+ - app/validators/name_validator.rb
60
+ - app/validators/password_validator.rb
61
+ - app/validators/phone_validator.rb
62
+ - app/validators/slug_validator.rb
63
+ - app/validators/ssn_validator.rb
64
+ - app/validators/url_validator.rb
65
+ - app/validators/username_validator.rb
66
+ - app/validators/zipcode_validator.rb
53
67
  - drexed-validators.gemspec
54
- - lib/active_model/validations/email_validator.rb
55
- - lib/active_model/validations/url_validator.rb
56
68
  - lib/drexed/validators.rb
57
69
  - lib/drexed/validators/version.rb
58
70
  homepage: https://github.com/drexed/drexed-validators
@@ -1,11 +0,0 @@
1
- module ActiveModel
2
- module Validations
3
- class EmailValidator < EachValidator
4
- def validate_each(object, attribute, value)
5
- unless value =~ /\A[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/i
6
- object.errors[attribute] << (options[:message] || "is not formatted properly")
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- module ActiveModel
2
- module Validations
3
- class UrlValidator < EachValidator
4
- def validate_each(object, attribute, value)
5
- unless value =~ /https?:\/\/[\S]+/i
6
- object.errors[attribute] << (options[:message] || "is not formatted properly")
7
- end
8
- end
9
- end
10
- end
11
- end