subvalid 0.1.0 → 0.2.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 +5 -5
- data/.travis.yml +7 -4
- data/README.md +29 -0
- data/lib/subvalid/validators/length_validator.rb +10 -2
- data/lib/subvalid/version.rb +1 -1
- data/spec/subvalid/validator_spec.rb +0 -7
- data/spec/subvalid/validators/length_validator_spec.rb +179 -0
- data/subvalid.gemspec +2 -2
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c0653675ee971b2059d362155fd30634850d423b4137cbde73d65516b7ba599b
|
4
|
+
data.tar.gz: e95bcb0dde03e2be59337374573928b70a5ff16f7bf801d1922b4d6cce98e8de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3547b4e243cb436ba8440804932a94abdc1b242f8fd0dd7692864ee35d0964c15ddf5d900438b81bfee4327d5b60767e78daf0631c31dc967aab537dbdd5a542
|
7
|
+
data.tar.gz: f1309e4d68890d1df8fc40a54d61f7099f0c830ed4a1bb813a1bba8fb389ff5e953ff433b22c1e83f371af2fa0cb1306a577a91224dbf2785d9e6f740fb334ea
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -164,6 +164,35 @@ class PersonValidator
|
|
164
164
|
end
|
165
165
|
```
|
166
166
|
|
167
|
+
### Length Options
|
168
|
+
|
169
|
+
You can specify length constraints in different ways:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
class PersonValidator
|
173
|
+
include Subvalid::Validator
|
174
|
+
|
175
|
+
validates :name, length: { minimum: 2 }
|
176
|
+
validates :bio, length: { maximum: 500 }
|
177
|
+
validates :password, length: { in: 6..20 }
|
178
|
+
validates :registration_number, length: { is: 6 }
|
179
|
+
end
|
180
|
+
```
|
181
|
+
|
182
|
+
The possible length constraint options are:
|
183
|
+
|
184
|
+
`:minimum` - The attribute cannot have less than the specified length.
|
185
|
+
|
186
|
+
`:maximum` - The attribute cannot have more than the specified length.
|
187
|
+
|
188
|
+
`:in` (or `:within`) - The attribute length must be included in a given interval. The value for this option must be a range.
|
189
|
+
|
190
|
+
`:is` - The attribute length must be equal to the given value.
|
191
|
+
|
192
|
+
The default error messages depend on the type of length validation being performed. You can use the `:message` option to specify an error message.
|
193
|
+
|
194
|
+
Note that the default error messages are plural. A personalised message should be provided in cases where it is grammatically incorrect, eg, "cannot be shorter than 1 characters".
|
195
|
+
|
167
196
|
## Contact
|
168
197
|
|
169
198
|
- [github project](https://github.com/envato/subvalid)
|
@@ -4,11 +4,19 @@ module Subvalid
|
|
4
4
|
def self.validate(object, validation_result=ValidationResult.new, *args)
|
5
5
|
return unless object
|
6
6
|
args = args.to_h
|
7
|
+
message = args.delete(:message)
|
7
8
|
args.each do |operator, value|
|
8
9
|
case operator
|
10
|
+
when :minimum
|
11
|
+
validation_result.add_error(message || "cannot be shorter than #{value} characters") if object.size < value
|
9
12
|
when :maximum
|
10
|
-
validation_result.add_error("
|
11
|
-
|
13
|
+
validation_result.add_error(message || "cannot be longer than #{value} characters") if object.size > value
|
14
|
+
when :is
|
15
|
+
validation_result.add_error(message || "should have exactly #{value} characters") if object.size != value
|
16
|
+
when :in, :within
|
17
|
+
unless value.include?(object.size)
|
18
|
+
validation_result.add_error(message || "should contain #{value.first} to #{value.last} characters")
|
19
|
+
end
|
12
20
|
else
|
13
21
|
raise "don't know what to do with operator=#{operator}"
|
14
22
|
end
|
data/lib/subvalid/version.rb
CHANGED
@@ -17,13 +17,6 @@ describe Subvalid::Validator do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
class TestValidator
|
21
|
-
def self.validate(object, validation_result=ValidationResult.new, *args)
|
22
|
-
validation_result.add_error("testing #{object}")
|
23
|
-
end
|
24
|
-
end
|
25
|
-
Subvalid::ValidatorRegistry.register(:test, TestValidator)
|
26
|
-
|
27
20
|
Poro = Struct.new(:foo, :bar, :child, :some_predicate) do
|
28
21
|
def to_s
|
29
22
|
"I'M A PORO"
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Subvalid::Validators::LengthValidator do
|
4
|
+
Thing = Struct.new(:list)
|
5
|
+
let(:thing) { Thing.new(list) }
|
6
|
+
let(:list) { [] }
|
7
|
+
|
8
|
+
describe '#validate' do
|
9
|
+
context 'when attribute is a string' do
|
10
|
+
context 'with minimum' do
|
11
|
+
class MinValidator
|
12
|
+
include Subvalid::Validator
|
13
|
+
validates :list, length: {minimum: 2}
|
14
|
+
end
|
15
|
+
|
16
|
+
subject(:validator) { MinValidator.validate(thing) }
|
17
|
+
|
18
|
+
it 'returns a validation result' do
|
19
|
+
expect(validator).to be_a(Subvalid::ValidationResult)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when the length is just right' do
|
23
|
+
let(:list) { 'a' * 3 }
|
24
|
+
it { is_expected.to be_valid }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when the length is too short' do
|
28
|
+
it { is_expected.not_to be_valid }
|
29
|
+
|
30
|
+
it 'shows the default error message' do
|
31
|
+
expect(validator.children[:list].errors).to include("cannot be shorter than 2 characters")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'with maximum' do
|
37
|
+
class MaxValidator
|
38
|
+
include Subvalid::Validator
|
39
|
+
validates :list, length: {maximum: 10}
|
40
|
+
end
|
41
|
+
|
42
|
+
subject(:validator) { MaxValidator.validate(thing) }
|
43
|
+
|
44
|
+
it 'returns a validation result' do
|
45
|
+
expect(validator).to be_a(Subvalid::ValidationResult)
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when the length is just right' do
|
49
|
+
it { is_expected.to be_valid }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when the length is too long' do
|
53
|
+
let(:list) { 'a' * 30 }
|
54
|
+
it { is_expected.not_to be_valid }
|
55
|
+
|
56
|
+
it 'shows the default error message' do
|
57
|
+
expect(validator.children[:list].errors).to include("cannot be longer than 10 characters")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with exact character count' do
|
63
|
+
class ExactLengthValidator
|
64
|
+
include Subvalid::Validator
|
65
|
+
validates :list, length: {is: 4}
|
66
|
+
end
|
67
|
+
|
68
|
+
subject(:validator) { ExactLengthValidator.validate(thing) }
|
69
|
+
|
70
|
+
it 'returns a validation result' do
|
71
|
+
expect(validator).to be_a(Subvalid::ValidationResult)
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'when the length is just right' do
|
75
|
+
let(:list) { 'a' * 4 }
|
76
|
+
it { is_expected.to be_valid }
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'when the length is too short' do
|
80
|
+
it { is_expected.not_to be_valid }
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when the length is too long' do
|
84
|
+
let(:list) { 'a' * 5 }
|
85
|
+
it { is_expected.not_to be_valid }
|
86
|
+
|
87
|
+
it 'shows the default error message' do
|
88
|
+
expect(validator.children[:list].errors).to include("should have exactly 4 characters")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'with a range' do
|
94
|
+
context 'when using within' do
|
95
|
+
class WithinValidator
|
96
|
+
include Subvalid::Validator
|
97
|
+
validates :list, length: {within: 2..4}
|
98
|
+
end
|
99
|
+
|
100
|
+
subject(:validator) { WithinValidator.validate(thing) }
|
101
|
+
|
102
|
+
it 'returns a validation result' do
|
103
|
+
expect(validator).to be_a(Subvalid::ValidationResult)
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when the length is just right' do
|
107
|
+
let(:list) { 'a' * 3 }
|
108
|
+
it { is_expected.to be_valid }
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'when the length is too short' do
|
112
|
+
it { is_expected.not_to be_valid }
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'when the length is too long' do
|
116
|
+
let(:list) { 'a' * 5 }
|
117
|
+
it { is_expected.not_to be_valid }
|
118
|
+
|
119
|
+
it 'shows the default error message' do
|
120
|
+
expect(validator.children[:list].errors).to include("should contain 2 to 4 characters")
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'when using in' do
|
126
|
+
class InValidator
|
127
|
+
include Subvalid::Validator
|
128
|
+
validates :list, length: {in: 2..4}
|
129
|
+
end
|
130
|
+
|
131
|
+
subject(:validator) { InValidator.validate(thing) }
|
132
|
+
|
133
|
+
it 'returns a validation result' do
|
134
|
+
expect(validator).to be_a(Subvalid::ValidationResult)
|
135
|
+
end
|
136
|
+
|
137
|
+
context 'when the length is just right' do
|
138
|
+
let(:list) { 'a' * 3 }
|
139
|
+
it { is_expected.to be_valid }
|
140
|
+
end
|
141
|
+
|
142
|
+
context 'when the length is too short' do
|
143
|
+
it { is_expected.not_to be_valid }
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'when the length is too long' do
|
147
|
+
let(:list) { 'a' * 5 }
|
148
|
+
it { is_expected.not_to be_valid }
|
149
|
+
|
150
|
+
it 'shows the default error message' do
|
151
|
+
expect(validator.children[:list].errors).to include("should contain 2 to 4 characters")
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'when attribute is an array' do
|
159
|
+
context 'with custom message' do
|
160
|
+
class MaxWithMessageValidator
|
161
|
+
include Subvalid::Validator
|
162
|
+
validates :list, length: {maximum: 4, message: "cannot contain more than 5 items"}
|
163
|
+
end
|
164
|
+
|
165
|
+
subject(:validator) { MaxWithMessageValidator.validate(thing) }
|
166
|
+
|
167
|
+
context 'when there are too many elements' do
|
168
|
+
context 'with provided message' do
|
169
|
+
let(:list) { [:a] * 5 }
|
170
|
+
|
171
|
+
it 'shows custom error message' do
|
172
|
+
expect(validator.children[:list].errors).to include("cannot contain more than 5 items")
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
data/subvalid.gemspec
CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "bundler"
|
22
|
-
spec.add_development_dependency "rake"
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
24
|
spec.add_development_dependency "pry"
|
25
25
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subvalid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Doherty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
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: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
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
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- spec/subvalid/validation_result_spec.rb
|
101
101
|
- spec/subvalid/validator_registry_spec.rb
|
102
102
|
- spec/subvalid/validator_spec.rb
|
103
|
+
- spec/subvalid/validators/length_validator_spec.rb
|
103
104
|
- spec/subvalid_spec.rb
|
104
105
|
- subvalid.gemspec
|
105
106
|
homepage: ''
|
@@ -121,8 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
122
|
- !ruby/object:Gem::Version
|
122
123
|
version: '0'
|
123
124
|
requirements: []
|
124
|
-
|
125
|
-
rubygems_version: 2.5.1
|
125
|
+
rubygems_version: 3.0.1
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Subjective validation for Plain Old Ruby Objects
|
@@ -131,4 +131,5 @@ test_files:
|
|
131
131
|
- spec/subvalid/validation_result_spec.rb
|
132
132
|
- spec/subvalid/validator_registry_spec.rb
|
133
133
|
- spec/subvalid/validator_spec.rb
|
134
|
+
- spec/subvalid/validators/length_validator_spec.rb
|
134
135
|
- spec/subvalid_spec.rb
|