api-regulator 0.1.7 → 0.1.8
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/open_api_generator.rb +1 -1
- data/lib/api_regulator/validator.rb +50 -16
- 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: 99b1c4cbba316a8035a073ab0179c0013ff12ffda3ac32087d26608127af8f4e
|
4
|
+
data.tar.gz: 9d1fbf44040360e9d17b433dcf9bca61a97369673980c1a6c5207a8c0871e2fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a4be7c0e6da948900b799192ecf37493402b34900eb9919f7c55081feacb8816a1b351e75b65c92a821263646994d64944c0cc3774913f2e2dd400a5360caf3
|
7
|
+
data.tar.gz: ec7cccd9723efaefb9a99bb6eed077cf12f310d7e86e715584a28cbb6fe2b65d85678eb804e52fccd9c694be17499dd7aa45d18b21a942d68e60ca36efc5169b
|
data/Gemfile.lock
CHANGED
@@ -142,7 +142,7 @@ module ApiRegulator
|
|
142
142
|
elsif param.options[:format][:with] == Formats::DATE
|
143
143
|
schema[:format] = 'date'
|
144
144
|
elsif param.options[:format][:with] == Formats::DATETIME
|
145
|
-
schema[:format] = 'date-
|
145
|
+
schema[:format] = 'date-time'
|
146
146
|
elsif param.options[:format][:with] == Formats::URI
|
147
147
|
schema[:format] = 'uri'
|
148
148
|
elsif param.options[:format][:with].is_a?(Regexp)
|
@@ -56,9 +56,13 @@ module ApiRegulator
|
|
56
56
|
# Build a nested validator class for array items
|
57
57
|
item_validator_class = build_nested_validator_class(param.children, param.name, self)
|
58
58
|
validate -> { validate_array_of_objects(full_key, item_validator_class, param) }
|
59
|
+
|
60
|
+
# Store the nested validator
|
61
|
+
nested_validators[full_key] = [item_validator_class]
|
59
62
|
elsif param.item_type
|
60
63
|
# Scalar array with specific item type
|
61
64
|
validate -> { validate_array_of_scalars(full_key, param) }
|
65
|
+
nested_validators[full_key] = :scalars
|
62
66
|
else
|
63
67
|
raise "Arrays must have children or an item_type"
|
64
68
|
end
|
@@ -84,11 +88,11 @@ module ApiRegulator
|
|
84
88
|
allowed_attributes = attributes.slice(*self.class.defined_attributes.map(&:to_sym))
|
85
89
|
super(allowed_attributes)
|
86
90
|
end
|
87
|
-
end
|
88
91
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
+
# Add child attributes and validations
|
93
|
+
children.each do |child|
|
94
|
+
define_attribute_and_validations(child)
|
95
|
+
end
|
92
96
|
end
|
93
97
|
|
94
98
|
# Store the nested class under the parent class namespace
|
@@ -214,32 +218,62 @@ module ApiRegulator
|
|
214
218
|
def self.build_all(api_definitions)
|
215
219
|
api_definitions.each do |api_definition|
|
216
220
|
class_name = "#{api_definition.controller_path}/#{api_definition.action_name}".gsub("/", "_").camelcase
|
217
|
-
validator_class = build_class(api_definition)
|
221
|
+
validator_class = build_class(api_definition.params)
|
218
222
|
@validators[[api_definition.controller_path, api_definition.action_name]] = validator_class
|
219
223
|
Validator.const_set(class_name, validator_class)
|
220
224
|
end
|
221
225
|
end
|
222
226
|
|
223
|
-
def self.
|
224
|
-
|
227
|
+
def self.build_response_validators(api_definitions = ApiRegulator.api_definitions)
|
228
|
+
api_definitions.each do |api_definition|
|
229
|
+
api_definition.responses.each do |code, params|
|
230
|
+
class_name = "#{api_definition.controller_path}/#{api_definition.action_name}/Response#{code}".gsub("/", "_").camelcase
|
231
|
+
|
232
|
+
validator_class = build_class(params.children)
|
233
|
+
@validators[[api_definition.controller_path, api_definition.action_name, code]] = validator_class
|
234
|
+
Validator.const_set(class_name, validator_class)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def self.get(controller, action, code = nil)
|
240
|
+
@validators[[controller.to_s, action.to_s, code].compact]
|
225
241
|
end
|
226
242
|
|
227
|
-
def self.build_class(
|
228
|
-
Class.new
|
229
|
-
|
243
|
+
def self.build_class(params)
|
244
|
+
Class.new do
|
245
|
+
include ActiveModel::Model
|
246
|
+
include ActiveModel::Attributes
|
247
|
+
include AttributeDefinitionMixin
|
248
|
+
|
249
|
+
def initialize(attributes = {})
|
250
|
+
@raw_attributes = attributes.deep_symbolize_keys
|
251
|
+
self.class.defined_attributes
|
252
|
+
allowed_attributes = attributes.slice(*self.class.defined_attributes.map(&:to_sym))
|
253
|
+
super(allowed_attributes)
|
254
|
+
end
|
255
|
+
|
256
|
+
params.each do |param|
|
230
257
|
define_attribute_and_validations(param)
|
231
258
|
end
|
232
259
|
end
|
233
260
|
end
|
234
261
|
|
235
|
-
def self.
|
236
|
-
|
262
|
+
def self.validate_response(controller, action, code, body)
|
263
|
+
validator_class = get(controller, action, code)
|
264
|
+
|
265
|
+
unless validator_class
|
266
|
+
raise "No validator found"
|
267
|
+
end
|
268
|
+
|
269
|
+
validator = validator_class.new(body)
|
270
|
+
unless validator.valid?
|
271
|
+
raise ApiRegulator::ValidationError.new(validator.errors)
|
272
|
+
end
|
237
273
|
end
|
238
274
|
|
239
|
-
def
|
240
|
-
@
|
241
|
-
allowed_attributes = attributes.slice(*self.class.defined_attributes.map(&:to_sym))
|
242
|
-
super(allowed_attributes)
|
275
|
+
def self.reset_validators
|
276
|
+
@validators = {}
|
243
277
|
end
|
244
278
|
end
|
245
279
|
end
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geoff Massanek
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|