rails-param-validation 0.4.4 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee538fffc925cb6a1c60304d98c741b7bc5e2397e6144e3dc67a05249c94cf06
4
- data.tar.gz: 76d69f62b3cc38cdbd84018c110e7c80e5ac94889338bd18aaf9b078c10912b8
3
+ metadata.gz: f08d88f808fa165c2d4ffae550d8c4eb6d7a69904ec8f159a29562307a072cf9
4
+ data.tar.gz: 5335b9a61723376dd56bc09182506c20536bb94cad4630a0380103e65610017c
5
5
  SHA512:
6
- metadata.gz: 6279e2439b9d4065be253fba5c2d5a7604631808b844c1f1c0f5339709442ba4eb088ad18492271d353dd4a03055e2860faaf1ebd17ce2922334beb479ef6f95
7
- data.tar.gz: 672919b2153bb4e3409f9dadbe3a6696f3c9d6f48c3fd9f982064d50f07ed614b3b5c4b0ef5aa7eb5a2a5b76297877d22d89431d1b263cc083a87132b5928ef7
6
+ metadata.gz: e44809c8f21e77aa4f988cf1317b7f4c4ec312db72e9d239e70544596bbf51bc28007cbd16cb07dffe537ad3762dae09584ff14294afeebffb5a5919d9ef3f82
7
+ data.tar.gz: ad5241ed4bdaeac88b8416fb1bb63725bf4c2d83c31068425976f5ae527d955fc5b71e8993f2125b8441423ebe63e694044c850a0ed750f98a66a53e7bdecc89
@@ -64,7 +64,11 @@ module RailsParamValidation
64
64
  @paths.push(method: method, path: path)
65
65
  end
66
66
 
67
- def finalize!(class_name, method_name)
67
+ def finalize!(class_name, method_name, base_path)
68
+ @paths.each do |path|
69
+ path[:path] = ("/#{base_path}/#{path[:path]}").gsub(/\/+$/, "").gsub(/\/{2,}/, '/')
70
+ end
71
+
68
72
  @responses.each do |code, response|
69
73
  name = Types::Namespace.with_namespace(
70
74
  Types::Namespace.fetch(@source_file),
@@ -23,7 +23,7 @@ module RailsParamValidation
23
23
 
24
24
  @param_definition.store_origin! class_name, method_name
25
25
  RailsParamValidation.config.post_action_definition_hook.call(@param_definition)
26
- @param_definition.finalize! class_name, method_name
26
+ @param_definition.finalize! class_name, method_name, (@base_paths || {}).fetch(self.name, self.name.underscore)
27
27
 
28
28
  AnnotationManager.instance.annotate_method! self, name, :param_definition, @param_definition
29
29
  @param_definition = nil
@@ -38,6 +38,11 @@ module RailsParamValidation
38
38
  end
39
39
 
40
40
  module ClassMethods
41
+ def base_path(path)
42
+ @base_paths ||= {}
43
+ @base_paths[self.name] = path
44
+ end
45
+
41
46
  def param_definition
42
47
  @param_definition || raise(StandardError.new "Annotation must be part of an operation-block")
43
48
  end
@@ -94,10 +99,23 @@ module RailsParamValidation
94
99
  end
95
100
 
96
101
  def action(description = nil, flags = RailsParamValidation.config.default_action_flags)
102
+ if description.is_a?(Hash)
103
+ method = description.keys.first.to_sym
104
+ path = description.values.first
105
+ description = ""
106
+ else
107
+ method = nil
108
+ path = nil
109
+ end
110
+
97
111
  @param_definition = ActionDefinition.new(Types::Namespace.caller_file)
98
112
  @param_definition.description = description
99
113
  flags.each { |name, value| @param_definition.add_flag name, value }
100
114
 
115
+ if method
116
+ @param_definition.add_path method, path
117
+ end
118
+
101
119
  yield
102
120
  end
103
121
  end
@@ -7,7 +7,12 @@ module RailsParamValidation
7
7
 
8
8
  def self.clean_controller_name(klass)
9
9
  klass = klass.to_s if klass.is_a? Symbol
10
- (klass.is_a?(String) ? klass : klass.name).gsub(/Controller$/, '').split('::').last.capitalize.to_sym
10
+ (klass.is_a?(String) ? klass : klass.name).gsub(/Controller$/, '').split("::").join(".").to_sym
11
+ end
12
+
13
+ def self.clean_name(klass)
14
+ klass = klass.to_s if klass.is_a? Symbol
15
+ (klass.is_a?(String) ? klass : klass.name).split("::").join(".").to_sym
11
16
  end
12
17
  end
13
18
 
@@ -46,7 +46,7 @@ module RailsParamValidation
46
46
  param_definition
47
47
  end
48
48
 
49
- RoutingHelper.routes_for(operation.controller.to_s.underscore, operation.action.to_s).each do |route|
49
+ (operation.paths.any? ? operation.paths : RoutingHelper.routes_for(operation.controller.to_s.underscore, operation.action.to_s)).each do |route|
50
50
  action_definition = {
51
51
  operationId: "#{route[:method].downcase}#{route[:path].split(/[^a-zA-Z0-9]+/).map(&:downcase).map(&:capitalize).join}",
52
52
  tags: [RailsHelper.clean_controller_name(operation.controller)],
@@ -1,6 +1,6 @@
1
1
  namespace :openapi do
2
2
 
3
- desc "Export OpenAPI definition to openapi.yaml"
3
+ desc "Export OpenAPI definition to openapi.json"
4
4
  task export: :environment do
5
5
  # Ensure all controllers are loaded
6
6
  if defined? Zeitwerk
@@ -22,7 +22,7 @@ namespace :openapi do
22
22
  print "Writing #{filename}..."
23
23
 
24
24
  begin
25
- File.open(filename, "w") { |f| f.write YAML.dump(openapi.to_object) }
25
+ File.open(filename, "w") { |f| f.write JSON.pretty_generate(openapi.to_object) }
26
26
  puts " done."
27
27
  rescue Exception => e
28
28
  puts " failed."
@@ -50,7 +50,10 @@ class CustomT < AnnotationT
50
50
  end
51
51
 
52
52
  def schema
53
- CustomT.registry[@type]
53
+ schema = CustomT.registry[@type]
54
+ schema = CustomT.registry[@type] = schema.call if schema.is_a?(Proc)
55
+
56
+ schema
54
57
  end
55
58
 
56
59
  def self.register(type, schema)
@@ -89,6 +92,8 @@ module Types
89
92
 
90
93
  path.pop
91
94
  end
95
+
96
+ nil
92
97
  end
93
98
 
94
99
  def self.caller_file
@@ -140,7 +145,11 @@ module Types
140
145
  end
141
146
 
142
147
  def Type(type)
143
- AnnotationTypes::CustomT.new(Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type))
148
+ if type.is_a?(Class)
149
+ AnnotationTypes::CustomT.new(RailsHelper.clean_name(type))
150
+ else
151
+ AnnotationTypes::CustomT.new(Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type))
152
+ end
144
153
  end
145
154
  end
146
155
 
@@ -1,3 +1,3 @@
1
1
  module RailsParamValidation
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-param-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oskar Kirmis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-07 00:00:00.000000000 Z
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Declarative parameter definition and validation for Rails
14
14
  email:
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.3.7
96
+ rubygems_version: 3.3.12
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Declarative parameter definition and validation for Rails