hash_validator 2.0.0 → 2.0.1
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/.github/workflows/ruby.yml +6 -1
- data/.rubocop.yml +340 -0
- data/Gemfile +3 -1
- data/README.md +2 -1
- data/Rakefile +6 -4
- data/hash_validator.gemspec +11 -5
- data/lib/hash_validator/base.rb +2 -0
- data/lib/hash_validator/configuration.rb +5 -3
- data/lib/hash_validator/validations/many.rb +2 -0
- data/lib/hash_validator/validations/multiple.rb +2 -0
- data/lib/hash_validator/validations/optional.rb +2 -0
- data/lib/hash_validator/validations.rb +5 -3
- data/lib/hash_validator/validators/alpha_validator.rb +5 -3
- data/lib/hash_validator/validators/alphanumeric_validator.rb +5 -3
- data/lib/hash_validator/validators/array_validator.rb +44 -48
- data/lib/hash_validator/validators/base.rb +6 -4
- data/lib/hash_validator/validators/boolean_validator.rb +3 -1
- data/lib/hash_validator/validators/class_validator.rb +3 -1
- data/lib/hash_validator/validators/digits_validator.rb +5 -3
- data/lib/hash_validator/validators/dynamic_func_validator.rb +8 -8
- data/lib/hash_validator/validators/dynamic_pattern_validator.rb +5 -3
- data/lib/hash_validator/validators/email_validator.rb +4 -2
- data/lib/hash_validator/validators/enumerable_validator.rb +4 -2
- data/lib/hash_validator/validators/hash_validator.rb +5 -3
- data/lib/hash_validator/validators/hex_color_validator.rb +5 -3
- data/lib/hash_validator/validators/ip_validator.rb +6 -4
- data/lib/hash_validator/validators/ipv4_validator.rb +5 -3
- data/lib/hash_validator/validators/ipv6_validator.rb +6 -4
- data/lib/hash_validator/validators/json_validator.rb +6 -4
- data/lib/hash_validator/validators/lambda_validator.rb +4 -2
- data/lib/hash_validator/validators/many_validator.rb +3 -1
- data/lib/hash_validator/validators/multiple_validator.rb +4 -2
- data/lib/hash_validator/validators/optional_validator.rb +3 -1
- data/lib/hash_validator/validators/presence_validator.rb +4 -2
- data/lib/hash_validator/validators/regex_validator.rb +4 -2
- data/lib/hash_validator/validators/simple_type_validators.rb +3 -1
- data/lib/hash_validator/validators/simple_validator.rb +2 -0
- data/lib/hash_validator/validators/url_validator.rb +6 -4
- data/lib/hash_validator/validators.rb +35 -33
- data/lib/hash_validator/version.rb +3 -1
- data/lib/hash_validator.rb +7 -5
- data/spec/configuration_spec.rb +76 -74
- data/spec/hash_validator_spec.rb +132 -113
- data/spec/hash_validator_spec_helper.rb +2 -0
- data/spec/spec_helper.rb +14 -4
- data/spec/validators/alpha_validator_spec.rb +43 -41
- data/spec/validators/alphanumeric_validator_spec.rb +44 -42
- data/spec/validators/array_spec.rb +102 -47
- data/spec/validators/base_spec.rb +25 -10
- data/spec/validators/boolean_spec.rb +15 -13
- data/spec/validators/class_spec.rb +20 -18
- data/spec/validators/digits_validator_spec.rb +46 -44
- data/spec/validators/dynamic_func_validator_spec.rb +90 -88
- data/spec/validators/dynamic_pattern_validator_spec.rb +65 -63
- data/spec/validators/email_spec.rb +15 -13
- data/spec/validators/hash_validator_spec.rb +39 -37
- data/spec/validators/hex_color_validator_spec.rb +49 -47
- data/spec/validators/in_enumerable_spec.rb +32 -30
- data/spec/validators/ip_validator_spec.rb +46 -44
- data/spec/validators/ipv4_validator_spec.rb +45 -43
- data/spec/validators/ipv6_validator_spec.rb +44 -42
- data/spec/validators/json_validator_spec.rb +38 -36
- data/spec/validators/lambda_spec.rb +20 -18
- data/spec/validators/many_spec.rb +25 -23
- data/spec/validators/multiple_spec.rb +13 -11
- data/spec/validators/optional_spec.rb +22 -20
- data/spec/validators/presence_spec.rb +16 -14
- data/spec/validators/regexp_spec.rb +14 -12
- data/spec/validators/simple_spec.rb +17 -15
- data/spec/validators/simple_types_spec.rb +21 -19
- data/spec/validators/url_validator_spec.rb +34 -32
- data/spec/validators/user_defined_spec.rb +29 -27
- metadata +59 -2
@@ -1,150 +1,152 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe HashValidator::Validator::DynamicPatternValidator do
|
4
|
-
describe
|
6
|
+
describe "Pattern-based validator" do
|
5
7
|
let(:odd_pattern_validator) do
|
6
8
|
HashValidator::Validator::DynamicPatternValidator.new(
|
7
|
-
|
9
|
+
"odd_pattern",
|
8
10
|
/\A\d*[13579]\z/,
|
9
|
-
|
11
|
+
"is not an odd number"
|
10
12
|
)
|
11
13
|
end
|
12
14
|
|
13
15
|
let(:postal_code_validator) do
|
14
16
|
HashValidator::Validator::DynamicPatternValidator.new(
|
15
|
-
|
17
|
+
"us_postal",
|
16
18
|
/\A\d{5}(-\d{4})?\z/,
|
17
|
-
|
19
|
+
"is not a valid US postal code"
|
18
20
|
)
|
19
21
|
end
|
20
22
|
|
21
23
|
let(:errors) { Hash.new }
|
22
24
|
|
23
|
-
describe
|
24
|
-
it
|
25
|
+
describe "#initialize" do
|
26
|
+
it "requires a valid regular expression" do
|
25
27
|
expect {
|
26
|
-
HashValidator::Validator::DynamicPatternValidator.new(
|
27
|
-
}.to raise_error(ArgumentError,
|
28
|
+
HashValidator::Validator::DynamicPatternValidator.new("test", "not_a_regex")
|
29
|
+
}.to raise_error(ArgumentError, "Pattern must be a regular expression")
|
28
30
|
end
|
29
31
|
|
30
|
-
it
|
32
|
+
it "accepts a custom error message" do
|
31
33
|
validator = HashValidator::Validator::DynamicPatternValidator.new(
|
32
|
-
|
34
|
+
"test",
|
33
35
|
/test/,
|
34
|
-
|
36
|
+
"custom error"
|
35
37
|
)
|
36
|
-
expect(validator.error_message).to eq(
|
38
|
+
expect(validator.error_message).to eq("custom error")
|
37
39
|
end
|
38
40
|
|
39
|
-
it
|
40
|
-
validator = HashValidator::Validator::DynamicPatternValidator.new(
|
41
|
-
expect(validator.error_message).to eq(
|
41
|
+
it "uses default error message when none provided" do
|
42
|
+
validator = HashValidator::Validator::DynamicPatternValidator.new("test", /test/)
|
43
|
+
expect(validator.error_message).to eq("test required")
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
45
|
-
describe
|
46
|
-
it
|
47
|
-
expect(odd_pattern_validator.should_validate?(
|
47
|
+
describe "#should_validate?" do
|
48
|
+
it "validates the correct name" do
|
49
|
+
expect(odd_pattern_validator.should_validate?("odd_pattern")).to eq true
|
48
50
|
end
|
49
51
|
|
50
|
-
it
|
51
|
-
expect(odd_pattern_validator.should_validate?(
|
52
|
-
expect(odd_pattern_validator.should_validate?(
|
52
|
+
it "does not validate other names" do
|
53
|
+
expect(odd_pattern_validator.should_validate?("even_pattern")).to eq false
|
54
|
+
expect(odd_pattern_validator.should_validate?("string")).to eq false
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
56
|
-
describe
|
57
|
-
context
|
58
|
-
it
|
59
|
-
odd_pattern_validator.validate(:key,
|
58
|
+
describe "#validate" do
|
59
|
+
context "with odd number pattern" do
|
60
|
+
it "validates odd numbers correctly" do
|
61
|
+
odd_pattern_validator.validate(:key, "1", {}, errors)
|
60
62
|
expect(errors).to be_empty
|
61
63
|
|
62
64
|
errors.clear
|
63
|
-
odd_pattern_validator.validate(:key,
|
65
|
+
odd_pattern_validator.validate(:key, "13", {}, errors)
|
64
66
|
expect(errors).to be_empty
|
65
67
|
|
66
68
|
errors.clear
|
67
|
-
odd_pattern_validator.validate(:key,
|
69
|
+
odd_pattern_validator.validate(:key, "999", {}, errors)
|
68
70
|
expect(errors).to be_empty
|
69
71
|
end
|
70
72
|
|
71
|
-
it
|
72
|
-
odd_pattern_validator.validate(:key,
|
73
|
-
expect(errors).to eq({ key:
|
73
|
+
it "rejects even numbers" do
|
74
|
+
odd_pattern_validator.validate(:key, "2", {}, errors)
|
75
|
+
expect(errors).to eq({ key: "is not an odd number" })
|
74
76
|
|
75
77
|
errors.clear
|
76
|
-
odd_pattern_validator.validate(:key,
|
77
|
-
expect(errors).to eq({ key:
|
78
|
+
odd_pattern_validator.validate(:key, "100", {}, errors)
|
79
|
+
expect(errors).to eq({ key: "is not an odd number" })
|
78
80
|
end
|
79
81
|
|
80
|
-
it
|
81
|
-
odd_pattern_validator.validate(:key,
|
82
|
-
expect(errors).to eq({ key:
|
82
|
+
it "rejects non-numeric strings" do
|
83
|
+
odd_pattern_validator.validate(:key, "abc", {}, errors)
|
84
|
+
expect(errors).to eq({ key: "is not an odd number" })
|
83
85
|
end
|
84
86
|
|
85
|
-
it
|
87
|
+
it "converts non-string values to strings" do
|
86
88
|
odd_pattern_validator.validate(:key, 13, {}, errors)
|
87
89
|
expect(errors).to be_empty
|
88
90
|
|
89
91
|
errors.clear
|
90
92
|
odd_pattern_validator.validate(:key, 14, {}, errors)
|
91
|
-
expect(errors).to eq({ key:
|
93
|
+
expect(errors).to eq({ key: "is not an odd number" })
|
92
94
|
end
|
93
95
|
end
|
94
96
|
|
95
|
-
context
|
96
|
-
it
|
97
|
-
postal_code_validator.validate(:key,
|
97
|
+
context "with postal code pattern" do
|
98
|
+
it "validates correct postal codes" do
|
99
|
+
postal_code_validator.validate(:key, "12345", {}, errors)
|
98
100
|
expect(errors).to be_empty
|
99
101
|
|
100
102
|
errors.clear
|
101
|
-
postal_code_validator.validate(:key,
|
103
|
+
postal_code_validator.validate(:key, "12345-6789", {}, errors)
|
102
104
|
expect(errors).to be_empty
|
103
105
|
end
|
104
106
|
|
105
|
-
it
|
106
|
-
postal_code_validator.validate(:key,
|
107
|
-
expect(errors).to eq({ key:
|
107
|
+
it "rejects invalid postal codes" do
|
108
|
+
postal_code_validator.validate(:key, "1234", {}, errors)
|
109
|
+
expect(errors).to eq({ key: "is not a valid US postal code" })
|
108
110
|
|
109
111
|
errors.clear
|
110
|
-
postal_code_validator.validate(:key,
|
111
|
-
expect(errors).to eq({ key:
|
112
|
+
postal_code_validator.validate(:key, "12345-678", {}, errors)
|
113
|
+
expect(errors).to eq({ key: "is not a valid US postal code" })
|
112
114
|
|
113
115
|
errors.clear
|
114
|
-
postal_code_validator.validate(:key,
|
115
|
-
expect(errors).to eq({ key:
|
116
|
+
postal_code_validator.validate(:key, "ABCDE", {}, errors)
|
117
|
+
expect(errors).to eq({ key: "is not a valid US postal code" })
|
116
118
|
end
|
117
119
|
end
|
118
120
|
end
|
119
121
|
|
120
|
-
describe
|
122
|
+
describe "Integration with HashValidator.add_validator" do
|
121
123
|
before do
|
122
|
-
HashValidator.add_validator(
|
123
|
-
pattern: /\A\d{3}-\d{3}-\d{4}\z/,
|
124
|
-
error_message:
|
124
|
+
HashValidator.add_validator("phone_number",
|
125
|
+
pattern: /\A\d{3}-\d{3}-\d{4}\z/,
|
126
|
+
error_message: "is not a valid phone number")
|
125
127
|
end
|
126
128
|
|
127
129
|
after do
|
128
|
-
HashValidator.remove_validator(
|
130
|
+
HashValidator.remove_validator("phone_number")
|
129
131
|
end
|
130
132
|
|
131
|
-
it
|
133
|
+
it "can be registered and used via add_validator" do
|
132
134
|
validator = HashValidator.validate(
|
133
|
-
{ phone:
|
134
|
-
{ phone:
|
135
|
+
{ phone: "555-123-4567" },
|
136
|
+
{ phone: "phone_number" }
|
135
137
|
)
|
136
138
|
expect(validator.valid?).to eq true
|
137
139
|
expect(validator.errors).to be_empty
|
138
140
|
end
|
139
141
|
|
140
|
-
it
|
142
|
+
it "returns errors for invalid values" do
|
141
143
|
validator = HashValidator.validate(
|
142
|
-
{ phone:
|
143
|
-
{ phone:
|
144
|
+
{ phone: "5551234567" },
|
145
|
+
{ phone: "phone_number" }
|
144
146
|
)
|
145
147
|
expect(validator.valid?).to eq false
|
146
|
-
expect(validator.errors).to eq({ phone:
|
148
|
+
expect(validator.errors).to eq({ phone: "is not a valid phone number" })
|
147
149
|
end
|
148
150
|
end
|
149
151
|
end
|
150
|
-
end
|
152
|
+
end
|
@@ -1,40 +1,42 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe HashValidator::Validator::Base do
|
4
6
|
let(:validator) { HashValidator::Validator::EmailValidator.new }
|
5
7
|
let(:errors) { Hash.new }
|
6
8
|
|
7
|
-
describe
|
9
|
+
describe "#should_validate?" do
|
8
10
|
it 'should validate the name "email"' do
|
9
|
-
expect(validator.should_validate?(
|
11
|
+
expect(validator.should_validate?("email")).to eq true
|
10
12
|
end
|
11
13
|
|
12
|
-
it
|
13
|
-
expect(validator.should_validate?(
|
14
|
-
expect(validator.should_validate?(
|
14
|
+
it "should not validate other names" do
|
15
|
+
expect(validator.should_validate?("string")).to eq false
|
16
|
+
expect(validator.should_validate?("array")).to eq false
|
15
17
|
expect(validator.should_validate?(nil)).to eq false
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
describe
|
20
|
-
it
|
21
|
+
describe "#validate" do
|
22
|
+
it "should validate an email with true" do
|
21
23
|
validator.validate(:key, "johndoe@gmail.com", {}, errors)
|
22
24
|
|
23
25
|
expect(errors).to be_empty
|
24
26
|
end
|
25
27
|
|
26
|
-
it
|
27
|
-
validator.validate(:key,
|
28
|
+
it "should validate a string without an @ symbol with false" do
|
29
|
+
validator.validate(:key, "test", {}, errors)
|
28
30
|
|
29
31
|
expect(errors).not_to be_empty
|
30
|
-
expect(errors).to eq({ key:
|
32
|
+
expect(errors).to eq({ key: "is not a valid email" })
|
31
33
|
end
|
32
34
|
|
33
|
-
it
|
35
|
+
it "should validate a number with false" do
|
34
36
|
validator.validate(:key, 123, {}, errors)
|
35
37
|
|
36
38
|
expect(errors).not_to be_empty
|
37
|
-
expect(errors).to eq({ key:
|
39
|
+
expect(errors).to eq({ key: "is not a valid email" })
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "ActionController::Parameters support" do
|
4
6
|
# Mock ActionController::Parameters for testing
|
5
7
|
let(:mock_params_class) do
|
6
8
|
Class.new do
|
@@ -23,12 +25,12 @@ describe 'ActionController::Parameters support' do
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def is_a?(klass)
|
26
|
-
return true if klass.name ==
|
28
|
+
return true if klass.name == "ActionController::Parameters"
|
27
29
|
super
|
28
30
|
end
|
29
31
|
|
30
32
|
def class
|
31
|
-
|
33
|
+
Struct.new(:name).new("ActionController::Parameters")
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
@@ -41,62 +43,62 @@ describe 'ActionController::Parameters support' do
|
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
44
|
-
it
|
45
|
-
params = ActionController::Parameters.new({ name:
|
46
|
-
validations = { name:
|
47
|
-
|
46
|
+
it "should validate ActionController::Parameters objects" do
|
47
|
+
params = ActionController::Parameters.new({ name: "John", age: 30 })
|
48
|
+
validations = { name: "string", age: "integer" }
|
49
|
+
|
48
50
|
validator = HashValidator.validate(params, validations)
|
49
|
-
|
51
|
+
|
50
52
|
expect(validator.valid?).to be true
|
51
53
|
expect(validator.errors).to be_empty
|
52
54
|
end
|
53
55
|
|
54
|
-
it
|
55
|
-
nested_params = ActionController::Parameters.new({ theme:
|
56
|
-
params = ActionController::Parameters.new({
|
57
|
-
name:
|
56
|
+
it "should handle nested ActionController::Parameters" do
|
57
|
+
nested_params = ActionController::Parameters.new({ theme: "dark" })
|
58
|
+
params = ActionController::Parameters.new({
|
59
|
+
name: "John",
|
58
60
|
preferences: nested_params
|
59
61
|
})
|
60
|
-
|
61
|
-
validations = {
|
62
|
-
name:
|
63
|
-
preferences: { theme:
|
62
|
+
|
63
|
+
validations = {
|
64
|
+
name: "string",
|
65
|
+
preferences: { theme: "string" }
|
64
66
|
}
|
65
|
-
|
67
|
+
|
66
68
|
validator = HashValidator.validate(params, validations)
|
67
|
-
|
69
|
+
|
68
70
|
expect(validator.valid?).to be true
|
69
71
|
expect(validator.errors).to be_empty
|
70
72
|
end
|
71
73
|
|
72
|
-
it
|
74
|
+
it "should validate ActionController::Parameters with our new validators" do
|
73
75
|
params = ActionController::Parameters.new({
|
74
|
-
name:
|
75
|
-
email:
|
76
|
-
website:
|
77
|
-
zip_code:
|
76
|
+
name: "John",
|
77
|
+
email: "john@example.com",
|
78
|
+
website: "https://john.com",
|
79
|
+
zip_code: "12345"
|
78
80
|
})
|
79
|
-
|
81
|
+
|
80
82
|
validations = {
|
81
|
-
name:
|
82
|
-
email:
|
83
|
-
website:
|
84
|
-
zip_code:
|
83
|
+
name: "alpha",
|
84
|
+
email: "email",
|
85
|
+
website: "url",
|
86
|
+
zip_code: "digits"
|
85
87
|
}
|
86
|
-
|
88
|
+
|
87
89
|
validator = HashValidator.validate(params, validations)
|
88
|
-
|
90
|
+
|
89
91
|
expect(validator.valid?).to be true
|
90
92
|
expect(validator.errors).to be_empty
|
91
93
|
end
|
92
94
|
|
93
|
-
it
|
94
|
-
hash = { name:
|
95
|
-
validations = { name:
|
96
|
-
|
95
|
+
it "should still work with regular hashes" do
|
96
|
+
hash = { name: "John", age: 30 }
|
97
|
+
validations = { name: "string", age: "integer" }
|
98
|
+
|
97
99
|
validator = HashValidator.validate(hash, validations)
|
98
|
-
|
100
|
+
|
99
101
|
expect(validator.valid?).to be true
|
100
102
|
expect(validator.errors).to be_empty
|
101
103
|
end
|
102
|
-
end
|
104
|
+
end
|
@@ -1,111 +1,113 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe HashValidator::Validator::HexColorValidator do
|
4
6
|
let(:validator) { HashValidator::Validator::HexColorValidator.new }
|
5
7
|
|
6
|
-
context
|
7
|
-
it
|
8
|
+
context "valid hex colors" do
|
9
|
+
it "validates 6-digit hex colors (lowercase)" do
|
8
10
|
errors = {}
|
9
|
-
validator.validate(
|
11
|
+
validator.validate("key", "#ff0000", {}, errors)
|
10
12
|
expect(errors).to be_empty
|
11
13
|
end
|
12
14
|
|
13
|
-
it
|
15
|
+
it "validates 6-digit hex colors (uppercase)" do
|
14
16
|
errors = {}
|
15
|
-
validator.validate(
|
17
|
+
validator.validate("key", "#FF0000", {}, errors)
|
16
18
|
expect(errors).to be_empty
|
17
19
|
end
|
18
20
|
|
19
|
-
it
|
21
|
+
it "validates 6-digit hex colors (mixed case)" do
|
20
22
|
errors = {}
|
21
|
-
validator.validate(
|
23
|
+
validator.validate("key", "#Ff0000", {}, errors)
|
22
24
|
expect(errors).to be_empty
|
23
25
|
end
|
24
26
|
|
25
|
-
it
|
27
|
+
it "validates 3-digit hex colors (lowercase)" do
|
26
28
|
errors = {}
|
27
|
-
validator.validate(
|
29
|
+
validator.validate("key", "#f00", {}, errors)
|
28
30
|
expect(errors).to be_empty
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
33
|
+
it "validates 3-digit hex colors (uppercase)" do
|
32
34
|
errors = {}
|
33
|
-
validator.validate(
|
35
|
+
validator.validate("key", "#F00", {}, errors)
|
34
36
|
expect(errors).to be_empty
|
35
37
|
end
|
36
38
|
|
37
|
-
it
|
39
|
+
it "validates black color" do
|
38
40
|
errors = {}
|
39
|
-
validator.validate(
|
41
|
+
validator.validate("key", "#000000", {}, errors)
|
40
42
|
expect(errors).to be_empty
|
41
43
|
end
|
42
44
|
|
43
|
-
it
|
45
|
+
it "validates white color" do
|
44
46
|
errors = {}
|
45
|
-
validator.validate(
|
47
|
+
validator.validate("key", "#ffffff", {}, errors)
|
46
48
|
expect(errors).to be_empty
|
47
49
|
end
|
48
50
|
|
49
|
-
it
|
51
|
+
it "validates complex hex colors" do
|
50
52
|
errors = {}
|
51
|
-
validator.validate(
|
53
|
+
validator.validate("key", "#3a7bd4", {}, errors)
|
52
54
|
expect(errors).to be_empty
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
56
|
-
context
|
57
|
-
it
|
58
|
+
context "invalid hex colors" do
|
59
|
+
it "rejects non-string values" do
|
58
60
|
errors = {}
|
59
|
-
validator.validate(
|
60
|
-
expect(errors[
|
61
|
+
validator.validate("key", 123, {}, errors)
|
62
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
61
63
|
end
|
62
64
|
|
63
|
-
it
|
65
|
+
it "rejects nil values" do
|
64
66
|
errors = {}
|
65
|
-
validator.validate(
|
66
|
-
expect(errors[
|
67
|
+
validator.validate("key", nil, {}, errors)
|
68
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
67
69
|
end
|
68
70
|
|
69
|
-
it
|
71
|
+
it "rejects hex colors without hash" do
|
70
72
|
errors = {}
|
71
|
-
validator.validate(
|
72
|
-
expect(errors[
|
73
|
+
validator.validate("key", "ff0000", {}, errors)
|
74
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
73
75
|
end
|
74
76
|
|
75
|
-
it
|
77
|
+
it "rejects invalid length (4 digits)" do
|
76
78
|
errors = {}
|
77
|
-
validator.validate(
|
78
|
-
expect(errors[
|
79
|
+
validator.validate("key", "#ff00", {}, errors)
|
80
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
79
81
|
end
|
80
82
|
|
81
|
-
it
|
83
|
+
it "rejects invalid length (5 digits)" do
|
82
84
|
errors = {}
|
83
|
-
validator.validate(
|
84
|
-
expect(errors[
|
85
|
+
validator.validate("key", "#ff000", {}, errors)
|
86
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
85
87
|
end
|
86
88
|
|
87
|
-
it
|
89
|
+
it "rejects invalid length (7 digits)" do
|
88
90
|
errors = {}
|
89
|
-
validator.validate(
|
90
|
-
expect(errors[
|
91
|
+
validator.validate("key", "#ff00000", {}, errors)
|
92
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
91
93
|
end
|
92
94
|
|
93
|
-
it
|
95
|
+
it "rejects invalid characters" do
|
94
96
|
errors = {}
|
95
|
-
validator.validate(
|
96
|
-
expect(errors[
|
97
|
+
validator.validate("key", "#gg0000", {}, errors)
|
98
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
97
99
|
end
|
98
100
|
|
99
|
-
it
|
101
|
+
it "rejects empty strings" do
|
100
102
|
errors = {}
|
101
|
-
validator.validate(
|
102
|
-
expect(errors[
|
103
|
+
validator.validate("key", "", {}, errors)
|
104
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
103
105
|
end
|
104
106
|
|
105
|
-
it
|
107
|
+
it "rejects just hash symbol" do
|
106
108
|
errors = {}
|
107
|
-
validator.validate(
|
108
|
-
expect(errors[
|
109
|
+
validator.validate("key", "#", {}, errors)
|
110
|
+
expect(errors["key"]).to eq("is not a valid hex color")
|
109
111
|
end
|
110
112
|
end
|
111
|
-
end
|
113
|
+
end
|