taro 2.2.0 → 2.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d73348290387aeef3de0558f6557af6572326043dff427ee295386a12e8bf6a6
4
- data.tar.gz: 89b0a6c8efae50665d43385b0b4182fba6b07e5f97e933d3add5d0e1ba19f421
3
+ metadata.gz: c96d6fd71ad22df31d5ab4cefb09389fd7ed5b41b5dd8821132fa2c5fa322837
4
+ data.tar.gz: f56362229e93f2f8c51a5490245d4a37e224015fecd42bb86a35926a01e8d776
5
5
  SHA512:
6
- metadata.gz: 67a0d0a5d2464c7ec6954143c95311b92b1a4a78041b298449815b1e57340de7091ac89235fd9e674ee1d3e99ed531049e99207ec62498fa94687b7c91956edd
7
- data.tar.gz: '0768d75380ee9b18de093bebf1952b4eeac1e7965fea8c3a0cb5fbec3ef84ec6a27d5e141a837bf6a0f9d698e5b2ceff9ba2e30a33143edd434ebd25812a933a'
6
+ metadata.gz: 34ad2aedee3c3a21973420ca5e1185024c7d6b127ee9ca914b847134939bcd136e70f10130607b8392ce8044dfcb7c8014fa306d00d81b81266d68fcdad19b95
7
+ data.tar.gz: 1bec2fbada3a23267f87195323b2b112297c21e78c22281071bd2ec84a7359f78836ed250e85ae3f3d1765f428dd7ff1bdaaf92f5df2bd17883abed11b7a39eb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [2.3.0] - 2025-02-24
4
+
5
+ ### Added
6
+
7
+ - `Taro.config.raise_for_undeclared_params`
8
+
3
9
  ## [2.2.0] - 2025-02-22
4
10
 
5
11
  ### Added
data/README.md CHANGED
@@ -140,6 +140,14 @@ Requests are automatically validated to match the declared input schema, unless
140
140
  Taro.config.parse_params = false
141
141
  ```
142
142
 
143
+ There is also an option to raise an error if any undeclared params are submitted:
144
+
145
+ ```ruby
146
+ Taro.config.raise_for_undeclared_params = true
147
+ ```
148
+
149
+ This option is similar to `action_on_unpermitted_parameters = :raise` in Rails and is most useful in dev and test environments. The railtie enables it automatically in these environments.
150
+
143
151
  #### Response validation
144
152
 
145
153
  Responses are automatically validated to have used the correct type for rendering, which guarantees that they match the declaration. This means you have to use the types to render complex responses, and manually building a Hash that conforms to the schema will raise an error. Primitive/scalar types are an exception, e.g. you *can* do:
@@ -367,7 +375,6 @@ end
367
375
 
368
376
  ## Possible future features
369
377
 
370
- - warning/raising for undeclared input params (currently they are ignored)
371
378
  - usage without rails is possible but not convenient yet
372
379
  - rspec matchers for testing
373
380
  - sum types
data/lib/taro/config.rb CHANGED
@@ -5,6 +5,7 @@ module Taro::Config
5
5
  :export_format,
6
6
  :export_path,
7
7
  :parse_params,
8
+ :raise_for_undeclared_params,
8
9
  :validate_response,
9
10
  )
10
11
 
@@ -14,6 +15,7 @@ module Taro::Config
14
15
  self.export_format = :yaml
15
16
  self.export_path = 'api.yml'
16
17
  self.parse_params = true
18
+ self.raise_for_undeclared_params = false # may be overridden by railtie
17
19
  self.validate_response = true
18
20
  end
19
21
 
@@ -5,7 +5,7 @@ class Taro::Declaration
5
5
  attr_reader :desc, :summary, :params, :return_defs, :return_descriptions, :tags
6
6
 
7
7
  def initialize(for_klass = nil)
8
- @params = Class.new(Taro::Types::InputType)
8
+ @params = Class.new(Taro::Types::RailsParamsType)
9
9
  @return_defs = {}
10
10
  @return_descriptions = {}
11
11
 
@@ -1,5 +1,7 @@
1
1
  class Taro::Rails::Railtie < ::Rails::Railtie
2
2
  initializer("taro") do |app|
3
+ Taro.config.raise_for_undeclared_params = %w[development test].include?(Rails.env)
4
+
3
5
  # The `:action_controller` hook fires for both ActionController::API
4
6
  # and ActionController::Base, executing the block in their context.
5
7
  ActiveSupport.on_load(:action_controller) do
@@ -0,0 +1,10 @@
1
+ require_relative 'object_type'
2
+
3
+ # Abstract base class for rails declaration params. Internal use only.
4
+ class Taro::Types::RailsParamsType < Taro::Types::InputType
5
+ # Skip validation of base params because they contain rails "additions"
6
+ # like controller, action, routing-related stuff, de-nested values, etc.
7
+ def validate_no_undeclared_params?
8
+ false
9
+ end
10
+ end
@@ -1,6 +1,7 @@
1
1
  # Provides input and response handling for types with fields.
2
2
  module Taro::Types::Shared::ObjectCoercion
3
3
  def coerce_input
4
+ validate_no_undeclared_params
4
5
  self.class.fields.transform_values do |field|
5
6
  field.value_for_input(object)
6
7
  end
@@ -13,4 +14,17 @@ module Taro::Types::Shared::ObjectCoercion
13
14
  field.value_for_response(object, context: self, object_is_hash:)
14
15
  end
15
16
  end
17
+
18
+ private
19
+
20
+ def validate_no_undeclared_params
21
+ return unless validate_no_undeclared_params?
22
+
23
+ undeclared = object.to_h.keys.map(&:to_sym) - self.class.send(:field_defs).keys
24
+ undeclared.any? && input_error("Undeclared params: #{undeclared.join(', ')}")
25
+ end
26
+
27
+ def validate_no_undeclared_params?
28
+ Taro.config.raise_for_undeclared_params
29
+ end
16
30
  end
@@ -1,10 +1,8 @@
1
1
  module Taro::Types::Shared::Rendering
2
2
  # The `::render` method is intended for use in controllers.
3
3
  # Overrides of this method must call super.
4
- def render(object, cache_attrs = {})
5
- result = Taro::Cache.call(object, **cache_attrs) do
6
- new(object).cached_coerce_response
7
- end
4
+ def render(object)
5
+ result = new(object).cached_coerce_response
8
6
  self.last_render = [type_class, result.__id__]
9
7
  result
10
8
  end
data/lib/taro/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # :nocov:
2
2
  module Taro
3
- VERSION = "2.2.0"
3
+ VERSION = "2.3.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taro
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janosch Müller
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-02-22 00:00:00.000000000 Z
12
+ date: 2025-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -89,6 +89,7 @@ files:
89
89
  - lib/taro/types/object_types/no_content_type.rb
90
90
  - lib/taro/types/object_types/page_info_type.rb
91
91
  - lib/taro/types/object_types/page_type.rb
92
+ - lib/taro/types/rails_params_type.rb
92
93
  - lib/taro/types/response_type.rb
93
94
  - lib/taro/types/scalar/boolean_type.rb
94
95
  - lib/taro/types/scalar/float_type.rb