rails-param-validation 0.4.2 → 0.4.5

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: ea025e319c6a3d02b8781c6c80ffc068cb72b8d694e3dc1fc843656f2dd4b862
4
- data.tar.gz: 9f1f786ec22824cb5ae9eaa8a33ef39d5a147ac9ed19d570e6414513cf418adf
3
+ metadata.gz: 7cac5e8e1680a9f3cbdce7cd511d39f4b95d0cddf9f2955e9bbbde3ff56daa5b
4
+ data.tar.gz: a2f4422840a284a0658fa9c241bcda19edbcf7ad3927bc94a79ec38178b86e5b
5
5
  SHA512:
6
- metadata.gz: a2ec3191599ac5b8fa5a4dbcf7684759de23a129be732e90275298b358e9d84d53cb653225413cadc229ad03b1e9baecac9dbce2e2269b1d8b9e8344a21b3fff
7
- data.tar.gz: 980cf1cde5a0d0500da47c4eb69ec514a28c0da4eb769b13d23d443f63d5587faf915c23705b6178c02817d6665bda3249cc1862bcc1c10093e04b6d1c7514d9
6
+ metadata.gz: 5f1a29d5ed1039772184372c44073af3104cf4acc7d8f9299c561891dcb71f877ef937617fe91776919fd32021e7bc67e69a53f3e78c27e098520376d1ea2466
7
+ data.tar.gz: e49a9586e8446164a580e4143f8af1dfaa2f04228d102e6ce00b1fb21bc9ddac36c8885b92df5064768439a136bf74d9ff8f1674787f4bf70b605f64ecd6ff6e
@@ -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),
@@ -31,6 +31,7 @@ module RailsParamValidation
31
31
  attr_accessor :default_body_content_type
32
32
  attr_accessor :default_action_flags
33
33
  attr_accessor :post_action_definition_hook
34
+ attr_accessor :auto_include_in_classes
34
35
  attr_reader :openapi
35
36
 
36
37
  def initialize
@@ -41,6 +42,7 @@ module RailsParamValidation
41
42
  @default_body_content_type = 'application/json'
42
43
  @default_action_flags = {}
43
44
  @post_action_definition_hook = ->(_action_definition) {}
45
+ @auto_include_in_classes = %w[ActionController::Base ActionController::API]
44
46
  end
45
47
  end
46
48
 
@@ -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)],
@@ -17,15 +17,14 @@ module RailsParamValidation
17
17
 
18
18
  initializer 'rails_param_validation.action_controller_extension' do
19
19
  ActiveSupport.on_load(:action_controller) do
20
- ActionController::Base.send :include, ActionControllerExtension
21
- ActionController::Base.send :include, AnnotationExtension
22
- ActionController::Base.send :include, CustomTypesExtension
23
- ActionController::Base.send :extend, RailsParamValidation::Types
24
-
25
- ActionController::API.send :include, ActionControllerExtension
26
- ActionController::API.send :include, AnnotationExtension
27
- ActionController::API.send :include, CustomTypesExtension
28
- ActionController::API.send :extend, RailsParamValidation::Types
20
+ RailsParamValidation.config.auto_include_in_classes.each do |klass_name|
21
+ klass = klass_name.constantize
22
+
23
+ klass.send :include, ActionControllerExtension
24
+ klass.send :include, AnnotationExtension
25
+ klass.send :include, CustomTypesExtension
26
+ klass.send :extend, RailsParamValidation::Types
27
+ end
29
28
  end
30
29
  end
31
30
 
@@ -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."
@@ -2,7 +2,7 @@ module RailsParamValidation
2
2
 
3
3
  def self.register(type, schema)
4
4
  AnnotationTypes::CustomT.register(
5
- Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type),
5
+ Types::Namespace.with_namespace(Types::Namespace.fetch(Types::Namespace.caller_file), type),
6
6
  schema
7
7
  )
8
8
  end
@@ -89,6 +89,8 @@ module Types
89
89
 
90
90
  path.pop
91
91
  end
92
+
93
+ nil
92
94
  end
93
95
 
94
96
  def self.caller_file
@@ -140,7 +142,11 @@ module Types
140
142
  end
141
143
 
142
144
  def Type(type)
143
- AnnotationTypes::CustomT.new(Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type))
145
+ if type.is_a?(Class)
146
+ AnnotationTypes::CustomT.new(RailsHelper.clean_name(type))
147
+ else
148
+ AnnotationTypes::CustomT.new(Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type))
149
+ end
144
150
  end
145
151
  end
146
152
 
@@ -6,11 +6,6 @@ module RailsParamValidation
6
6
  end
7
7
 
8
8
  def matches?(path, data)
9
- if schema.schema.is_a? NilClass
10
- puts path
11
- puts schema.inspect
12
- end
13
-
14
9
  ValidatorFactory.create(schema.schema).matches? path, data
15
10
  end
16
11
 
@@ -29,4 +24,4 @@ module RailsParamValidation
29
24
  end
30
25
  end
31
26
 
32
- end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsParamValidation
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.5"
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.2
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oskar Kirmis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-04 00:00:00.000000000 Z
11
+ date: 2022-04-19 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.1.2
96
+ rubygems_version: 3.3.11
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Declarative parameter definition and validation for Rails