rails-param-validation 0.1.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -0
- data/.gitlab-ci.yml +0 -0
- data/Gemfile +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +0 -0
- data/Rakefile +0 -0
- data/docs/_config.yml +0 -0
- data/docs/annotations.md +4 -4
- data/docs/getting-started.md +0 -0
- data/docs/image/error-screenshot.png +0 -0
- data/docs/index.md +0 -0
- data/docs/main-idea.md +0 -0
- data/docs/openapi.md +0 -0
- data/docs/type-definition.md +3 -3
- data/lib/rails-param-validation.rb +0 -0
- data/lib/rails-param-validation/errors/missing_parameter_annotation.rb +0 -0
- data/lib/rails-param-validation/errors/no_matching_factory.rb +0 -0
- data/lib/rails-param-validation/errors/param_validation_failed_error.rb +0 -0
- data/lib/rails-param-validation/errors/type_not_found.rb +0 -0
- data/lib/rails-param-validation/rails/action_definition.rb +15 -1
- data/lib/rails-param-validation/rails/annotation_manager.rb +0 -0
- data/lib/rails-param-validation/rails/config.rb +8 -1
- data/lib/rails-param-validation/rails/extensions/annotation_extension.rb +12 -2
- data/lib/rails-param-validation/rails/extensions/custom_type_extension.rb +0 -0
- data/lib/rails-param-validation/rails/extensions/error.template.html.erb +0 -0
- data/lib/rails-param-validation/rails/extensions/validation_extension.rb +21 -13
- data/lib/rails-param-validation/rails/helper.rb +0 -0
- data/lib/rails-param-validation/rails/openapi/openapi.rb +4 -0
- data/lib/rails-param-validation/rails/openapi/routing_helper.rb +3 -1
- data/lib/rails-param-validation/rails/rails.rb +11 -4
- data/lib/rails-param-validation/rails/tasks/openapi.rake +2 -2
- data/lib/rails-param-validation/types/types.rb +4 -0
- data/lib/rails-param-validation/validator.rb +0 -0
- data/lib/rails-param-validation/validator_factory.rb +0 -0
- data/lib/rails-param-validation/validators/alternatives.rb +0 -0
- data/lib/rails-param-validation/validators/array.rb +0 -0
- data/lib/rails-param-validation/validators/boolean.rb +0 -0
- data/lib/rails-param-validation/validators/constant.rb +0 -0
- data/lib/rails-param-validation/validators/custom_type.rb +1 -2
- data/lib/rails-param-validation/validators/date.rb +0 -0
- data/lib/rails-param-validation/validators/datetime.rb +0 -0
- data/lib/rails-param-validation/validators/float.rb +0 -0
- data/lib/rails-param-validation/validators/hash.rb +0 -0
- data/lib/rails-param-validation/validators/integer.rb +0 -0
- data/lib/rails-param-validation/validators/object.rb +0 -0
- data/lib/rails-param-validation/validators/optional.rb +0 -0
- data/lib/rails-param-validation/validators/regex.rb +0 -0
- data/lib/rails-param-validation/validators/string.rb +0 -0
- data/lib/rails-param-validation/validators/uuid.rb +0 -0
- data/lib/rails-param-validation/version.rb +1 -1
- data/rails-param-validation.gemspec +0 -0
- 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: 1e81185301762be6703608dfc575c27813c49fde9969057c0b821c7cc78f7680
|
4
|
+
data.tar.gz: 5c3c75f3d2a8e6275d1c794540d972e62b13ada2592a5602ca71bb80794d6f05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1af2e48e2ef3d6af489e009177839378b5e19b8817a456a9cb1e399e525b2a974d52ed1986a321c44035248c527d8ef1b7711de0e175252791a12567a8bd98da
|
7
|
+
data.tar.gz: b4528b9e92d0c7079cced511255760766771ba6b6f9e5f013d8f1049bdf1fce6f10f86750760bae7ebb12d53ce66b027d18d4228c7dd953d5f1e6fa70af60979
|
data/.gitignore
CHANGED
File without changes
|
data/.gitlab-ci.yml
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/docs/_config.yml
CHANGED
File without changes
|
data/docs/annotations.md
CHANGED
@@ -24,15 +24,15 @@ end
|
|
24
24
|
|
25
25
|
**query_param**
|
26
26
|
|
27
|
-
Parameter passed via query string, e.g. `http://localhost/my_action?parameter=value`. The first value is the parameter name (which must be a symbol), the second is the [type definition](./
|
27
|
+
Parameter passed via query string, e.g. `http://localhost/my_action?parameter=value`. The first value is the parameter name (which must be a symbol), the second is the [type definition](./type-definition.md). The last (optional) parameter to this call is the description.
|
28
28
|
|
29
29
|
**path_param**
|
30
30
|
|
31
|
-
Parameter passed as part of the actions route, e.g. `http://localhost/my_action/value` where the route definition is something like `get '/my_action/:parameter, to: 'my_controller#my_action'`. The first value is the parameter name (which must be a symbol), the second is the [type definition](./
|
31
|
+
Parameter passed as part of the actions route, e.g. `http://localhost/my_action/value` where the route definition is something like `get '/my_action/:parameter, to: 'my_controller#my_action'`. The first value is the parameter name (which must be a symbol), the second is the [type definition](./type-definition.md). The last (optional) parameter to this call is the description.
|
32
32
|
|
33
33
|
**body_param**
|
34
34
|
|
35
|
-
Parameter as part of a JSON body in a POST/PUT/PATCH operation. The first value is the parameter name (which must be a symbol) as the JSON body must be a JSON object which has a key named like the parameter. The second parameter is the [type definition](./
|
35
|
+
Parameter as part of a JSON body in a POST/PUT/PATCH operation. The first value is the parameter name (which must be a symbol) as the JSON body must be a JSON object which has a key named like the parameter. The second parameter is the [type definition](./type-definition.md). The last (optional) parameter to this call is the description.
|
36
36
|
|
37
37
|
Example body:
|
38
38
|
|
@@ -55,7 +55,7 @@ end
|
|
55
55
|
|
56
56
|
**response**
|
57
57
|
|
58
|
-
The response annotation defines the possible responses the action can generate, where the first parameter is the HTTP status code, the second is the [type definition](./
|
58
|
+
The response annotation defines the possible responses the action can generate, where the first parameter is the HTTP status code, the second is the [type definition](./type-definition.md). The last (optional) parameter to this call is the description.
|
59
59
|
|
60
60
|
**accept_all_params**
|
61
61
|
|
data/docs/getting-started.md
CHANGED
File without changes
|
File without changes
|
data/docs/index.md
CHANGED
File without changes
|
data/docs/main-idea.md
CHANGED
File without changes
|
data/docs/openapi.md
CHANGED
File without changes
|
data/docs/type-definition.md
CHANGED
@@ -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
|
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 :
|
89
|
+
query_param :published, Boolean, "Indicate if a blog post is published"
|
90
90
|
```
|
91
91
|
|
92
|
-
In this case, `params[:
|
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
|
File without changes
|
File without changes
|
File without changes
|
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,
|
File without changes
|
@@ -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
|
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
|
File without changes
|
File without changes
|
@@ -50,22 +50,30 @@ module RailsParamValidation
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def _render_invalid_param_response(result)
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
if respond_to? :respond_to
|
54
|
+
# Depending on the accept header, choose the way to answer
|
55
|
+
respond_to do |format|
|
56
|
+
format.html do
|
57
|
+
if RailsParamValidation.config.use_default_html_response
|
58
|
+
_create_html_error result
|
59
|
+
else
|
60
|
+
raise ParamValidationFailedError.new(result)
|
61
|
+
end
|
60
62
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
format.json do
|
64
|
+
if RailsParamValidation.config.use_default_json_response
|
65
|
+
_create_json_error result
|
66
|
+
else
|
67
|
+
raise ParamValidationFailedError.new(result)
|
68
|
+
end
|
67
69
|
end
|
68
70
|
end
|
71
|
+
else
|
72
|
+
if RailsParamValidation.config.use_default_json_response
|
73
|
+
_create_json_error result
|
74
|
+
else
|
75
|
+
raise ParamValidationFailedError.new(result)
|
76
|
+
end
|
69
77
|
end
|
70
78
|
end
|
71
79
|
|
File without changes
|
@@ -51,6 +51,7 @@ module RailsParamValidation
|
|
51
51
|
operationId: "#{route[:method].downcase}#{route[:path].split(/[^a-zA-Z0-9]+/).map(&:downcase).map(&:capitalize).join}",
|
52
52
|
tags: [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,
|
@@ -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
|
|
@@ -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
|
-
|
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,10 +16,17 @@ module RailsParamValidation
|
|
16
16
|
railtie_name :param_validation
|
17
17
|
|
18
18
|
initializer 'rails_param_validation.action_controller_extension' do
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
23
30
|
end
|
24
31
|
|
25
32
|
rake_tasks do
|
@@ -1,6 +1,6 @@
|
|
1
1
|
namespace :openapi do
|
2
2
|
|
3
|
-
desc "Export OpenAPI definition to
|
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 =
|
21
|
+
filename = RailsParamValidation.openapi.file_path
|
22
22
|
print "Writing #{filename}..."
|
23
23
|
|
24
24
|
begin
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -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
|
-
|
9
|
+
ValidatorFactory.create(schema.schema).matches? path, data
|
11
10
|
end
|
12
11
|
|
13
12
|
def to_openapi
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
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.
|
4
|
+
version: 0.3.0
|
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-
|
11
|
+
date: 2020-07-27 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.
|
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
|