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
data/spec/configuration_spec.rb
CHANGED
@@ -1,25 +1,27 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "HashValidator.configure" do
|
6
|
+
describe "configuration DSL" do
|
5
7
|
after(:each) do
|
6
8
|
# Clean up any validators added during tests
|
7
|
-
HashValidator.remove_validator(
|
8
|
-
HashValidator.remove_validator(
|
9
|
-
HashValidator.remove_validator(
|
10
|
-
HashValidator.remove_validator(
|
11
|
-
HashValidator.remove_validator(
|
12
|
-
HashValidator.remove_validator(
|
9
|
+
HashValidator.remove_validator("test_pattern")
|
10
|
+
HashValidator.remove_validator("test_func")
|
11
|
+
HashValidator.remove_validator("bulk_odd")
|
12
|
+
HashValidator.remove_validator("bulk_even")
|
13
|
+
HashValidator.remove_validator("bulk_palindrome")
|
14
|
+
HashValidator.remove_validator("custom_configured")
|
13
15
|
end
|
14
16
|
|
15
|
-
it
|
17
|
+
it "allows adding instance-based validators" do
|
16
18
|
class HashValidator::Validator::CustomConfiguredValidator < HashValidator::Validator::Base
|
17
19
|
def initialize
|
18
|
-
super(
|
20
|
+
super("custom_configured")
|
19
21
|
end
|
20
|
-
|
22
|
+
|
21
23
|
def valid?(value)
|
22
|
-
value ==
|
24
|
+
value == "configured"
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -28,162 +30,162 @@ describe 'HashValidator.configure' do
|
|
28
30
|
end
|
29
31
|
|
30
32
|
validator = HashValidator.validate(
|
31
|
-
{ status:
|
32
|
-
{ status:
|
33
|
+
{ status: "configured" },
|
34
|
+
{ status: "custom_configured" }
|
33
35
|
)
|
34
36
|
expect(validator.valid?).to eq true
|
35
37
|
end
|
36
38
|
|
37
|
-
it
|
39
|
+
it "allows adding pattern-based validators" do
|
38
40
|
HashValidator.configure do |config|
|
39
|
-
config.add_validator
|
40
|
-
pattern: /\A[A-Z]{3}\z/,
|
41
|
-
error_message:
|
41
|
+
config.add_validator "test_pattern",
|
42
|
+
pattern: /\A[A-Z]{3}\z/,
|
43
|
+
error_message: "must be three uppercase letters"
|
42
44
|
end
|
43
45
|
|
44
46
|
validator = HashValidator.validate(
|
45
|
-
{ code:
|
46
|
-
{ code:
|
47
|
+
{ code: "ABC" },
|
48
|
+
{ code: "test_pattern" }
|
47
49
|
)
|
48
50
|
expect(validator.valid?).to eq true
|
49
51
|
|
50
52
|
validator = HashValidator.validate(
|
51
|
-
{ code:
|
52
|
-
{ code:
|
53
|
+
{ code: "abc" },
|
54
|
+
{ code: "test_pattern" }
|
53
55
|
)
|
54
56
|
expect(validator.valid?).to eq false
|
55
|
-
expect(validator.errors).to eq({ code:
|
57
|
+
expect(validator.errors).to eq({ code: "must be three uppercase letters" })
|
56
58
|
end
|
57
59
|
|
58
|
-
it
|
60
|
+
it "allows adding function-based validators" do
|
59
61
|
HashValidator.configure do |config|
|
60
|
-
config.add_validator
|
62
|
+
config.add_validator "test_func",
|
61
63
|
func: ->(v) { v.is_a?(Integer) && v > 100 },
|
62
|
-
error_message:
|
64
|
+
error_message: "must be an integer greater than 100"
|
63
65
|
end
|
64
66
|
|
65
67
|
validator = HashValidator.validate(
|
66
68
|
{ score: 150 },
|
67
|
-
{ score:
|
69
|
+
{ score: "test_func" }
|
68
70
|
)
|
69
71
|
expect(validator.valid?).to eq true
|
70
72
|
|
71
73
|
validator = HashValidator.validate(
|
72
74
|
{ score: 50 },
|
73
|
-
{ score:
|
75
|
+
{ score: "test_func" }
|
74
76
|
)
|
75
77
|
expect(validator.valid?).to eq false
|
76
|
-
expect(validator.errors).to eq({ score:
|
78
|
+
expect(validator.errors).to eq({ score: "must be an integer greater than 100" })
|
77
79
|
end
|
78
80
|
|
79
|
-
it
|
81
|
+
it "allows adding multiple validators in one configure block" do
|
80
82
|
HashValidator.configure do |config|
|
81
|
-
config.add_validator
|
83
|
+
config.add_validator "bulk_odd",
|
82
84
|
pattern: /\A\d*[13579]\z/,
|
83
|
-
error_message:
|
84
|
-
|
85
|
-
config.add_validator
|
85
|
+
error_message: "must be odd"
|
86
|
+
|
87
|
+
config.add_validator "bulk_even",
|
86
88
|
pattern: /\A\d*[02468]\z/,
|
87
|
-
error_message:
|
88
|
-
|
89
|
-
config.add_validator
|
89
|
+
error_message: "must be even"
|
90
|
+
|
91
|
+
config.add_validator "bulk_palindrome",
|
90
92
|
func: proc { |s| s.to_s == s.to_s.reverse },
|
91
|
-
error_message:
|
93
|
+
error_message: "must be a palindrome"
|
92
94
|
end
|
93
95
|
|
94
96
|
# Test odd validator
|
95
97
|
validator = HashValidator.validate(
|
96
|
-
{ num:
|
97
|
-
{ num:
|
98
|
+
{ num: "13" },
|
99
|
+
{ num: "bulk_odd" }
|
98
100
|
)
|
99
101
|
expect(validator.valid?).to eq true
|
100
102
|
|
101
103
|
# Test even validator
|
102
104
|
validator = HashValidator.validate(
|
103
|
-
{ num:
|
104
|
-
{ num:
|
105
|
+
{ num: "24" },
|
106
|
+
{ num: "bulk_even" }
|
105
107
|
)
|
106
108
|
expect(validator.valid?).to eq true
|
107
109
|
|
108
110
|
# Test palindrome validator
|
109
111
|
validator = HashValidator.validate(
|
110
|
-
{ word:
|
111
|
-
{ word:
|
112
|
+
{ word: "level" },
|
113
|
+
{ word: "bulk_palindrome" }
|
112
114
|
)
|
113
115
|
expect(validator.valid?).to eq true
|
114
116
|
end
|
115
117
|
|
116
|
-
it
|
118
|
+
it "allows removing validators within configure block" do
|
117
119
|
# First add a validator
|
118
|
-
HashValidator.add_validator
|
120
|
+
HashValidator.add_validator "test_pattern",
|
119
121
|
pattern: /test/,
|
120
|
-
error_message:
|
122
|
+
error_message: "test"
|
121
123
|
|
122
124
|
# Then remove it in configure
|
123
125
|
HashValidator.configure do |config|
|
124
|
-
config.remove_validator
|
126
|
+
config.remove_validator "test_pattern"
|
125
127
|
end
|
126
128
|
|
127
129
|
# Should raise error as validator no longer exists
|
128
130
|
expect {
|
129
|
-
HashValidator.validate({ value:
|
131
|
+
HashValidator.validate({ value: "test" }, { value: "test_pattern" })
|
130
132
|
}.to raise_error(StandardError, /Could not find valid validator/)
|
131
133
|
end
|
132
134
|
|
133
|
-
it
|
135
|
+
it "works without a block" do
|
134
136
|
expect {
|
135
137
|
HashValidator.configure
|
136
138
|
}.not_to raise_error
|
137
139
|
end
|
138
140
|
|
139
|
-
it
|
141
|
+
it "yields a Configuration instance" do
|
140
142
|
HashValidator.configure do |config|
|
141
143
|
expect(config).to be_a(HashValidator::Configuration)
|
142
144
|
end
|
143
145
|
end
|
144
146
|
end
|
145
147
|
|
146
|
-
describe
|
148
|
+
describe "Rails-style initializer example" do
|
147
149
|
after(:each) do
|
148
|
-
HashValidator.remove_validator(
|
149
|
-
HashValidator.remove_validator(
|
150
|
-
HashValidator.remove_validator(
|
150
|
+
HashValidator.remove_validator("phone")
|
151
|
+
HashValidator.remove_validator("postal_code")
|
152
|
+
HashValidator.remove_validator("age_range")
|
151
153
|
end
|
152
154
|
|
153
|
-
it
|
155
|
+
it "can be used like a Rails initializer" do
|
154
156
|
# This would typically be in config/initializers/hash_validator.rb
|
155
157
|
HashValidator.configure do |config|
|
156
158
|
# Add pattern validators
|
157
|
-
config.add_validator
|
159
|
+
config.add_validator "phone",
|
158
160
|
pattern: /\A\+?[1-9]\d{1,14}\z/,
|
159
|
-
error_message:
|
160
|
-
|
161
|
-
config.add_validator
|
161
|
+
error_message: "must be a valid international phone number"
|
162
|
+
|
163
|
+
config.add_validator "postal_code",
|
162
164
|
pattern: /\A[A-Z0-9]{3,10}\z/i,
|
163
|
-
error_message:
|
164
|
-
|
165
|
+
error_message: "must be a valid postal code"
|
166
|
+
|
165
167
|
# Add function validator
|
166
|
-
config.add_validator
|
168
|
+
config.add_validator "age_range",
|
167
169
|
func: ->(age) { age.is_a?(Integer) && age.between?(0, 120) },
|
168
|
-
error_message:
|
170
|
+
error_message: "must be between 0 and 120"
|
169
171
|
end
|
170
172
|
|
171
173
|
# Use the configured validators
|
172
174
|
validator = HashValidator.validate(
|
173
|
-
{
|
174
|
-
phone:
|
175
|
-
postal_code:
|
175
|
+
{
|
176
|
+
phone: "+14155551234",
|
177
|
+
postal_code: "SW1A1AA",
|
176
178
|
age: 30
|
177
179
|
},
|
178
|
-
{
|
179
|
-
phone:
|
180
|
-
postal_code:
|
181
|
-
age:
|
180
|
+
{
|
181
|
+
phone: "phone",
|
182
|
+
postal_code: "postal_code",
|
183
|
+
age: "age_range"
|
182
184
|
}
|
183
185
|
)
|
184
|
-
|
186
|
+
|
185
187
|
expect(validator.valid?).to eq true
|
186
188
|
expect(validator.errors).to be_empty
|
187
189
|
end
|
188
190
|
end
|
189
|
-
end
|
191
|
+
end
|