active_model_validators 0.1.0
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 +7 -0
- data/LICENSE +13 -0
- data/README.md +55 -0
- data/Rakefile +8 -0
- data/lib/active_model_validators.rb +6 -0
- data/lib/active_model_validators/email_validator.rb +24 -0
- data/lib/active_model_validators/immutability_validator.rb +28 -0
- data/lib/active_model_validators/numeric_array_validator.rb +42 -0
- data/lib/active_model_validators/url_validator.rb +56 -0
- data/lib/active_model_validators/version.rb +3 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61e6ec307e6ea3098d03cad4856de654fe1a2961
|
4
|
+
data.tar.gz: 4dd44eab4a2e3803bf478edd82d4789700e9f498
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e862af0adebbc890ded460477ec2f7b1db4a5376387d573b88c7e294f2ed811512ac8d048d0d38e848a636688f9ba2223055a70d31f539bdbd71f730551d095d
|
7
|
+
data.tar.gz: d5e96ac6c19be4200c475cc6cf88de16bbeedcb803270354b7aac4157c69a9f1e63cfebc6fa278581c4a37c69019f6c1d9777ad292b6ed6b4a5896fef528b82f
|
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2018 OnApp Ltd.
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
<!-- [gem]: [TODO] -->
|
2
|
+
[travis]: https://travis-ci.org/OnApp/active_model_validators
|
3
|
+
|
4
|
+
# ActiveModelValidators
|
5
|
+
|
6
|
+
<!-- [][gem] -->
|
7
|
+
[][travis]
|
8
|
+
|
9
|
+
### Installation
|
10
|
+
Add the following code to your `Gemfile`
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
# Gemfile
|
14
|
+
gem 'active_model_validators'
|
15
|
+
```
|
16
|
+
and run `bundle install`
|
17
|
+
|
18
|
+
### Incuded validators:
|
19
|
+
Email validator, Url validator, Immutability validator, NumericArray validator.
|
20
|
+
|
21
|
+
### Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Model < ActiveRecord::Base
|
25
|
+
validates :my_email, :'active_model_validators/email' => true
|
26
|
+
# my_model_instance = MyModel.create(my_email: 'valid@email.com')
|
27
|
+
# my_model_instance.valid? # => true
|
28
|
+
# my_model_instance.my_email = 'invalid email'
|
29
|
+
# my_model_instance.valid? # => false
|
30
|
+
|
31
|
+
validates :my_immutable_attribute, :'active_model_validators/immutability' => true
|
32
|
+
# my_model_instance = MyModel.create(my_immutable_attribute: 'foo')
|
33
|
+
# my_model_instance.my_immutable_attribute = 'bar'
|
34
|
+
# my_model_instance.valid? # => false
|
35
|
+
|
36
|
+
validates :my_atribute_with_array, :'active_model_validators/numeric_array' => true
|
37
|
+
# my_model_instance = MyModel.create(my_attribute_with_array: [1,2,3])
|
38
|
+
# my_model_instance.valid? # => true
|
39
|
+
# my_model_instance.my_attribute_with_array = 'I am not an array'
|
40
|
+
# my_model_instance.valid? # => false
|
41
|
+
|
42
|
+
validates :my_url_attribue, :'active_model_validators/url' => true
|
43
|
+
# by default it works with +http+ and +https+
|
44
|
+
# It can be easily extended with +protocols+ param
|
45
|
+
# :'active_model_validators/url' => { protocols: [URI::HTTP, URI::HTTPS, URI::FTP] }
|
46
|
+
# valid_url = 'https://www.valid.com'
|
47
|
+
# my_model_instance = MyModel.create(my_url_attribue: valid_url)
|
48
|
+
# my_model_instance.valid? # => true
|
49
|
+
# my_model_instance.my_url_attribue = 'invalid url'
|
50
|
+
# my_model_instance.valid? # => false
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
### Licence
|
55
|
+
See [`LICENSE`](LICENSE) file.
|
data/Rakefile
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
require_relative 'active_model_validators/email_validator'
|
4
|
+
require_relative 'active_model_validators/url_validator'
|
5
|
+
require_relative 'active_model_validators/immutability_validator'
|
6
|
+
require_relative 'active_model_validators/numeric_array_validator'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveModelValidators #:nodoc:
|
2
|
+
|
3
|
+
# == Email addresses validator
|
4
|
+
class EmailValidator < ::ActiveModel::EachValidator
|
5
|
+
|
6
|
+
# Regular expression used for validation
|
7
|
+
# EMAIL_ADDRESS_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
8
|
+
EMAIL_ADDRESS_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
9
|
+
|
10
|
+
# * +record+ - ActiveRecord model
|
11
|
+
# * +attr+ - model attribute where email address will be stored
|
12
|
+
# * +value+ - entered email address
|
13
|
+
#
|
14
|
+
# ==== Example
|
15
|
+
# class Model < ActiveRecord::Base
|
16
|
+
# validates :my_email, :'active_model_validators/email' => true
|
17
|
+
# end
|
18
|
+
def validate_each(record, attr, value)
|
19
|
+
unless value =~ EMAIL_ADDRESS_REGEX
|
20
|
+
record.errors.add(attr, :invalid)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ActiveModelValidators #:nodoc:
|
2
|
+
|
3
|
+
# == Attributes immutability validator
|
4
|
+
class ImmutabilityValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
# Adds error if there an attempt to change an immutable attribute
|
7
|
+
#
|
8
|
+
# * +record+ - ActiveRecord model
|
9
|
+
# * +attr+ - model attribute which is supposed to be immutable
|
10
|
+
# * +value+ - value
|
11
|
+
#
|
12
|
+
# ==== Example
|
13
|
+
# class Model < ActiveRecord::Base
|
14
|
+
# validates :my_atribute, :'active_model_validators/immutability' => true
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# my_model_instance = MyModel.create(my_attribute: 'foo')
|
18
|
+
# my_model_instance.my_attribute = 'bar'
|
19
|
+
# my_model_instance.valid? # => false
|
20
|
+
def validate_each(record, attribute, value)
|
21
|
+
return if record.new_record?
|
22
|
+
|
23
|
+
if record.public_send("#{attribute}_changed?")
|
24
|
+
record.errors.add(attribute, :immutability)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ActiveModelValidators #:nodoc:
|
2
|
+
|
3
|
+
# == Validator of numerical array
|
4
|
+
class NumericArrayValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
# Adds error if there an attribute with not a numerical array provided
|
7
|
+
#
|
8
|
+
# * +record+ - ActiveRecord model
|
9
|
+
# * +attr+ - model attribute to store an array
|
10
|
+
# * +value+ - value, supposed to be a numerical array
|
11
|
+
#
|
12
|
+
# ==== Example
|
13
|
+
# class MyModel < ActiveRecord::Base
|
14
|
+
# validates :my_atribute, :'active_model_validators/numeric_array' => true
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# my_model_instance = MyModel.create(my_attribute: [1,2,3])
|
18
|
+
# my_model_instance.valid? # => true
|
19
|
+
# my_model_instance.my_attribute = 'bar'
|
20
|
+
# my_model_instance.valid? # => false
|
21
|
+
def validate_each(record, attribute, value)
|
22
|
+
unless array_numeric?(value)
|
23
|
+
record.errors.add(attribute, options[:message] || numeric_message)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def array_numeric?(values) #:nodoc:
|
30
|
+
return false unless values.is_a? Array
|
31
|
+
|
32
|
+
values.all? { |value| value.is_a?(Integer) && value > 0 }
|
33
|
+
end
|
34
|
+
|
35
|
+
def numeric_message #:nodoc:
|
36
|
+
I18n.t(
|
37
|
+
'activerecord.errors.messages.not_numeric_array',
|
38
|
+
default: 'Should be an array of integers'
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module ActiveModelValidators #:nodoc:
|
4
|
+
|
5
|
+
# == URL validator
|
6
|
+
class UrlValidator < ::ActiveModel::EachValidator
|
7
|
+
# Default protocols used for validation if no custom protocols provided
|
8
|
+
# DEFAULT_PROTOCOLS = [::URI::HTTP, ::URI::HTTPS]
|
9
|
+
DEFAULT_PROTOCOLS = [::URI::HTTP, ::URI::HTTPS].freeze
|
10
|
+
|
11
|
+
# Adds error if there is invalid URL address.
|
12
|
+
#
|
13
|
+
# By default it works with +http+ and +https+.
|
14
|
+
#
|
15
|
+
# It can be easily extended with protocols param
|
16
|
+
#
|
17
|
+
# * +record+ - ActiveRecord model
|
18
|
+
# * +attr+ - model attribute to store an URL
|
19
|
+
# * +value+ - value, supposed to be a valid URL-adress
|
20
|
+
#
|
21
|
+
# ==== Example
|
22
|
+
#
|
23
|
+
# class MyModel < ActiveRecord::Base
|
24
|
+
# # default usage
|
25
|
+
# validates :my_url, :'active_model_validators/url' => true
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# class MyModel < ActiveRecord::Base
|
29
|
+
# # with custom URL protocols
|
30
|
+
# validates :my_url, :'active_model_validators/url' => { protocols: [URI::HTTP, URI::HTTPS, URI::FTP] }
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# valid_url = 'https://www.valid.com'
|
34
|
+
# my_model_instance = MyModel.create(my_url: valid_url)
|
35
|
+
# my_model_instance.valid? # => true
|
36
|
+
# my_model_instance.my_attribute = 'invalid_url'
|
37
|
+
# my_model_instance.valid? # => false
|
38
|
+
def validate_each(record, attribute, value)
|
39
|
+
record.errors.add(attribute, error_message) unless url_valid?(value)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def url_valid?(url) #:nodoc:
|
45
|
+
options.fetch(:protocols, DEFAULT_PROTOCOLS).any? { |type| URI.parse(url).kind_of?(type) }
|
46
|
+
rescue URI::InvalidURIError
|
47
|
+
false
|
48
|
+
end
|
49
|
+
|
50
|
+
def error_message #:nodoc:
|
51
|
+
options.fetch(:message) do
|
52
|
+
I18n.t('activerecord.errors.messages.url', default: 'must be valid URL')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_validators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- OnApp Ltd.
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: OnApp ActiveModel validators.
|
42
|
+
email: support@onapp.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/active_model_validators.rb
|
51
|
+
- lib/active_model_validators/email_validator.rb
|
52
|
+
- lib/active_model_validators/immutability_validator.rb
|
53
|
+
- lib/active_model_validators/numeric_array_validator.rb
|
54
|
+
- lib/active_model_validators/url_validator.rb
|
55
|
+
- lib/active_model_validators/version.rb
|
56
|
+
homepage: https://github.com/OnApp/active_model_validators
|
57
|
+
licenses:
|
58
|
+
- Apache 2.0
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.2.0
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: A small handy library to validate active_record models
|
80
|
+
test_files: []
|