simple-validations-rails 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -2
- data/lib/simple-validations-rails/version.rb +1 -1
- data/lib/simple-validations-rails.rb +2 -0
- data/lib/validators/boolean_validator.rb +12 -3
- data/lib/validators/email_validator.rb +12 -5
- data/lib/validators/locale_validator.rb +9 -3
- data/lib/validators/plugin_validator.rb +28 -0
- data/lib/validators/timezone_validator.rb +11 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4533056f636e7aa0e5c3c262097b443bbcc02d5b38fb14c263820c0f4187d8b
|
4
|
+
data.tar.gz: 5dfeae219e97e8b3363bb42dea7d2503e5f7909b06924fab29d4a3e97e9c2243
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1e331ae556d23eb138317658a82ac8ca2a9969109882e86afa5ae3945d32510030359ef585a8ed408aa49fa650b4359b264e4df556b45349f6b4ca0de8be492
|
7
|
+
data.tar.gz: 45777113c8557f6e6bada83db9f2dc623117a057e3595b5a7bb5dd9057472514c938f5d2b298d7f51d1c53231918762920462ebf9e9a5fbe7d270d4e5c7497b3
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# simple-validations-rails
|
2
|
+
![Gem](https://img.shields.io/gem/v/simple-validations-rails.svg)
|
2
3
|
A simple set of validations I got tired of writing over and over and over again. Includes boolean, locale, and email validators.
|
3
4
|
|
4
5
|
## Installation
|
@@ -44,10 +45,21 @@ validates :strict_preference_that_can_be_null, boolean: {strict: true, allow_nil
|
|
44
45
|
Validates that the value is within the allowed locales for the application.
|
45
46
|
```ruby
|
46
47
|
# Allows strings/symbols, checks against available locales.
|
47
|
-
validates locale, locale: true
|
48
|
+
validates :locale, locale: true
|
48
49
|
|
49
50
|
# Same as above, but nil is valid.
|
50
|
-
validates locale, locale: {allow_nil: true}
|
51
|
+
validates :locale, locale: {allow_nil: true}
|
52
|
+
```
|
53
|
+
|
54
|
+
### Timezone Validator
|
55
|
+
|
56
|
+
Validates that the value is a valid timezone.
|
57
|
+
```ruby
|
58
|
+
# Allows strings/symbols, checks against available locales.
|
59
|
+
validates :time_zone, timezone: true
|
60
|
+
|
61
|
+
# Same as above, but nil is valid.
|
62
|
+
validates :time_zone, timezone: {allow_nil: true}
|
51
63
|
```
|
52
64
|
|
53
65
|
### Email Validator
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'simple-validations-rails/railtie'
|
2
2
|
require 'active_model'
|
3
|
+
require 'validators/plugin_validator'
|
3
4
|
require 'validators/boolean_validator'
|
4
5
|
require 'validators/email_validator'
|
5
6
|
require 'validators/locale_validator'
|
7
|
+
require 'validators/timezone_validator'
|
@@ -1,5 +1,14 @@
|
|
1
|
-
class BooleanValidator <
|
2
|
-
|
3
|
-
|
1
|
+
class BooleanValidator < PluginValidator
|
2
|
+
protected
|
3
|
+
|
4
|
+
def validation_message
|
5
|
+
'is not a valid value.'
|
6
|
+
end
|
7
|
+
|
8
|
+
def is_valid?(value)
|
9
|
+
return true if !options[:strict] && value.present?
|
10
|
+
return true if [true, false].include?(value)
|
11
|
+
return true if allows_nil? && value.nil?
|
12
|
+
return false
|
4
13
|
end
|
5
14
|
end
|
@@ -1,13 +1,20 @@
|
|
1
|
-
class EmailValidator <
|
1
|
+
class EmailValidator < PluginValidator
|
2
2
|
MAX_EMAIL_LENGTH = 254
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
protected
|
5
|
+
|
6
|
+
def validation_message
|
7
|
+
'is not a valid email'
|
6
8
|
end
|
7
9
|
|
8
|
-
|
10
|
+
def is_valid?(value)
|
11
|
+
unless options[:skip_length]
|
12
|
+
return false if value.legnth > MAX_EMAIL_LENGTH
|
13
|
+
end
|
14
|
+
return email_regex.match?(value)
|
15
|
+
end
|
9
16
|
|
10
|
-
def self.
|
17
|
+
def self.email_regex()
|
11
18
|
options = {mode: :normal}.merge options
|
12
19
|
|
13
20
|
case options[:mode]
|
@@ -1,5 +1,11 @@
|
|
1
|
-
class LocaleValidator <
|
2
|
-
|
3
|
-
|
1
|
+
class LocaleValidator < PluginValidator
|
2
|
+
protected
|
3
|
+
|
4
|
+
def validation_message
|
5
|
+
'is not a valid locale.'
|
6
|
+
end
|
7
|
+
|
8
|
+
def is_valid?(value)
|
9
|
+
I18n.locale_available?(value)
|
4
10
|
end
|
5
11
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class PluginValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
record.errors.add attribute, (options[:message] || validation_message) unless (allows_nil? && value.nil?) || is_valid?(value)
|
4
|
+
end
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
def allows_nil?()
|
9
|
+
if options[:allow_nil]
|
10
|
+
ActiveSupport::Deprecation.warn('allow_nil is depreciated in favor of the more standard allow_blank')
|
11
|
+
return true
|
12
|
+
elsif options[:allow_blank]
|
13
|
+
return true
|
14
|
+
else
|
15
|
+
return false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Called to validate the value:
|
20
|
+
def is_valid?(value)
|
21
|
+
raise NotImplementedError, 'PluginValidator is not designed to be used directly'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the default validation message:
|
25
|
+
def validation_message
|
26
|
+
raise NotImplementedError, 'PluginValidator is not designed to be used directly'
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-validations-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Schultz
|
@@ -52,6 +52,8 @@ files:
|
|
52
52
|
- lib/validators/boolean_validator.rb
|
53
53
|
- lib/validators/email_validator.rb
|
54
54
|
- lib/validators/locale_validator.rb
|
55
|
+
- lib/validators/plugin_validator.rb
|
56
|
+
- lib/validators/timezone_validator.rb
|
55
57
|
homepage: https://github.com/MatthewSchultz/simple-validations-rails
|
56
58
|
licenses:
|
57
59
|
- MIT
|
@@ -65,7 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
67
|
requirements:
|
66
68
|
- - ">="
|
67
69
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
70
|
+
version: 2.4.0
|
69
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
72
|
requirements:
|
71
73
|
- - ">="
|