rails-param-validation 0.1.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
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 +4 -4
  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 +0 -0
  22. data/lib/rails-param-validation/rails/annotation_manager.rb +0 -0
  23. data/lib/rails-param-validation/rails/config.rb +2 -1
  24. data/lib/rails-param-validation/rails/extensions/annotation_extension.rb +0 -0
  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 +22 -14
  28. data/lib/rails-param-validation/rails/helper.rb +0 -0
  29. data/lib/rails-param-validation/rails/openapi/openapi.rb +0 -0
  30. data/lib/rails-param-validation/rails/openapi/routing_helper.rb +0 -0
  31. data/lib/rails-param-validation/rails/rails.rb +11 -4
  32. data/lib/rails-param-validation/rails/tasks/openapi.rake +2 -2
  33. data/lib/rails-param-validation/types/types.rb +4 -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 +0 -0
  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: 9b55168eefe4dc2b76d0a342546f807ea6b22475a7a1798a03a53c04943726a6
4
- data.tar.gz: e3c250dbb5fc1f9766f75a32146e72a42a1a7250cb261fc528088ac0e6785735
3
+ metadata.gz: b4a98d65517bb634880f15f8646ee1645a60c00a962c7c604df80c9ead49404f
4
+ data.tar.gz: 7e755c2e7011fa1c848c1f4cf59ca93232a8829ba081c11b859707e652ed3a9f
5
5
  SHA512:
6
- metadata.gz: e9591ca7d203ddc520b95a9e1bcce791259d5f16ef31a43b816c6caecf53a1f68214c2b1376b38a822f59d54b29fd35bfccd30e0858361aec3c5e56700dd3a14
7
- data.tar.gz: d5d75b3730271590d2c5fb30ad0b7c888a13f350b38af3da66a305e3464f84821145c8147ab81e5072cb4e6394b7f63d02356e1c2a5fb838d3911a3c3d904d24
6
+ metadata.gz: ceb39335b93f488396532b5317a7cc48ddd633e795417bb30b4c628f790778864024ce83e67affb2a4e91713b9e8f2f8336bdfb3d4998714fbf3ca1df6112518
7
+ data.tar.gz: 811e141a19bcafadc2a0270620ad844ae104249b45dd9581b0d083694b48874914dba256a304842a4d60fbbc5316ab7013a35027c693166b04ce2c07caeb39e4
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
@@ -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](./docs/type-definition.md). The last (optional) parameter to this call is the description.
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](./docs/type-definition.md). The last (optional) parameter to this call is the description.
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](./docs/type-definition.md). The last (optional) parameter to this call is the description.
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](./docs/type-definition.md). The last (optional) parameter to this call is the description.
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
 
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 OpenApiMetaConfig
4
- attr_accessor :title, :version, :url, :description
4
+ attr_accessor :title, :version, :url, :description, :file_path
5
5
 
6
6
  def initialize
7
7
  app_class = Rails.application.class
@@ -10,6 +10,7 @@ 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
13
14
  end
14
15
 
15
16
  private
@@ -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
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
@@ -50,22 +50,30 @@ module RailsParamValidation
50
50
  end
51
51
 
52
52
  def _render_invalid_param_response(result)
53
- # Depending on the accept header, choose the way to answer
54
- respond_to do |format|
55
- format.html do
56
- if RailsParamValidation.config.use_default_html_response
57
- _create_html_error result
58
- else
59
- raise ParamValidationFailedError.new(result)
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
- end
62
- format.json do
63
- if RailsParamValidation.config.use_default_json_response
64
- _create_json_error result
65
- else
66
- raise ParamValidationFailedError.new(result)
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
 
@@ -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
- ActionController::Base.send :include, ActionControllerExtension
20
- ActionController::Base.send :include, AnnotationExtension
21
- ActionController::Base.send :include, CustomTypesExtension
22
- ActionController::Base.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
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 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
@@ -1,5 +1,9 @@
1
1
  module RailsParamValidation
2
2
 
3
+ def self.register(type, schema)
4
+ AnnotationTypes::CustomT.register type, schema
5
+ end
6
+
3
7
  module AnnotationTypes
4
8
  class AnnotationT
5
9
  attr_reader :inner_type
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
- @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.1.1"
2
+ VERSION = "0.2.3"
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.1.1
4
+ version: 0.2.3
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-03-08 00:00:00.000000000 Z
11
+ date: 2020-07-23 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