simple_validate 1.1.1 → 1.1.2
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 +5 -5
- data/.rubocop.yml +11 -0
- data/README.md +33 -7
- data/Rakefile +5 -3
- data/lib/simple_validate/validates_base.rb +5 -5
- data/lib/simple_validate/validates_exclusion_of.rb +8 -0
- data/lib/simple_validate/validates_format_of.rb +6 -3
- data/lib/simple_validate/validates_inclusion_of.rb +8 -0
- data/lib/simple_validate/validates_length_of.rb +20 -16
- data/lib/simple_validate/validates_numericality_of.rb +2 -1
- data/lib/simple_validate/validates_presence_of.rb +2 -2
- data/lib/simple_validate/validates_set_base.rb +14 -0
- data/lib/simple_validate/version.rb +1 -1
- data/lib/simple_validate.rb +18 -10
- data/simple_validate.gemspec +7 -7
- metadata +28 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4be4e322ba8383ab04495306a2ed7b71db142d32062e50f11fc6c27a60632491
|
4
|
+
data.tar.gz: 1aeaad50c99f01008917a593b73fbe0c87e45e72548d5a479f4d8e646756109b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04b3f8dcbe4ab31acd3b65a407e1c561dcc2a6a813551b9bb7227e2b8c37b143ad157c0165099ccb0d1fd2759c81fbe7b679010274f79cde3b48c84b98eabd0a
|
7
|
+
data.tar.gz: 383a7560fd119a51047ca27d0dbc790cfd1127b9b09533ad6ab590e365a419d62509dee923814a7b9aa8cf5aafc7702a5206f66354cec01635e71748e532476a
|
data/.rubocop.yml
ADDED
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
|
2
|
-
# SimpleValidate
|
1
|
+
# Simple Validate
|
3
2
|
|
4
3
|
[](https://travis-ci.org/nikkypx/simple_validate)
|
5
4
|
|
6
|
-
Borrowing ideas from [validatable](https://github.com/jnunemaker/validatable) and Rails validations, this is a library for validations for any ruby object.
|
5
|
+
> Borrowing ideas from [validatable](https://github.com/jnunemaker/validatable) and Rails validations, this is a library for validations for any ruby object.
|
7
6
|
|
8
7
|
## Installation
|
9
8
|
|
@@ -35,7 +34,9 @@ class Person
|
|
35
34
|
validates_presence_of :name, :age
|
36
35
|
validates_numericality_of :age
|
37
36
|
end
|
37
|
+
```
|
38
38
|
|
39
|
+
```ruby
|
39
40
|
=> p = Person.new
|
40
41
|
=> #<Person:0x007f9431536408>
|
41
42
|
=> p.valid?
|
@@ -49,10 +50,8 @@ end
|
|
49
50
|
|
50
51
|
### Presence
|
51
52
|
|
52
|
-
* It is possible to pass a different message to any validation.
|
53
|
-
|
54
53
|
```ruby
|
55
|
-
validates_presence_of :attribute
|
54
|
+
validates_presence_of :attribute
|
56
55
|
```
|
57
56
|
|
58
57
|
### Numericality
|
@@ -64,7 +63,7 @@ end
|
|
64
63
|
### Format
|
65
64
|
|
66
65
|
```ruby
|
67
|
-
validates_format_of :attribute, with:
|
66
|
+
validates_format_of :attribute, with: /[A-Z]+/
|
68
67
|
```
|
69
68
|
|
70
69
|
### Length
|
@@ -77,10 +76,37 @@ end
|
|
77
76
|
validates_length_of :attribute, in: 6..9
|
78
77
|
```
|
79
78
|
|
79
|
+
### Inclusion and Exclusion
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
validates_inclusion_of :domain, in: [:net, :com]
|
83
|
+
validates_exclusion_of :name, in: [:Bojack, :Horseman]
|
84
|
+
```
|
85
|
+
|
86
|
+
### Options
|
87
|
+
|
88
|
+
#### Custom error messages
|
89
|
+
|
90
|
+
* It is possible to pass a custom error message to any validation.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
validates_presence_of :attribute, message: 'attribute is required!'
|
94
|
+
```
|
95
|
+
|
96
|
+
#### Conditional validation
|
97
|
+
|
98
|
+
* It is possible to pass an `if` option with a proc for a conditional validation
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
validates_presence_of :attribute, if: Proc.new { true }
|
102
|
+
```
|
103
|
+
|
104
|
+
|
80
105
|
## Development
|
81
106
|
|
82
107
|
`$ rake` to run the specs
|
83
108
|
|
109
|
+
|
84
110
|
## Contributing
|
85
111
|
|
86
112
|
Bug reports and pull requests are welcome on GitHub at https://github.com/nikkypx/simple_validate.
|
data/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
3
4
|
|
4
5
|
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
RuboCop::RakeTask.new
|
5
7
|
|
6
|
-
task :
|
8
|
+
task default: %i[rubocop spec]
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module SimpleValidate
|
2
2
|
class ValidatesBase
|
3
|
-
|
4
|
-
attr_accessor :attribute
|
3
|
+
attr_accessor :message, :attribute, :condition
|
5
4
|
|
6
|
-
def initialize(attribute, message)
|
7
|
-
|
8
|
-
|
5
|
+
def initialize(attribute, message, condition)
|
6
|
+
self.message = message
|
7
|
+
self.attribute = attribute
|
8
|
+
self.condition = condition
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -1,13 +1,16 @@
|
|
1
1
|
module SimpleValidate
|
2
2
|
class ValidatesFormatOf < ValidatesBase
|
3
|
+
attr_accessor :regex
|
3
4
|
|
4
5
|
def initialize(attribute, options)
|
5
|
-
|
6
|
-
super(attribute, options[:message] ||
|
6
|
+
self.regex = options[:with]
|
7
|
+
super(attribute, options[:message] ||
|
8
|
+
'is incorrect format', options[:if] || proc { true })
|
7
9
|
end
|
8
10
|
|
9
11
|
def valid?(instance)
|
10
|
-
|
12
|
+
raise ArgumentError if regex.nil? || !regex.is_a?(Regexp)
|
13
|
+
!!(instance.send(attribute) =~ regex)
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
@@ -1,21 +1,20 @@
|
|
1
1
|
module SimpleValidate
|
2
|
-
class ValidatesLengthOf
|
3
|
-
attr_reader :attribute
|
2
|
+
class ValidatesLengthOf < ValidatesBase
|
4
3
|
class InvalidLengthOption < ArgumentError; end
|
4
|
+
attr_reader :attribute
|
5
|
+
attr_accessor :options
|
5
6
|
|
6
|
-
VALID_LENGTH_OPTIONS = %i
|
7
|
+
VALID_LENGTH_OPTIONS = %i[maximum minimum in is].freeze
|
7
8
|
|
8
9
|
def initialize(attribute, options)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
check_options(@options)
|
13
|
-
@length_validator = @options.select { |k, _| VALID_LENGTH_OPTIONS.include?(k) }
|
10
|
+
super(attribute, options.delete(:message), options.delete(:if) ||
|
11
|
+
proc { true })
|
12
|
+
self.options = options
|
14
13
|
end
|
15
14
|
|
16
15
|
def message
|
17
16
|
@message ||= begin
|
18
|
-
case
|
17
|
+
case validator
|
19
18
|
when :minimum
|
20
19
|
'is too short'
|
21
20
|
when :maximum
|
@@ -27,18 +26,15 @@ module SimpleValidate
|
|
27
26
|
end
|
28
27
|
|
29
28
|
def validator
|
30
|
-
|
29
|
+
options.keys.first
|
31
30
|
end
|
32
31
|
|
33
|
-
def
|
34
|
-
|
35
|
-
raise InvalidLengthOption, "Invalid length option given #{@options.keys}"
|
36
|
-
end
|
32
|
+
def valid_length
|
33
|
+
options.values.first
|
37
34
|
end
|
38
35
|
|
39
36
|
def valid_length?(actual_length)
|
40
|
-
|
41
|
-
case validator_key
|
37
|
+
case validator
|
42
38
|
when :minimum
|
43
39
|
actual_length >= valid_length
|
44
40
|
when :maximum
|
@@ -51,6 +47,14 @@ module SimpleValidate
|
|
51
47
|
end
|
52
48
|
|
53
49
|
def valid?(instance)
|
50
|
+
if options.keys.size > 1
|
51
|
+
raise ArgumentError, 'Only one length argument can be provided'
|
52
|
+
end
|
53
|
+
|
54
|
+
unless VALID_LENGTH_OPTIONS.include?(options.keys.first)
|
55
|
+
raise InvalidLengthOption, "Invalid length option given #{options.keys}"
|
56
|
+
end
|
57
|
+
|
54
58
|
actual_length = instance.send(attribute).length
|
55
59
|
valid_length?(actual_length)
|
56
60
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module SimpleValidate
|
2
2
|
class ValidatesNumericalityOf < ValidatesBase
|
3
3
|
def initialize(attribute, options)
|
4
|
-
super(attribute, options[:message] ||
|
4
|
+
super(attribute, options[:message] ||
|
5
|
+
'must be a number', options[:if] || proc { true })
|
5
6
|
end
|
6
7
|
|
7
8
|
def valid?(instance)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module SimpleValidate
|
2
2
|
class ValidatesPresenceOf < ValidatesBase
|
3
|
-
|
4
3
|
def initialize(attribute, options)
|
5
|
-
super(attribute, options[:message] ||
|
4
|
+
super(attribute, options[:message] ||
|
5
|
+
"can't be empty", options[:if] || proc { true })
|
6
6
|
end
|
7
7
|
|
8
8
|
def valid?(instance)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module SimpleValidate
|
4
|
+
class ValidatesSetBase < ValidatesBase
|
5
|
+
attr_accessor :set, :options
|
6
|
+
|
7
|
+
def initialize(attribute, options)
|
8
|
+
self.options = options
|
9
|
+
self.set = Set.new(Array(options[:in]).map(&:to_s))
|
10
|
+
super(attribute, options[:message] ||
|
11
|
+
'breaks inclusion/exclusion rules', options[:if] || proc { true })
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/simple_validate.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
%w[base
|
2
|
+
presence_of
|
3
|
+
format_of
|
4
|
+
numericality_of
|
5
|
+
length_of
|
6
|
+
set_base
|
7
|
+
inclusion_of
|
8
|
+
exclusion_of].each do |validation|
|
9
|
+
require "simple_validate/validates_#{validation}"
|
10
|
+
end
|
11
|
+
|
7
12
|
require 'simple_validate/errors'
|
13
|
+
require 'simple_validate/version'
|
8
14
|
require 'active_support/all'
|
9
15
|
|
10
16
|
module SimpleValidate
|
@@ -26,18 +32,20 @@ module SimpleValidate
|
|
26
32
|
|
27
33
|
module ClassMethods
|
28
34
|
def method_missing(method, *args, &block)
|
29
|
-
if
|
30
|
-
add_validations(args, const_get(
|
35
|
+
if respond_to?(method)
|
36
|
+
add_validations(args, const_get(method.to_s.classify))
|
31
37
|
else
|
32
38
|
super
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
42
|
def respond_to_missing?(method, include_private = false)
|
37
|
-
|
43
|
+
method.to_s =~ /(validates_
|
38
44
|
(format|
|
39
45
|
presence|
|
40
46
|
numericality|
|
47
|
+
inclusion|
|
48
|
+
exclusion|
|
41
49
|
length)_of)
|
42
50
|
/x || super
|
43
51
|
end
|
@@ -55,7 +63,7 @@ module SimpleValidate
|
|
55
63
|
|
56
64
|
def validate(instance)
|
57
65
|
validations.each do |validation|
|
58
|
-
|
66
|
+
if validation.condition.call && !validation.valid?(instance)
|
59
67
|
instance.errors.add(validation.attribute, validation.message)
|
60
68
|
end
|
61
69
|
end
|
data/simple_validate.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'simple_validate/version'
|
@@ -14,11 +13,12 @@ Gem::Specification.new do |spec|
|
|
14
13
|
spec.homepage = 'https://github.com/nikkypx/simple_validate'
|
15
14
|
spec.license = 'MIT'
|
16
15
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject
|
18
|
-
|
19
|
-
|
20
|
-
spec.require_paths = ['lib']
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
21
19
|
|
22
|
-
spec.
|
23
|
-
spec.
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
spec.add_dependency 'activesupport', '~> 5.0'
|
22
|
+
spec.add_development_dependency 'rspec'
|
23
|
+
spec.add_development_dependency 'rubocop'
|
24
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_validate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Palaniuk
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,28 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
description: Validations for any ruby object
|
42
56
|
email:
|
43
57
|
- npalaniuk@gmail.com
|
@@ -47,6 +61,7 @@ extra_rdoc_files: []
|
|
47
61
|
files:
|
48
62
|
- ".gitignore"
|
49
63
|
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
50
65
|
- ".travis.yml"
|
51
66
|
- Gemfile
|
52
67
|
- LICENSE.txt
|
@@ -55,10 +70,13 @@ files:
|
|
55
70
|
- lib/simple_validate.rb
|
56
71
|
- lib/simple_validate/errors.rb
|
57
72
|
- lib/simple_validate/validates_base.rb
|
73
|
+
- lib/simple_validate/validates_exclusion_of.rb
|
58
74
|
- lib/simple_validate/validates_format_of.rb
|
75
|
+
- lib/simple_validate/validates_inclusion_of.rb
|
59
76
|
- lib/simple_validate/validates_length_of.rb
|
60
77
|
- lib/simple_validate/validates_numericality_of.rb
|
61
78
|
- lib/simple_validate/validates_presence_of.rb
|
79
|
+
- lib/simple_validate/validates_set_base.rb
|
62
80
|
- lib/simple_validate/version.rb
|
63
81
|
- simple_validate.gemspec
|
64
82
|
homepage: https://github.com/nikkypx/simple_validate
|
@@ -81,9 +99,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
99
|
version: '0'
|
82
100
|
requirements: []
|
83
101
|
rubyforge_project:
|
84
|
-
rubygems_version: 2.
|
102
|
+
rubygems_version: 2.7.3
|
85
103
|
signing_key:
|
86
104
|
specification_version: 4
|
87
105
|
summary: Validations for any plain old ruby object
|
88
106
|
test_files: []
|
89
|
-
has_rdoc:
|