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 +4 -4
- data/lib/rails-param-validation/rails/action_definition.rb +5 -1
- data/lib/rails-param-validation/rails/config.rb +2 -0
- data/lib/rails-param-validation/rails/extensions/annotation_extension.rb +19 -1
- data/lib/rails-param-validation/rails/helper.rb +6 -1
- data/lib/rails-param-validation/rails/openapi/openapi.rb +1 -1
- data/lib/rails-param-validation/rails/rails.rb +8 -9
- data/lib/rails-param-validation/rails/tasks/openapi.rake +2 -2
- data/lib/rails-param-validation/types/types.rb +8 -2
- data/lib/rails-param-validation/validators/custom_type.rb +1 -6
- data/lib/rails-param-validation/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cac5e8e1680a9f3cbdce7cd511d39f4b95d0cddf9f2955e9bbbde3ff56daa5b
|
4
|
+
data.tar.gz: a2f4422840a284a0658fa9c241bcda19edbcf7ad3927bc94a79ec38178b86e5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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.
|
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
|
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
|
-
|
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
|
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
|
+
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:
|
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.
|
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
|