hydra-validations 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/hydra/validations.rb +8 -0
- data/lib/hydra/validations/enumerable_behavior.rb +9 -3
- data/lib/hydra/validations/version.rb +1 -1
- data/spec/support/shared_examples_for_validators.rb +76 -0
- data/spec/validators/format_validator_spec.rb +31 -17
- data/spec/validators/inclusion_validator_spec.rb +29 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14a1051a97f663c054e73db5a4f50c90a45a447a
|
4
|
+
data.tar.gz: d7ffb762b468269fc1275604c50bacdabb05f83b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ceca377220db7fc7391c1d8bc2cc4729579b74c8fc9508cc11f60a98f3a79aff6d08c7480f4218e8e6f39811a1ba06f7b39fd83255ce8e72cd249865a7b69d05
|
7
|
+
data.tar.gz: 4c24af4ec23bfddbb3635486be91d310d2acd4c8225bdc17166b06be0f90ea1ef57e59105dcedba9f6106f53e60e53d0f1820d1db149d867a25bf187d7500f46
|
data/README.md
CHANGED
@@ -111,9 +111,9 @@ class UniquenessValidatable < ActiveFedora::Base
|
|
111
111
|
end
|
112
112
|
```
|
113
113
|
|
114
|
-
###
|
114
|
+
### SingleCardinalityValidator
|
115
115
|
|
116
|
-
Validates that the attribute value is a
|
116
|
+
Validates that the attribute value is a scalar or single-member enumerable.
|
117
117
|
|
118
118
|
```ruby
|
119
119
|
class Validatable
|
data/lib/hydra/validations.rb
CHANGED
@@ -10,6 +10,14 @@ module Hydra
|
|
10
10
|
include HelperMethods
|
11
11
|
end
|
12
12
|
|
13
|
+
module ClassMethods
|
14
|
+
protected
|
15
|
+
# Overwrites ActiveModel::Validations::ClassMethods, adding :allow_empty
|
16
|
+
def _validates_default_keys
|
17
|
+
[:if, :unless, :on, :allow_blank, :allow_nil , :strict, :allow_empty]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
end
|
14
22
|
end
|
15
23
|
|
@@ -7,10 +7,18 @@ module Hydra
|
|
7
7
|
# Behavior includes 'fixing' each error message to include the specific value
|
8
8
|
# which was invalid.
|
9
9
|
#
|
10
|
+
# `allow_empty` option can be used instead of `allow_blank` for greater
|
11
|
+
# clarity and precision with enumerable values. If a validator includes
|
12
|
+
# this mixin, then an empty enumerable value will always be invalid if
|
13
|
+
# `allow_empty` is false or nil.
|
14
|
+
#
|
10
15
|
module EnumerableBehavior
|
11
16
|
|
12
17
|
def validate_each(record, attribute, value)
|
13
|
-
|
18
|
+
return super unless value.respond_to?(:each)
|
19
|
+
if value.respond_to?(:empty?) && value.empty?
|
20
|
+
record.errors.add(attribute, "can't be empty") unless options[:allow_empty]
|
21
|
+
else
|
14
22
|
value.each do |v|
|
15
23
|
prev = record.errors[attribute].size rescue 0
|
16
24
|
super(record, attribute, v)
|
@@ -19,8 +27,6 @@ module Hydra
|
|
19
27
|
record.errors.add(attribute, fixed_message(v, messages.pop))
|
20
28
|
end
|
21
29
|
end
|
22
|
-
else
|
23
|
-
super
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
@@ -31,3 +31,79 @@ shared_examples "it validates the single cardinality of a scalar attribute" do
|
|
31
31
|
expect(subject).to be_valid
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
35
|
+
shared_examples "an enumerable validator" do
|
36
|
+
|
37
|
+
let(:record) { record_class.new }
|
38
|
+
|
39
|
+
before do
|
40
|
+
record_class.validates attribute, opts
|
41
|
+
allow(record).to receive(attribute) { value }
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when the value is nil" do
|
45
|
+
let(:value) { nil }
|
46
|
+
context "and `allow_nil` is true" do
|
47
|
+
let(:opts) { options.merge(allow_nil: true) }
|
48
|
+
it "should be valid" do
|
49
|
+
expect(record).to be_valid
|
50
|
+
end
|
51
|
+
end
|
52
|
+
context "and `allow_nil` is not true" do
|
53
|
+
let(:opts) { options }
|
54
|
+
it "should validate the value" do
|
55
|
+
expect_any_instance_of(described_class).to receive(:validate_each).with(record, attribute, value)
|
56
|
+
record.valid?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when the value is a blank scalar" do
|
62
|
+
let(:value) { "" }
|
63
|
+
context "and `allow_blank` is true" do
|
64
|
+
let(:opts) { options.merge(allow_blank: true) }
|
65
|
+
it "should be valid" do
|
66
|
+
expect(record).to be_valid
|
67
|
+
end
|
68
|
+
end
|
69
|
+
context "and `allow_blank` is not true" do
|
70
|
+
let(:opts) { options }
|
71
|
+
it "should validate the value" do
|
72
|
+
expect_any_instance_of(described_class).to receive(:validate_each).with(record, attribute, value)
|
73
|
+
record.valid?
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when the value is is a non-blank scalar" do
|
79
|
+
let(:value) { "foo" }
|
80
|
+
let(:opts) { options }
|
81
|
+
it "should validate the value" do
|
82
|
+
expect_any_instance_of(described_class).to receive(:validate_each).with(record, attribute, value)
|
83
|
+
record.valid?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when the value is an empty enumerable" do
|
88
|
+
let(:value) { [] }
|
89
|
+
context "and `allow_blank` is true" do
|
90
|
+
let(:opts) { options.merge(allow_blank: true) }
|
91
|
+
it "should be valid" do
|
92
|
+
expect(record).to be_valid
|
93
|
+
end
|
94
|
+
end
|
95
|
+
context "and `allow_empty` is true" do
|
96
|
+
let(:opts) { options.merge(allow_empty: true) }
|
97
|
+
it "should be valid" do
|
98
|
+
expect(record).to be_valid
|
99
|
+
end
|
100
|
+
end
|
101
|
+
context "and neither `allow_blank` nor `allow_empty` is true" do
|
102
|
+
let(:opts) { options }
|
103
|
+
it "should be invalid" do
|
104
|
+
expect(record).to be_invalid
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -1,25 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'support/shared_examples_for_validators'
|
2
3
|
|
3
4
|
shared_examples "it validates the format of each member of the attribute value" do
|
4
5
|
context "all attribute value members are valid" do
|
5
|
-
before {
|
6
|
+
before { record.field = ["foo", "bar"] }
|
6
7
|
it "should be valid" do
|
7
|
-
expect(
|
8
|
+
expect(record).to be_valid
|
8
9
|
end
|
9
10
|
end
|
11
|
+
|
10
12
|
context "one of the attribute value members is invalid" do
|
11
|
-
before {
|
13
|
+
before { record.field = ["foo1", "bar"] }
|
12
14
|
it "should be invalid" do
|
13
|
-
expect(
|
15
|
+
expect(record).to be_invalid
|
14
16
|
end
|
15
17
|
it "should 'fix' the error message to include the value" do
|
16
|
-
|
17
|
-
expect(
|
18
|
+
record.valid?
|
19
|
+
expect(record.errors[:field]).to eq ["value \"foo1\" is invalid"]
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
22
24
|
describe Hydra::Validations::FormatValidator do
|
25
|
+
|
23
26
|
before(:all) do
|
24
27
|
class Validatable
|
25
28
|
include ActiveModel::Validations
|
@@ -27,21 +30,32 @@ describe Hydra::Validations::FormatValidator do
|
|
27
30
|
attr_accessor :field
|
28
31
|
end
|
29
32
|
end
|
33
|
+
|
30
34
|
before(:each) { Validatable.clear_validators! }
|
35
|
+
|
31
36
|
after(:all) { Object.send(:remove_const, :Validatable) }
|
32
|
-
|
33
|
-
describe "
|
34
|
-
|
35
|
-
Validatable.
|
36
|
-
|
37
|
+
|
38
|
+
describe "class methods" do
|
39
|
+
describe ".validates" do
|
40
|
+
before { Validatable.validates :field, format: { with: /\A[[:alpha:]]+\Z/ } }
|
41
|
+
it_behaves_like "it validates the format of each member of the attribute value" do
|
42
|
+
let(:record) { Validatable.new }
|
43
|
+
end
|
37
44
|
end
|
38
|
-
it_behaves_like "it validates the format of each member of the attribute value"
|
39
45
|
end
|
40
|
-
describe "
|
41
|
-
|
42
|
-
Validatable.
|
43
|
-
|
46
|
+
describe "helper methods" do
|
47
|
+
describe ".validates_format_of" do
|
48
|
+
before { Validatable.validates_format_of :field, with: /\A[[:alpha:]]+\Z/ }
|
49
|
+
it_behaves_like "it validates the format of each member of the attribute value" do
|
50
|
+
let(:record) { Validatable.new }
|
51
|
+
end
|
44
52
|
end
|
45
|
-
it_behaves_like "it validates the format of each member of the attribute value"
|
46
53
|
end
|
54
|
+
|
55
|
+
it_behaves_like "an enumerable validator" do
|
56
|
+
let(:options) { { format: { with: /\A[[:alpha:]]+\Z/ } } }
|
57
|
+
let(:record_class) { Validatable }
|
58
|
+
let(:attribute) { :field }
|
59
|
+
end
|
60
|
+
|
47
61
|
end
|
@@ -1,25 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'support/shared_examples_for_validators'
|
2
3
|
|
3
4
|
shared_examples "it validates the inclusion of each member of the attribute value" do
|
5
|
+
|
4
6
|
context "all attribute value members are valid" do
|
5
|
-
before {
|
7
|
+
before { record.field = ["foo", "bar"] }
|
6
8
|
it "should be valid" do
|
7
|
-
expect(
|
9
|
+
expect(record).to be_valid
|
8
10
|
end
|
9
11
|
end
|
12
|
+
|
10
13
|
context "one of the attribute value members is invalid" do
|
11
|
-
before {
|
14
|
+
before { record.field = ["foo1", "bar"] }
|
12
15
|
it "should be invalid" do
|
13
|
-
expect(
|
16
|
+
expect(record).to be_invalid
|
14
17
|
end
|
15
18
|
it "should 'fix' the error message to include the value" do
|
16
|
-
|
17
|
-
expect(
|
19
|
+
record.valid?
|
20
|
+
expect(record.errors[:field]).to eq ["value \"foo1\" is not included in the list"]
|
18
21
|
end
|
19
22
|
end
|
23
|
+
|
20
24
|
end
|
21
25
|
|
22
26
|
describe Hydra::Validations::InclusionValidator do
|
27
|
+
|
23
28
|
before(:all) do
|
24
29
|
class Validatable
|
25
30
|
include ActiveModel::Validations
|
@@ -27,22 +32,31 @@ describe Hydra::Validations::InclusionValidator do
|
|
27
32
|
attr_accessor :field
|
28
33
|
end
|
29
34
|
end
|
35
|
+
|
30
36
|
before(:each) { Validatable.clear_validators! }
|
37
|
+
|
31
38
|
after(:all) { Object.send(:remove_const, :Validatable) }
|
32
|
-
|
39
|
+
|
33
40
|
let(:valid_values) { ["foo", "bar", "baz"] }
|
41
|
+
|
34
42
|
describe ".validates" do
|
35
|
-
before
|
36
|
-
|
37
|
-
|
43
|
+
before { Validatable.validates :field, inclusion: { in: valid_values } }
|
44
|
+
it_behaves_like "it validates the inclusion of each member of the attribute value" do
|
45
|
+
let(:record) { Validatable.new }
|
38
46
|
end
|
39
|
-
it_behaves_like "it validates the inclusion of each member of the attribute value"
|
40
47
|
end
|
48
|
+
|
41
49
|
describe ".validates_inclusion_of" do
|
42
|
-
before
|
43
|
-
|
44
|
-
|
50
|
+
before { Validatable.validates_inclusion_of :field, in: valid_values }
|
51
|
+
it_behaves_like "it validates the inclusion of each member of the attribute value" do
|
52
|
+
let(:record) { Validatable.new }
|
45
53
|
end
|
46
|
-
it_behaves_like "it validates the inclusion of each member of the attribute value"
|
47
54
|
end
|
55
|
+
|
56
|
+
it_behaves_like "an enumerable validator" do
|
57
|
+
let(:options) { { inclusion: { in: valid_values } } }
|
58
|
+
let(:record_class) { Validatable }
|
59
|
+
let(:attribute) { :field }
|
60
|
+
end
|
61
|
+
|
48
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hydra-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dchandekstark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08
|
11
|
+
date: 2014-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|