rails-param-validation 0.2.1 → 0.3.2

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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -0
  3. data/.gitlab-ci.yml +0 -0
  4. data/Gemfile +0 -0
  5. data/LICENSE.txt +0 -0
  6. data/README.md +0 -0
  7. data/Rakefile +0 -0
  8. data/docs/_config.yml +0 -0
  9. data/docs/annotations.md +0 -0
  10. data/docs/getting-started.md +0 -0
  11. data/docs/image/error-screenshot.png +0 -0
  12. data/docs/index.md +0 -0
  13. data/docs/main-idea.md +0 -0
  14. data/docs/openapi.md +0 -0
  15. data/docs/type-definition.md +3 -3
  16. data/lib/rails-param-validation.rb +0 -0
  17. data/lib/rails-param-validation/errors/missing_parameter_annotation.rb +0 -0
  18. data/lib/rails-param-validation/errors/no_matching_factory.rb +0 -0
  19. data/lib/rails-param-validation/errors/param_validation_failed_error.rb +0 -0
  20. data/lib/rails-param-validation/errors/type_not_found.rb +0 -0
  21. data/lib/rails-param-validation/rails/action_definition.rb +16 -2
  22. data/lib/rails-param-validation/rails/annotation_manager.rb +0 -0
  23. data/lib/rails-param-validation/rails/config.rb +8 -1
  24. data/lib/rails-param-validation/rails/extensions/annotation_extension.rb +12 -2
  25. data/lib/rails-param-validation/rails/extensions/custom_type_extension.rb +0 -0
  26. data/lib/rails-param-validation/rails/extensions/error.template.html.erb +0 -0
  27. data/lib/rails-param-validation/rails/extensions/validation_extension.rb +1 -3
  28. data/lib/rails-param-validation/rails/helper.rb +6 -1
  29. data/lib/rails-param-validation/rails/openapi/openapi.rb +8 -4
  30. data/lib/rails-param-validation/rails/openapi/routing_helper.rb +3 -1
  31. data/lib/rails-param-validation/rails/rails.rb +11 -9
  32. data/lib/rails-param-validation/rails/tasks/openapi.rake +2 -2
  33. data/lib/rails-param-validation/types/types.rb +0 -0
  34. data/lib/rails-param-validation/validator.rb +0 -0
  35. data/lib/rails-param-validation/validator_factory.rb +0 -0
  36. data/lib/rails-param-validation/validators/alternatives.rb +0 -0
  37. data/lib/rails-param-validation/validators/array.rb +0 -0
  38. data/lib/rails-param-validation/validators/boolean.rb +0 -0
  39. data/lib/rails-param-validation/validators/constant.rb +4 -2
  40. data/lib/rails-param-validation/validators/custom_type.rb +1 -2
  41. data/lib/rails-param-validation/validators/date.rb +0 -0
  42. data/lib/rails-param-validation/validators/datetime.rb +0 -0
  43. data/lib/rails-param-validation/validators/float.rb +0 -0
  44. data/lib/rails-param-validation/validators/hash.rb +0 -0
  45. data/lib/rails-param-validation/validators/integer.rb +0 -0
  46. data/lib/rails-param-validation/validators/object.rb +0 -0
  47. data/lib/rails-param-validation/validators/optional.rb +0 -0
  48. data/lib/rails-param-validation/validators/regex.rb +0 -0
  49. data/lib/rails-param-validation/validators/string.rb +0 -0
  50. data/lib/rails-param-validation/validators/uuid.rb +0 -0
  51. data/lib/rails-param-validation/version.rb +1 -1
  52. data/rails-param-validation.gemspec +0 -0
  53. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 611940a379b0bcf38dae5ec17504074523e2a6d169669f0de05623380c6c0ed0
4
- data.tar.gz: f3e7b0c51e9504a915a36d27b8f9e6289845046fa87d9e5b3d612cc18ca3a498
3
+ metadata.gz: c91d17285ccb7056d82f4765816c11097127528e459cc76ed720fe81e14ed9b6
4
+ data.tar.gz: 680f1b12419968f6528a48552a9d0c9327cdae7c7a0072d2ed43189b91784854
5
5
  SHA512:
6
- metadata.gz: 3eb0f67d39cda8c5737839abda8f720c559d9faf1062e5a873c8835eb98b9beeb988d461750f997705744eaf3f142376db5e5753d1f88495965126ad22ad9599
7
- data.tar.gz: fe3696bdfef9b9697c838c26b6314810b0b7a9199da23bbdd874331ed20aca7d78824fd96c61fc4db75c8f9bb1073b61a23c962c72d6281df2f3731d18d84349
6
+ metadata.gz: cda64c3b3a95f3ecbc96381f9e5b2e4681b0c6d2b3dde14e6d3e2037e8c65d1b9dd70663aae52739271914086327e0ce913e685e346af883135b37d33762e9dd
7
+ data.tar.gz: b39531488e543569efa588e35c8f3fc887e14e2891f0243817f2fb4476470315d609854b90d032f895205c626a6546cbaab686876dea7a517a947f5e1ed40171
data/.gitignore CHANGED
File without changes
File without changes
data/Gemfile CHANGED
File without changes
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -82,14 +82,14 @@ In this case, `params[:radius].is_a? Float` returns `true`.
82
82
  <a name="type-boolean"></a>
83
83
  ### Boolean
84
84
 
85
- Accepts a float or a string, which represents a boolean (either `"true"` or `"false"`).
85
+ Accepts a boolean or a string, which represents a boolean (either `"true"` or `"false"`).
86
86
 
87
87
  Example:
88
88
  ```ruby
89
- query_param :radius, Float, "Search radius"
89
+ query_param :published, Boolean, "Indicate if a blog post is published"
90
90
  ```
91
91
 
92
- In this case, `params[:radius].is_a? Float` returns `true`.
92
+ In this case, `params[:published].is_a?(TrueClass) || params[:published].is_a?(FalseClass)` returns `true`.
93
93
 
94
94
 
95
95
  <a name="type-uuid"></a>
File without changes
@@ -1,7 +1,7 @@
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
5
5
  attr_accessor :description
6
6
 
7
7
  def initialize
@@ -11,6 +11,8 @@ module RailsParamValidation
11
11
  @description = ''
12
12
  @request_body_type = RailsParamValidation.config.default_body_content_type if defined?(Rails)
13
13
  @responses = {}
14
+ @flags = {}
15
+ @security = []
14
16
  end
15
17
 
16
18
  def store_origin!(controller, action)
@@ -38,6 +40,18 @@ module RailsParamValidation
38
40
  }
39
41
  end
40
42
 
43
+ def add_security(security)
44
+ @security.push(security.is_a?(Hash) ? security : { security => [] })
45
+ end
46
+
47
+ def add_flag(name, value)
48
+ @flags[name.to_sym] = value
49
+ end
50
+
51
+ def flag(name, default)
52
+ @flags.fetch(name, default)
53
+ end
54
+
41
55
  def add_response(status, schema, description)
42
56
  @responses[status] = {
43
57
  schema: schema,
@@ -51,7 +65,7 @@ module RailsParamValidation
51
65
 
52
66
  def finalize!(class_name, method_name)
53
67
  @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
68
+ name = "#{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
55
69
  AnnotationTypes::CustomT.register(name, response[:schema])
56
70
 
57
71
  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
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
@@ -10,6 +10,9 @@ module RailsParamValidation
10
10
  self.title = app_name(app_class)
11
11
  self.version = '1.0'
12
12
  self.description = "#{app_name(app_class)} application"
13
+ self.file_path = Rails.root.join("openapi.yaml").to_s
14
+ self.security_schemes = {}
15
+ self.skip_format_endpoints = true
13
16
  end
14
17
 
15
18
  private
@@ -26,6 +29,8 @@ module RailsParamValidation
26
29
  attr_accessor :use_validator_caching
27
30
  attr_accessor :raise_on_missing_annotation
28
31
  attr_accessor :default_body_content_type
32
+ attr_accessor :default_action_flags
33
+ attr_accessor :post_action_definition_hook
29
34
  attr_reader :openapi
30
35
 
31
36
  def initialize
@@ -34,6 +39,8 @@ module RailsParamValidation
34
39
  @use_validator_caching = Rails.env.production?
35
40
  @raise_on_missing_annotation = true
36
41
  @default_body_content_type = 'application/json'
42
+ @default_action_flags = {}
43
+ @post_action_definition_hook = ->(_action_definition) {}
37
44
  end
38
45
  end
39
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
+ 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)
88
97
  @param_definition = ActionDefinition.new
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
@@ -38,9 +38,7 @@ module RailsParamValidation
38
38
 
39
39
  if result.matches?
40
40
  # Copy the parameters if the validation succeeded
41
- @validated_parameters = ActiveSupport::HashWithIndifferentAccess.new(
42
- result.value.merge(action: action, controller: controller)
43
- )
41
+ @validated_parameters = result.value.merge(action: action, controller: controller)
44
42
  else
45
43
  # Render an appropriate error message
46
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('::').map { |p| p.capitalize }.join.to_sym
11
+ end
7
12
  end
8
13
 
9
- end
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,7 @@ 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 = "#{RailsHelper.clean_controller_name operation.controller}#{operation.action.capitalize}Body".to_sym
73
74
  AnnotationTypes::CustomT.register(body_type_name, body)
74
75
 
75
76
  action_definition[:requestBody] = {
@@ -90,6 +91,9 @@ module RailsParamValidation
90
91
  object[:components][:schemas][name] = ValidatorFactory.create(AnnotationTypes::CustomT.registered(name)).to_openapi
91
92
  end
92
93
 
94
+ if RailsParamValidation.openapi.security_schemes.any?
95
+ object[:components][:securitySchemes] = RailsParamValidation.openapi.security_schemes
96
+ end
93
97
  stringify_values object
94
98
  end
95
99
 
@@ -100,7 +104,7 @@ module RailsParamValidation
100
104
  description = AnnotationManager.instance.class_annotation klass, :description
101
105
 
102
106
  if description
103
- @tags[RailsHelper.controller_to_tag klass] = description
107
+ @tags[RailsHelper.controller_to_tag klass.constantize] = description
104
108
  end
105
109
 
106
110
  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
@@ -16,15 +16,17 @@ module RailsParamValidation
16
16
  railtie_name :param_validation
17
17
 
18
18
  initializer 'rails_param_validation.action_controller_extension' do
19
- ActionController::Base.send :include, ActionControllerExtension
20
- ActionController::Base.send :include, AnnotationExtension
21
- ActionController::Base.send :include, CustomTypesExtension
22
- ActionController::Base.send :extend, RailsParamValidation::Types
23
-
24
- ActionController::API.send :include, ActionControllerExtension
25
- ActionController::API.send :include, AnnotationExtension
26
- ActionController::API.send :include, CustomTypesExtension
27
- ActionController::API.send :extend, RailsParamValidation::Types
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
29
+ end
28
30
  end
29
31
 
30
32
  rake_tasks do
@@ -1,6 +1,6 @@
1
1
  namespace :openapi do
2
2
 
3
- desc "Export OpenAPI definition to to openapi.yaml"
3
+ desc "Export OpenAPI definition to openapi.yaml"
4
4
  task export: :environment do
5
5
  # Ensure all controllers are loaded
6
6
  if defined? Zeitwerk
@@ -18,7 +18,7 @@ namespace :openapi do
18
18
  RailsParamValidation.openapi.description
19
19
  )
20
20
 
21
- filename = Rails.root.join("openapi.yaml").to_s
21
+ filename = RailsParamValidation.openapi.file_path
22
22
  print "Writing #{filename}..."
23
23
 
24
24
  begin
File without changes
File without changes
@@ -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
 
@@ -35,4 +37,4 @@ module RailsParamValidation
35
37
  end
36
38
  end
37
39
 
38
- end
40
+ end
@@ -3,11 +3,10 @@ module RailsParamValidation
3
3
  class CustomTypeValidator < Validator
4
4
  def initialize(type)
5
5
  super type
6
- @validator = ValidatorFactory.create type.schema
7
6
  end
8
7
 
9
8
  def matches?(path, data)
10
- @validator.matches? path, data
9
+ ValidatorFactory.create(schema.schema).matches? path, data
11
10
  end
12
11
 
13
12
  def to_openapi
@@ -1,3 +1,3 @@
1
1
  module RailsParamValidation
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.2"
3
3
  end
File without changes
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.1
4
+ version: 0.3.2
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-06-20 00:00:00.000000000 Z
11
+ date: 2020-07-29 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.1
96
+ rubygems_version: 3.0.2
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Declarative parameter definition and validation for Rails