lotus-validations 0.3.3 → 0.4.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 +4 -4
- data/CHANGELOG.md +7 -0
- data/LICENSE.md +1 -1
- data/README.md +29 -1
- data/lib/lotus/validations/attribute.rb +9 -3
- data/lib/lotus/validations/attribute_definer.rb +2 -2
- data/lib/lotus/validations/blank_value_checker.rb +10 -0
- data/lib/lotus/validations/version.rb +1 -1
- data/lotus-validations.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3108e0f651b886a399aad4ebd34bbb2a7cfc405f
|
4
|
+
data.tar.gz: 4a195aba653bc52553cd6c6c7bd583d5db3ca2e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87ec3a0e492883bc32c64e6b754fb76bdec5080ab96135e70128db885c7aac7e10851d34fb25b7d551ba1b0437e4d131f0e0386fbc4d4191d559e7e7504d672f
|
7
|
+
data.tar.gz: 0f1f326609266e52b7626d9d558c8ad70bb7074cf390ff05d4fca97d6751ab5e5288e5c61d3a60f8fab158b5aa2f5b8cca765d6e1ca224493a794bd6aae32e0e
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# Lotus::Validations
|
2
2
|
Validations mixin for Ruby objects
|
3
3
|
|
4
|
+
## v0.4.0 - 2016-01-12
|
5
|
+
## Changed
|
6
|
+
- [Hélio Costa e Silva & Luca Guidi] Ignore blank values for format and size validation
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
- [Pascal Betz] Ensure acceptance validation to reject blank strings
|
10
|
+
|
4
11
|
## v0.3.3 - 2015-09-30
|
5
12
|
### Added
|
6
13
|
- [Luca Guidi] Official support for JRuby 9k+
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -67,6 +67,34 @@ person.email # => "me@example.org"
|
|
67
67
|
person.age # => raises NoMethodError because `:age` wasn't defined as attribute.
|
68
68
|
```
|
69
69
|
|
70
|
+
#### Blank Values
|
71
|
+
|
72
|
+
The framework will treat as valid any blank attributes, __without__ `presence`, for both `format` and `size` predicates.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
require 'lotus/validations'
|
76
|
+
|
77
|
+
class Person
|
78
|
+
include Lotus::Validations
|
79
|
+
|
80
|
+
attribute :name, type: String, size: 5..45
|
81
|
+
attribute :email, type: String, size: 20..80, format: /@/
|
82
|
+
attribute :skills, type: Array, size: 1..3
|
83
|
+
attribute :keys, type: Hash, size: 1..3
|
84
|
+
end
|
85
|
+
|
86
|
+
Person.new.valid? # < true
|
87
|
+
Person.new(name: '').valid? # < true
|
88
|
+
Person.new(skills: '').valid? # < true
|
89
|
+
Person.new(skills: ['ruby', 'lotus']).valid? # < true
|
90
|
+
|
91
|
+
Person.new(skills: []).valid? # < false
|
92
|
+
Person.new(keys: {}).valid? # < false
|
93
|
+
Person.new(keys: {a: :b}, skills: []).valid? # < false
|
94
|
+
```
|
95
|
+
|
96
|
+
If you want to _disable_ this behaviour, please, refer to [presence](https://github.com/lotus/validations#presence).
|
97
|
+
|
70
98
|
### Validations
|
71
99
|
|
72
100
|
If you prefer Lotus::Validations to **only define validations**, but **not attributes**,
|
@@ -555,4 +583,4 @@ product.price # => 100
|
|
555
583
|
|
556
584
|
## Copyright
|
557
585
|
|
558
|
-
Copyright © 2014-
|
586
|
+
Copyright © 2014-2016 Luca Guidi – Released under MIT License
|
@@ -85,7 +85,7 @@ module Lotus
|
|
85
85
|
# This passes if the value is "truthy", it fails if not.
|
86
86
|
#
|
87
87
|
# Truthy examples: `Object.new`, `1`, `"1"`, `true`.
|
88
|
-
#
|
88
|
+
# Falsy examples: `nil`, `0`, `"0"`, `false`, `""`.
|
89
89
|
#
|
90
90
|
# @see Lotus::Validations::ClassMethods#attribute
|
91
91
|
# @see http://www.rubydoc.info/gems/lotus-utils/Lotus/Utils/Kernel#Boolean-class_method
|
@@ -93,7 +93,9 @@ module Lotus
|
|
93
93
|
# @since 0.2.0
|
94
94
|
# @api private
|
95
95
|
def acceptance
|
96
|
-
_validate(__method__)
|
96
|
+
_validate(__method__) do
|
97
|
+
!blank_value? && Lotus::Utils::Kernel.Boolean(@value)
|
98
|
+
end
|
97
99
|
end
|
98
100
|
|
99
101
|
# Validates format of the value.
|
@@ -106,7 +108,7 @@ module Lotus
|
|
106
108
|
# @since 0.2.0
|
107
109
|
# @api private
|
108
110
|
def format
|
109
|
-
_validate(__method__) {|matcher| @value.to_s.match(matcher) }
|
111
|
+
_validate(__method__) {|matcher| @value.to_s.match(matcher) } unless blank_value?
|
110
112
|
end
|
111
113
|
|
112
114
|
# Validates inclusion of the value in the defined collection.
|
@@ -171,6 +173,8 @@ module Lotus
|
|
171
173
|
#
|
172
174
|
# If the quantity is a Range, the size of the value MUST be included.
|
173
175
|
#
|
176
|
+
# If the attribute's value is blank, the size will not be considered
|
177
|
+
#
|
174
178
|
# The value is an object which implements `#size`.
|
175
179
|
#
|
176
180
|
# @raise [ArgumentError] if the defined quantity isn't a Numeric or a
|
@@ -181,6 +185,8 @@ module Lotus
|
|
181
185
|
# @since 0.2.0
|
182
186
|
# @api private
|
183
187
|
def size
|
188
|
+
return if blank_value?
|
189
|
+
|
184
190
|
_validate(__method__) do |validator|
|
185
191
|
case validator
|
186
192
|
when Numeric, ->(v) { v.respond_to?(:to_int) }
|
@@ -76,8 +76,8 @@ module Lotus
|
|
76
76
|
# @param name [#to_sym] the name of the attribute
|
77
77
|
# @param options [Hash] optional set of validations
|
78
78
|
# @option options [Class] :type the Ruby type used to coerce the value
|
79
|
-
# @option options [TrueClass,FalseClass] :acceptance requires
|
80
|
-
#
|
79
|
+
# @option options [TrueClass,FalseClass] :acceptance requires a non-empty
|
80
|
+
# value which coerces to TrueClass
|
81
81
|
# @option options [TrueClass,FalseClass] :confirmation requires the value
|
82
82
|
# to be confirmed twice
|
83
83
|
# @option options [#include?] :exclusion requires the value NOT be
|
@@ -38,8 +38,18 @@ module Lotus
|
|
38
38
|
# @since 0.2.2
|
39
39
|
# @api private
|
40
40
|
def _empty_value?
|
41
|
+
return false if _enumerable?
|
41
42
|
(@value.respond_to?(:empty?) and @value.empty?)
|
42
43
|
end
|
44
|
+
|
45
|
+
# Collectable classes should not be considered as blank value
|
46
|
+
# even if it's responds _true_ to its own `empty?` method.
|
47
|
+
#
|
48
|
+
# @since 0.4.0
|
49
|
+
# @api private
|
50
|
+
def _enumerable?
|
51
|
+
@value.respond_to?(:each)
|
52
|
+
end
|
43
53
|
end
|
44
54
|
end
|
45
55
|
end
|
data/lotus-validations.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
spec.required_ruby_version = '>= 2.0.0'
|
21
21
|
|
22
|
-
spec.add_dependency 'lotus-utils', '~> 0.
|
22
|
+
spec.add_dependency 'lotus-utils', '~> 0.6'
|
23
23
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
25
25
|
spec.add_development_dependency 'minitest', '~> 5'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lotus-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: lotus-utils
|
@@ -18,14 +18,14 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - "~>"
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '0.
|
21
|
+
version: '0.6'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - "~>"
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '0.
|
28
|
+
version: '0.6'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: bundler
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
version: '0'
|
114
114
|
requirements: []
|
115
115
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.
|
116
|
+
rubygems_version: 2.5.1
|
117
117
|
signing_key:
|
118
118
|
specification_version: 4
|
119
119
|
summary: Validations mixin for Ruby objects
|