rails-param-validation 0.2.3 → 0.4.1

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: b4a98d65517bb634880f15f8646ee1645a60c00a962c7c604df80c9ead49404f
4
- data.tar.gz: 7e755c2e7011fa1c848c1f4cf59ca93232a8829ba081c11b859707e652ed3a9f
3
+ metadata.gz: 430b5d7c83e83717e20bd96b20961fb51e617e74bb634ee356fdfaf67af9b66f
4
+ data.tar.gz: e615c6d36cc776b3c2fe8056dd4aa49cc2916a5c591ce06d5b7cccc0c0a56a7d
5
5
  SHA512:
6
- metadata.gz: ceb39335b93f488396532b5317a7cc48ddd633e795417bb30b4c628f790778864024ce83e67affb2a4e91713b9e8f2f8336bdfb3d4998714fbf3ca1df6112518
7
- data.tar.gz: 811e141a19bcafadc2a0270620ad844ae104249b45dd9581b0d083694b48874914dba256a304842a4d60fbbc5316ab7013a35027c693166b04ce2c07caeb39e4
6
+ metadata.gz: 87f12c586949c487bb699b6bfe7e0acd52d5a486c15fe6f2dd0e20d17d16ac725249be3fb0ace47553d5df8460bbba763a870193f2f960ed857dba2ceab1d75f
7
+ data.tar.gz: 01122bc9d7041b99168647219d5b7d8896677b1c3d6cbe394e2e67d3c641f4766a32bea25fcb4f175112ebc89128ba14345f715bba58a7f5181bddf2480722b7
@@ -1,16 +1,19 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class ActionDefinition
4
- attr_reader :params, :request_body_type, :paths, :responses, :controller, :action
4
+ attr_reader :params, :request_body_type, :paths, :responses, :controller, :action, :security, :source_file
5
5
  attr_accessor :description
6
6
 
7
- def initialize
7
+ def initialize(source_file)
8
8
  @params = {}
9
9
  @paths = []
10
10
  @param_validation_enabled = true
11
11
  @description = ''
12
12
  @request_body_type = RailsParamValidation.config.default_body_content_type if defined?(Rails)
13
13
  @responses = {}
14
+ @flags = {}
15
+ @source_file = source_file
16
+ @security = []
14
17
  end
15
18
 
16
19
  def store_origin!(controller, action)
@@ -38,6 +41,18 @@ module RailsParamValidation
38
41
  }
39
42
  end
40
43
 
44
+ def add_security(security)
45
+ @security.push(security.is_a?(Hash) ? security : { security => [] })
46
+ end
47
+
48
+ def add_flag(name, value)
49
+ @flags[name.to_sym] = value
50
+ end
51
+
52
+ def flag(name, default)
53
+ @flags.fetch(name, default)
54
+ end
55
+
41
56
  def add_response(status, schema, description)
42
57
  @responses[status] = {
43
58
  schema: schema,
@@ -51,7 +66,10 @@ module RailsParamValidation
51
66
 
52
67
  def finalize!(class_name, method_name)
53
68
  @responses.each do |code, response|
54
- name = "#{class_name.to_s.capitalize}#{method_name.to_s.camelcase}#{Rack::Utils::SYMBOL_TO_STATUS_CODE.key(code).to_s.camelize}Response".to_sym
69
+ name = Types::Namespace.with_namespace(
70
+ Types::Namespace.fetch(@source_file),
71
+ "#{RailsHelper.clean_controller_name(class_name)}.#{method_name.to_s.camelcase}.#{Rack::Utils::SYMBOL_TO_STATUS_CODE.key(code).to_s.camelize}Response".to_sym
72
+ )
55
73
  AnnotationTypes::CustomT.register(name, response[:schema])
56
74
 
57
75
  response.merge!(schema: AnnotationTypes::CustomT.new(name))
@@ -1,7 +1,7 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class OpenApiMetaConfig
4
- attr_accessor :title, :version, :url, :description, :file_path
4
+ attr_accessor :title, :version, :url, :description, :file_path, :security_schemes, :skip_format_endpoints
5
5
 
6
6
  def initialize
7
7
  app_class = Rails.application.class
@@ -11,6 +11,8 @@ module RailsParamValidation
11
11
  self.version = '1.0'
12
12
  self.description = "#{app_name(app_class)} application"
13
13
  self.file_path = Rails.root.join("openapi.yaml").to_s
14
+ self.security_schemes = {}
15
+ self.skip_format_endpoints = true
14
16
  end
15
17
 
16
18
  private
@@ -27,6 +29,8 @@ module RailsParamValidation
27
29
  attr_accessor :use_validator_caching
28
30
  attr_accessor :raise_on_missing_annotation
29
31
  attr_accessor :default_body_content_type
32
+ attr_accessor :default_action_flags
33
+ attr_accessor :post_action_definition_hook
30
34
  attr_reader :openapi
31
35
 
32
36
  def initialize
@@ -35,6 +39,8 @@ module RailsParamValidation
35
39
  @use_validator_caching = Rails.env.production?
36
40
  @raise_on_missing_annotation = true
37
41
  @default_body_content_type = 'application/json'
42
+ @default_action_flags = {}
43
+ @post_action_definition_hook = ->(_action_definition) {}
38
44
  end
39
45
  end
40
46
 
@@ -22,6 +22,7 @@ module RailsParamValidation
22
22
  method_name = name.to_sym
23
23
 
24
24
  @param_definition.store_origin! class_name, method_name
25
+ RailsParamValidation.config.post_action_definition_hook.call(@param_definition)
25
26
  @param_definition.finalize! class_name, method_name
26
27
 
27
28
  AnnotationManager.instance.annotate_method! self, name, :param_definition, @param_definition
@@ -84,12 +85,21 @@ module RailsParamValidation
84
85
  param_definition.add_response status, schema, description
85
86
  end
86
87
 
87
- def action(description = nil)
88
- @param_definition = ActionDefinition.new
88
+ def flag(name, value, _comment = nil)
89
+ @param_definition.add_flag name, value
90
+ end
91
+
92
+ def security_hint(security)
93
+ @param_definition.add_security(security)
94
+ end
95
+
96
+ def action(description = nil, flags = RailsParamValidation.config.default_action_flags)
97
+ @param_definition = ActionDefinition.new(Types::Namespace.caller_file)
89
98
  @param_definition.description = description
99
+ flags.each { |name, value| @param_definition.add_flag name, value }
90
100
 
91
101
  yield
92
102
  end
93
103
  end
94
104
  end
95
- end
105
+ end
@@ -6,7 +6,8 @@ module RailsParamValidation
6
6
 
7
7
  module ClassMethods
8
8
  def declare(type, schema)
9
- RailsParamValidation::AnnotationTypes::CustomT.register type, schema
9
+ namespace = Types::Namespace.fetch(Types::Namespace.caller_file)
10
+ RailsParamValidation::AnnotationTypes::CustomT.register Types::Namespace.with_namespace(namespace, type), schema
10
11
  end
11
12
  end
12
13
  end
@@ -38,7 +38,7 @@ module RailsParamValidation
38
38
 
39
39
  if result.matches?
40
40
  # Copy the parameters if the validation succeeded
41
- @validated_parameters = result.value.merge(action: action, controller: controller)
41
+ @validated_parameters = result.value.merge('action' => action, 'controller' => controller)
42
42
  else
43
43
  # Render an appropriate error message
44
44
  _render_invalid_param_response result
@@ -4,6 +4,11 @@ module RailsParamValidation
4
4
  def self.controller_to_tag(klass)
5
5
  (klass.is_a?(String) ? klass : klass.name).gsub(/Controller$/, '').to_sym
6
6
  end
7
+
8
+ def self.clean_controller_name(klass)
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
11
+ end
7
12
  end
8
13
 
9
14
  end
@@ -25,7 +25,7 @@ module RailsParamValidation
25
25
  openapi: OPENAPI_VERSION,
26
26
  info: { version: @info[:version], title: @info[:title], description: @info[:description] },
27
27
  servers: @info[:url].map { |url| { url: url } },
28
- tags: @tags.map { |tag, description| { name: tag, description: description } },
28
+ tags: @tags.map { |tag, description| { name: RailsHelper.clean_controller_name(tag), description: description } },
29
29
  paths: {},
30
30
  components: { schemas: {} }
31
31
  }
@@ -49,8 +49,9 @@ module RailsParamValidation
49
49
  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
- tags: [operation.controller],
52
+ tags: [RailsHelper.clean_controller_name(operation.controller)],
53
53
  parameters: parameters,
54
+ security: operation.security,
54
55
  responses: operation.responses.map do |status, values|
55
56
  [
56
57
  status.to_s,
@@ -69,7 +70,10 @@ module RailsParamValidation
69
70
  action_definition.merge!(summary: operation.description) if operation.description.present?
70
71
 
71
72
  if body.any?
72
- body_type_name = "#{operation.controller.capitalize}#{operation.action.capitalize}Body".to_sym
73
+ body_type_name = Types::Namespace.with_namespace(
74
+ Types::Namespace.fetch(operation.source_file),
75
+ "#{RailsHelper.clean_controller_name operation.controller}.#{operation.action.to_s.camelcase}.Body".to_sym
76
+ )
73
77
  AnnotationTypes::CustomT.register(body_type_name, body)
74
78
 
75
79
  action_definition[:requestBody] = {
@@ -90,6 +94,9 @@ module RailsParamValidation
90
94
  object[:components][:schemas][name] = ValidatorFactory.create(AnnotationTypes::CustomT.registered(name)).to_openapi
91
95
  end
92
96
 
97
+ if RailsParamValidation.openapi.security_schemes.any?
98
+ object[:components][:securitySchemes] = RailsParamValidation.openapi.security_schemes
99
+ end
93
100
  stringify_values object
94
101
  end
95
102
 
@@ -100,7 +107,7 @@ module RailsParamValidation
100
107
  description = AnnotationManager.instance.class_annotation klass, :description
101
108
 
102
109
  if description
103
- @tags[RailsHelper.controller_to_tag klass] = description
110
+ @tags[RailsHelper.controller_to_tag klass.constantize] = description
104
111
  end
105
112
 
106
113
  AnnotationManager.instance.methods(klass).each do |method|
@@ -29,7 +29,9 @@ module RailsParamValidation
29
29
  Rails.application.routes.routes.each do |route|
30
30
  if route.defaults[:controller] == controller && route.defaults[:action] == action
31
31
  Formatter.new.accept(route.path.ast, [""]).each do |path|
32
- routes.push(path: path, method: route.verb)
32
+ if !RailsParamValidation.openapi.skip_format_endpoints || !path.end_with?('.{format}')
33
+ routes.push(path: path, method: route.verb)
34
+ end
33
35
  end
34
36
  end
35
37
  end
@@ -1,7 +1,10 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  def self.register(type, schema)
4
- AnnotationTypes::CustomT.register type, schema
4
+ AnnotationTypes::CustomT.register(
5
+ Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type),
6
+ schema
7
+ )
5
8
  end
6
9
 
7
10
  module AnnotationTypes
@@ -72,6 +75,58 @@ end
72
75
  end
73
76
 
74
77
  module Types
78
+ class Namespace
79
+ def self.store(scope, namespace)
80
+ map[scope] = namespace
81
+ end
82
+
83
+ def self.fetch(scope)
84
+ path = scope.split('/')
85
+
86
+ (path.size - 1).times do
87
+ key = path.join '/'
88
+ return map[key] if map.key? key
89
+
90
+ path.pop
91
+ end
92
+ end
93
+
94
+ def self.caller_file
95
+ caller_locations(2, 1)[0].path
96
+ end
97
+
98
+ def self.with_namespace(namespace, type)
99
+ if namespace
100
+ path = namespace.to_s.split('.')
101
+
102
+ while type.start_with?("_")
103
+ type = type[1..-1]
104
+ path.pop
105
+ end
106
+
107
+ "#{path.join(".")}.#{type}".to_sym
108
+ else
109
+ type
110
+ end
111
+ end
112
+
113
+ class << self
114
+ protected
115
+
116
+ def map
117
+ @map ||= { '' => nil }
118
+ end
119
+ end
120
+ end
121
+
122
+ def FileNamespace(namespace)
123
+ Namespace.store Namespace.caller_file, namespace
124
+ end
125
+
126
+ def DirectoryNamespace(namespace)
127
+ Namespace.store File.dirname(caller_locations(1, 1)[0].path), namespace
128
+ end
129
+
75
130
  def ArrayType(inner_type)
76
131
  AnnotationTypes::ArrayT.new(inner_type)
77
132
  end
@@ -85,7 +140,7 @@ module Types
85
140
  end
86
141
 
87
142
  def Type(type)
88
- AnnotationTypes::CustomT.new(type)
143
+ AnnotationTypes::CustomT.new(Namespace.with_namespace(Namespace.fetch(Namespace.caller_file), type))
89
144
  end
90
145
  end
91
146
 
@@ -1,6 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class ConstantValidator < Validator
4
+ CLASS_MAP = { TrueClass => Boolean, FalseClass => Boolean }
5
+
4
6
  def initialize(schema)
5
7
  super schema
6
8
 
@@ -21,7 +23,7 @@ module RailsParamValidation
21
23
  end
22
24
 
23
25
  def to_openapi
24
- ValidatorFactory.create(schema.class).to_openapi.merge(enum: [schema])
26
+ ValidatorFactory.create(CLASS_MAP.fetch(schema.class, schema.class)).to_openapi.merge(enum: [schema])
25
27
  end
26
28
  end
27
29
 
@@ -6,6 +6,11 @@ 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
+
9
14
  ValidatorFactory.create(schema.schema).matches? path, data
10
15
  end
11
16
 
@@ -24,4 +29,4 @@ module RailsParamValidation
24
29
  end
25
30
  end
26
31
 
27
- end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsParamValidation
2
- VERSION = "0.2.3"
2
+ VERSION = "0.4.1"
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.2.3
4
+ version: 0.4.1
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-07-23 00:00:00.000000000 Z
11
+ date: 2020-08-04 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.0.2
96
+ rubygems_version: 3.1.2
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Declarative parameter definition and validation for Rails