ruby-grape/grape · error · Grape::DSL::Declared::MethodNotYetAvailable
#declared is not available prior to parameter validation
Error message
#declared is not available prior to parameter validation
What it means
The `declared` helper returns only the parameters declared via `params` blocks, but that information is only trustworthy after Grape has run the endpoint's filters and parameter validation. `declared` checks `before_filter_passed` and raises Grape::Exceptions::MethodNotYetAvailable ('#declared is not available prior to parameter validation') when it is called earlier in the request lifecycle, typically from inside a `before` block.
Source
Thrown at lib/grape/dsl/declared.rb:23
module Declared
# Denotes a situation where a DSL method has been invoked in a
# filter which it should not yet be available in
class MethodNotYetAvailable < StandardError
def initialize(msg = '#declared is not available prior to parameter validation')
super
end
end
# A filtering method that will return a hash
# consisting only of keys that have been declared by a
# `params` statement against the current/target endpoint or parent
# namespaces.
# @param params [Hash] The initial hash to filter. Usually this will just be `params`
# @param options [Hash] Can pass `:include_missing`, `:stringify` and `:include_parent_namespaces`
# options. `:include_parent_namespaces` defaults to true, hence must be set to false if
# you want only to return params declared against the current/target endpoint.
def declared(passed_params, include_parent_namespaces: true, include_missing: true, evaluate_given: false, stringify: false)
raise MethodNotYetAvailable unless before_filter_passed
contract_key_map = inheritable_setting.contract_key_maps
handler = DeclaredParamsHandler.new(include_missing:, evaluate_given:, stringify:, contract_key_map:)
declared_params = include_parent_namespaces ? inheritable_setting.route_declared_params : (inheritable_setting.declared_params.last || [])
renamed_params = inheritable_setting.route_renamed_params
route_params = config.params
handler.call(passed_params, declared_params, route_params, renamed_params)
end
end
end
end
View on GitHub (pinned to 22d7975629)
Solutions
- Move the `declared(params)` call into the route action (the get/post/put block), where validation has already run.
- If you only need the raw validated params in the before block, use `params` directly instead of `declared(params)`.
- Extract shared logic into a helper method that takes the already-filtered hash and call it from the action after `declared`.
Example fix
# before before do @scoped = declared(params, include_missing: false) # raises MethodNotYetAvailable end get '/things' do @scoped end # after get '/things' do declared(params, include_missing: false) end
Defensive patterns
Strategy: validation
Validate before calling
# In before blocks, use raw params; declared is only legal in the action before do @raw = params # NOT declared(params) end
Try / catch
begin declared(params, include_missing: false) rescue Grape::Exceptions::MethodNotYetAvailable params # fallback: raw params when called too early (e.g. shared helper) end
Prevention
- Call `declared` only inside route actions, never in `before` or `rescue_from`.
- Put shared filtering logic in helpers that accept the filtered hash as an argument.
- Add a spec that exercises each `before` block to catch premature helper use.
When it happens
Trigger: Calling `declared(params)` inside `before do ... end` in an endpoint or namespace. Calling `declared` from a `rescue_from` handler or a helper invoked during the filter phase, before the route action runs.
Common situations: Refactoring an endpoint and moving parameter filtering into a before block for reuse. Trying to log or whitelist params in a global before filter. Upgrading from an old Grape version where the ordering was not enforced.
Related errors
- type #{type} should support coercion via `[]`
- group type is required
- group type must be Array, Hash, JSON or Array[JSON]
- Either contract or block must be provided
- unknown validator: %{validator_type}
AI-assisted analysis of ruby-grape/grape@22d7975629 (2026-08-21).
Data as JSON: /api/errors/3aaf6c118880958a.
Report an issue: GitHub.