api-regulator 0.1.9 → 0.1.10
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/Gemfile.lock +1 -1
- data/lib/api_regulator/validator.rb +27 -3
- data/lib/api_regulator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 812fb74cb8ccd8e73304ba80e95fb06c1d7bf0c91e78337c0b13cb3cd39ff189
|
4
|
+
data.tar.gz: e761379ba9cd5b8eb2452a6508d9e6d261f63b9fe68f33e4cab133993a74a742
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8703cb89b3bd4d291c75ded89e4cfe5bb4c9a82d8312822a2011062484548beb8f54b624b968b8a860a8a3ec1ab01f235d914d3cd96f96515eaeaea28c6cc599
|
7
|
+
data.tar.gz: f51e863849f482218c4fa4e0b918e6973c63745cbee5be20356ad7eac6edb750d17aff5a918c0389bc5b4bb81e2a3efa229551a4ce0062b0d73e55058349d5ab
|
data/Gemfile.lock
CHANGED
@@ -29,9 +29,8 @@ module ApiRegulator
|
|
29
29
|
attribute full_key, param.type if param.type
|
30
30
|
self.defined_attributes += [full_key]
|
31
31
|
|
32
|
-
# Add validations for scalar attributes
|
33
32
|
param.options.each do |option, value|
|
34
|
-
validates full_key, option => value
|
33
|
+
validates full_key, option => value, allow_blank: !param.required?
|
35
34
|
end
|
36
35
|
|
37
36
|
# Add type-specific validations
|
@@ -40,6 +39,7 @@ module ApiRegulator
|
|
40
39
|
validate -> { validate_string(full_key) } if param.type == :string
|
41
40
|
end
|
42
41
|
|
42
|
+
|
43
43
|
def define_object_validations(param, full_key)
|
44
44
|
# Build nested validator class
|
45
45
|
nested_validator_class = build_nested_validator_class(param.children, param.name, self)
|
@@ -104,17 +104,20 @@ module ApiRegulator
|
|
104
104
|
# Instance methods
|
105
105
|
def validate_array_of_objects(attribute, item_validator_class, param)
|
106
106
|
raw_value = @raw_attributes[attribute]
|
107
|
+
|
108
|
+
# Ensure the presence check works only on the array itself, not its contents
|
107
109
|
if raw_value.blank?
|
108
110
|
errors.add(attribute, "can't be blank") if param.options[:presence]
|
109
111
|
return
|
110
112
|
end
|
113
|
+
|
111
114
|
unless raw_value.is_a?(Array)
|
112
115
|
errors.add(attribute, "must be an array")
|
113
116
|
return
|
114
117
|
end
|
115
118
|
|
116
119
|
raw_value.each_with_index do |value, index|
|
117
|
-
unless value.is_a?(Hash) ||
|
120
|
+
unless value.is_a?(Hash) || value.is_a?(ActionController::Parameters)
|
118
121
|
errors.add("#{attribute}[#{index}]", "must be a hash")
|
119
122
|
next
|
120
123
|
end
|
@@ -136,27 +139,45 @@ module ApiRegulator
|
|
136
139
|
errors.add(attribute, "can't be blank") if param.options[:presence]
|
137
140
|
return
|
138
141
|
end
|
142
|
+
|
139
143
|
unless raw_value.is_a?(Array)
|
140
144
|
errors.add(attribute, "must be an array")
|
141
145
|
return
|
142
146
|
end
|
143
147
|
|
148
|
+
inclusion_list = param.options[:inclusion]&.dig(:in)
|
149
|
+
|
144
150
|
raw_value.each_with_index do |value, index|
|
151
|
+
# Skip type and inclusion validation if nil is allowed and value is nil
|
152
|
+
if value.nil? && (inclusion_list&.include?(nil) || param.options[:allow_nil_items])
|
153
|
+
next
|
154
|
+
end
|
155
|
+
|
156
|
+
# Type validation for non-nil values
|
145
157
|
case param.item_type
|
146
158
|
when :string
|
147
159
|
unless value.is_a?(String)
|
148
160
|
errors.add("#{attribute}[#{index}]", "must be a string")
|
161
|
+
next
|
149
162
|
end
|
150
163
|
when :integer
|
151
164
|
unless value.is_a?(Integer)
|
152
165
|
errors.add("#{attribute}[#{index}]", "must be an integer")
|
166
|
+
next
|
153
167
|
end
|
154
168
|
when :boolean
|
155
169
|
unless [true, false].include?(value)
|
156
170
|
errors.add("#{attribute}[#{index}]", "must be a boolean")
|
171
|
+
next
|
157
172
|
end
|
158
173
|
else
|
159
174
|
errors.add("#{attribute}[#{index}]", "has an unsupported type")
|
175
|
+
next
|
176
|
+
end
|
177
|
+
|
178
|
+
# Inclusion validation for non-nil values
|
179
|
+
if inclusion_list && !inclusion_list.include?(value)
|
180
|
+
errors.add("#{attribute}[#{index}]", "is not included in the list")
|
160
181
|
end
|
161
182
|
end
|
162
183
|
end
|
@@ -164,6 +185,9 @@ module ApiRegulator
|
|
164
185
|
def validate_nested_object(attribute, nested_validator_class, param)
|
165
186
|
raw_value = @raw_attributes[attribute]
|
166
187
|
|
188
|
+
# Skip validation if optional and not present
|
189
|
+
return if raw_value.nil? && param.options[:optional]
|
190
|
+
|
167
191
|
if raw_value.nil?
|
168
192
|
errors.add(attribute, "can't be blank") if param.options[:presence]
|
169
193
|
return
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-regulator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geoff Massanek
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|