api-regulator 0.1.7 → 0.1.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21f65fe067c5267aa27557544c2ff86d4bc860be047322336b9489562d6912fa
4
- data.tar.gz: c87cb89c91c7d5f8a5d2d0454db67c80672696b34594e0f18aab5d0721db39bf
3
+ metadata.gz: c1d041ae7a972c291795b539d3543fbda9da721c10f505966af5eca71c00fac4
4
+ data.tar.gz: 592189f17e96f0c676adb9d393698ae32617a6f9558b5a255911b97ea422b6b9
5
5
  SHA512:
6
- metadata.gz: 02f6d4e3824a31fe8ac562d08c87ec1c5c3bb80050e7589ab76b9a2aee172ca06a96965bcc714d54729d263aa85c4eca245608eaaf98dff487f36b4cbdc65c60
7
- data.tar.gz: 294239002947154ea624821f5f73d4e9600113ce380a7d3344315dba9c3a36be77e77c1a41e28c02890f9fe56ccab981d2a63e177f14069fb0938a3cd023febf
6
+ metadata.gz: 29f9365bde56835debcffe256e6496d0cf3e8a9abe09aebeb88493141e893b38943cdfe83116cfd1bb7f1e95b73e59281849977b76834731cbf5d6ab022154ff
7
+ data.tar.gz: 2c3bcd87b072eb9cb5877d15224a1782c9a90e16700050cb9d594367c741a2d8d27da992ca0c99d9f48e6ea959390e93ca44d6fa1b0099aafb861f97cf68cc74
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api-regulator (0.1.7)
4
+ api-regulator (0.1.9)
5
5
  activemodel (~> 8.0)
6
6
  activesupport (~> 8.0)
7
7
 
@@ -36,9 +36,16 @@ module ApiRegulator
36
36
 
37
37
  body_params = api_definition.params.select(&:body?)
38
38
  permitted_body = body_params.each_with_object({}) do |param, hash|
39
- if params.key?(param.name)
40
- params.require(param.name).permit(*build_permitted_keys(param.children)).to_h.symbolize_keys
41
- hash[param.name.to_sym] = params.require(param.name).permit(*build_permitted_keys(param.children)).to_h.symbolize_keys
39
+ if param.type == :object
40
+ if param.required?
41
+ nested = params.expect(param.name => build_permitted_keys(param.children)).to_h.symbolize_keys
42
+ hash[param.name.to_sym] = nested
43
+ else
44
+ nested = params.permit(param.name => build_permitted_keys(param.children)).to_h.symbolize_keys
45
+ hash.merge!(nested)
46
+ end
47
+ else
48
+ hash[param.name.to_sym] = params[param.name]
42
49
  end
43
50
  end.symbolize_keys
44
51
 
@@ -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-type'
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)
@@ -37,6 +37,10 @@ module ApiRegulator
37
37
  end
38
38
  end
39
39
 
40
+ def required?
41
+ !!@options[:presence]
42
+ end
43
+
40
44
  def body?
41
45
  location == :body
42
46
  end
@@ -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
- # Add child attributes and validations
90
- children.each do |child|
91
- nested_validator_class.define_attribute_and_validations(child)
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.get(controller, action)
224
- @validators[[controller, action]]
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(api_definition)
228
- Class.new(self) do
229
- api_definition.params.each do |param|
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.reset_validators
236
- @validators = {}
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 initialize(attributes = {})
240
- @raw_attributes = attributes.deep_symbolize_keys
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
@@ -1,3 +1,3 @@
1
1
  module ApiRegulator
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.9"
3
3
  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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Massanek
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-20 00:00:00.000000000 Z
11
+ date: 2025-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport