api-regulator 0.1.15 → 0.1.16
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 +4 -4
- data/lib/api_regulator/api.rb +2 -0
- data/lib/api_regulator/shared_schema.rb +13 -1
- data/lib/api_regulator/validator.rb +19 -17
- 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: 60c81fb2bb5833ff9a4986bf2562130435a6b94318213688092a62040b4459dc
|
4
|
+
data.tar.gz: 97a81bcd48c8515c3337804af36a7ad20d96fb5150b26221627a4708ab7d3b6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b42e011a7af9d72be2cff39843028d088e5e0684de53d6feaf21a558ce8702aca2d81949596c8c7b797b330412e292c5a135e8ee7a39e9c3c1c46eab1c0ba96
|
7
|
+
data.tar.gz: 10d8acda94aa29c3b3351f540b2f36136f7cc91803b62ba2d21bbd779a6e7807d31344e9779faaa4e3e3d0e59c6292f0fe372b25365f0c04bf1e646e37a8562f
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
api-regulator (0.1.
|
4
|
+
api-regulator (0.1.16)
|
5
5
|
activemodel (~> 8.0)
|
6
6
|
activesupport (~> 8.0)
|
7
7
|
|
@@ -120,7 +120,7 @@ GEM
|
|
120
120
|
net-protocol
|
121
121
|
net-protocol (0.2.2)
|
122
122
|
timeout
|
123
|
-
net-smtp (0.5.
|
123
|
+
net-smtp (0.5.1)
|
124
124
|
net-protocol
|
125
125
|
nio4r (2.7.4)
|
126
126
|
nokogiri (1.18.2)
|
@@ -173,7 +173,7 @@ GEM
|
|
173
173
|
thor (~> 1.0, >= 1.2.2)
|
174
174
|
zeitwerk (~> 2.6)
|
175
175
|
rake (13.2.1)
|
176
|
-
rdoc (6.
|
176
|
+
rdoc (6.12.0)
|
177
177
|
psych (>= 4.0.0)
|
178
178
|
reline (0.6.0)
|
179
179
|
io-console (~> 0.5)
|
@@ -181,7 +181,7 @@ GEM
|
|
181
181
|
rspec-core (~> 3.13.0)
|
182
182
|
rspec-expectations (~> 3.13.0)
|
183
183
|
rspec-mocks (~> 3.13.0)
|
184
|
-
rspec-core (3.13.
|
184
|
+
rspec-core (3.13.3)
|
185
185
|
rspec-support (~> 3.13.0)
|
186
186
|
rspec-expectations (3.13.3)
|
187
187
|
diff-lcs (>= 1.2.0, < 2.0)
|
data/lib/api_regulator/api.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module ApiRegulator
|
2
2
|
class SharedSchema
|
3
|
-
attr_reader :name, :description, :params
|
3
|
+
attr_reader :name, :description, :params, :responses
|
4
4
|
|
5
5
|
def initialize(name, description, &block)
|
6
6
|
@name = name
|
7
7
|
@description = description
|
8
8
|
@params = []
|
9
|
+
@responses = {}
|
9
10
|
instance_eval(&block) if block_given?
|
10
11
|
end
|
11
12
|
|
@@ -13,6 +14,17 @@ module ApiRegulator
|
|
13
14
|
param = Param.new(name, type, item_type: item_type, desc: desc, location: location, **options, &block)
|
14
15
|
@params << param
|
15
16
|
end
|
17
|
+
|
18
|
+
def response(status_code, description_or_options, &block)
|
19
|
+
if description_or_options.is_a?(Hash) && description_or_options[:ref]
|
20
|
+
# Reference to a shared schema
|
21
|
+
@responses[status_code] = Param.new(:root, :object, ref: description_or_options[:ref], &block)
|
22
|
+
else
|
23
|
+
# Inline schema definition
|
24
|
+
schema = Param.new(:root, :object, desc: description_or_options, &block)
|
25
|
+
@responses[status_code] = schema
|
26
|
+
end
|
27
|
+
end
|
16
28
|
end
|
17
29
|
|
18
30
|
@shared_schemas = {}
|
@@ -10,29 +10,31 @@ module ApiRegulator
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class_methods do
|
13
|
-
def define_attribute_and_validations(param, parent_key: nil,
|
13
|
+
def define_attribute_and_validations(param, parent_key: nil, action_name: nil)
|
14
14
|
# Construct the full key
|
15
15
|
full_key = parent_key ? "#{parent_key}.#{param.name}".to_sym : param.name.to_sym
|
16
16
|
|
17
17
|
case param.type
|
18
18
|
when :array
|
19
|
-
define_array_validations(param, full_key,
|
19
|
+
define_array_validations(param, full_key, action_name)
|
20
20
|
when :object
|
21
|
-
define_object_validations(param, full_key,
|
21
|
+
define_object_validations(param, full_key, action_name)
|
22
22
|
else
|
23
|
-
define_scalar_validations(param, full_key,
|
23
|
+
define_scalar_validations(param, full_key, action_name)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
def define_scalar_validations(param, full_key,
|
28
|
-
# Define scalar attributes
|
27
|
+
def define_scalar_validations(param, full_key, action_name)
|
29
28
|
attribute full_key, param.type if param.type
|
30
29
|
self.defined_attributes += [full_key]
|
31
30
|
|
32
|
-
param.options.
|
31
|
+
if param.options.present?
|
33
32
|
validates full_key,
|
34
|
-
|
35
|
-
|
33
|
+
param.options.merge(
|
34
|
+
if: ->(record) do
|
35
|
+
param.required?(validation_context) || record.raw_attributes.key?(param.name)
|
36
|
+
end
|
37
|
+
)
|
36
38
|
end
|
37
39
|
|
38
40
|
# Add type-specific validations
|
@@ -44,9 +46,9 @@ module ApiRegulator
|
|
44
46
|
end
|
45
47
|
|
46
48
|
|
47
|
-
def define_object_validations(param, full_key,
|
49
|
+
def define_object_validations(param, full_key, action_name)
|
48
50
|
# Build nested validator class
|
49
|
-
nested_validator_class = build_nested_validator_class(param.children, param.name, self,
|
51
|
+
nested_validator_class = build_nested_validator_class(param.children, param.name, self, action_name)
|
50
52
|
|
51
53
|
# Add a custom validation for the nested object
|
52
54
|
validate -> { validate_nested_object(full_key, nested_validator_class, param) }
|
@@ -55,10 +57,10 @@ module ApiRegulator
|
|
55
57
|
nested_validators[full_key] = nested_validator_class
|
56
58
|
end
|
57
59
|
|
58
|
-
def define_array_validations(param, full_key,
|
60
|
+
def define_array_validations(param, full_key, action_name)
|
59
61
|
if param.children.any?
|
60
62
|
# Build a nested validator class for array items
|
61
|
-
item_validator_class = build_nested_validator_class(param.children, param.name, self,
|
63
|
+
item_validator_class = build_nested_validator_class(param.children, param.name, self, action_name)
|
62
64
|
validate -> { validate_array_of_objects(full_key, item_validator_class, param) }
|
63
65
|
|
64
66
|
# Store the nested validator
|
@@ -72,7 +74,7 @@ module ApiRegulator
|
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
75
|
-
def build_nested_validator_class(children, parent_key, parent_class,
|
77
|
+
def build_nested_validator_class(children, parent_key, parent_class, action_name)
|
76
78
|
# Create a unique class name based on the parent key
|
77
79
|
class_name = "#{parent_key.to_s.camelize}"
|
78
80
|
|
@@ -97,7 +99,7 @@ module ApiRegulator
|
|
97
99
|
|
98
100
|
# Add child attributes and validations
|
99
101
|
children.each do |child|
|
100
|
-
define_attribute_and_validations(child,
|
102
|
+
define_attribute_and_validations(child, action_name:)
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
@@ -270,7 +272,7 @@ module ApiRegulator
|
|
270
272
|
@validators[[controller.to_s, action.to_s, code].compact]
|
271
273
|
end
|
272
274
|
|
273
|
-
def self.build_class(params,
|
275
|
+
def self.build_class(params, action_name)
|
274
276
|
Class.new do
|
275
277
|
include ActiveModel::Model
|
276
278
|
include ActiveModel::Attributes
|
@@ -286,7 +288,7 @@ module ApiRegulator
|
|
286
288
|
end
|
287
289
|
|
288
290
|
params.each do |param|
|
289
|
-
define_attribute_and_validations(param,
|
291
|
+
define_attribute_and_validations(param, action_name:)
|
290
292
|
end
|
291
293
|
end
|
292
294
|
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.16
|
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-
|
11
|
+
date: 2025-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|