{"record":{"id":"3aaf6c118880958a","repo":"ruby-grape/grape","slug":"declared-is-not-available-prior-to-parameter-vali","errorCode":null,"errorMessage":"#declared is not available prior to parameter validation","messagePattern":"#declared is not available prior to parameter validation","errorType":"exception","errorClass":"Grape::DSL::Declared::MethodNotYetAvailable","httpStatus":null,"severity":"error","filePath":"lib/grape/dsl/declared.rb","lineNumber":23,"sourceCode":"    module Declared\n      # Denotes a situation where a DSL method has been invoked in a\n      # filter which it should not yet be available in\n      class MethodNotYetAvailable < StandardError\n        def initialize(msg = '#declared is not available prior to parameter validation')\n          super\n        end\n      end\n\n      # A filtering method that will return a hash\n      # consisting only of keys that have been declared by a\n      # `params` statement against the current/target endpoint or parent\n      # namespaces.\n      # @param params [Hash] The initial hash to filter. Usually this will just be `params`\n      # @param options [Hash] Can pass `:include_missing`, `:stringify` and `:include_parent_namespaces`\n      # options. `:include_parent_namespaces` defaults to true, hence must be set to false if\n      # you want only to return params declared against the current/target endpoint.\n      def declared(passed_params, include_parent_namespaces: true, include_missing: true, evaluate_given: false, stringify: false)\n        raise MethodNotYetAvailable unless before_filter_passed\n\n        contract_key_map = inheritable_setting.contract_key_maps\n        handler = DeclaredParamsHandler.new(include_missing:, evaluate_given:, stringify:, contract_key_map:)\n        declared_params = include_parent_namespaces ? inheritable_setting.route_declared_params : (inheritable_setting.declared_params.last || [])\n        renamed_params = inheritable_setting.route_renamed_params\n        route_params = config.params\n\n        handler.call(passed_params, declared_params, route_params, renamed_params)\n      end\n    end\n  end\nend\n","sourceCodeStart":5,"sourceCodeEnd":36,"githubUrl":"https://github.com/ruby-grape/grape/blob/22d7975629846a3c0c7bd2b34e140a7a1b4af8f6/lib/grape/dsl/declared.rb#L5-L36","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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`."],"exampleFix":"# before\nbefore do\n  @scoped = declared(params, include_missing: false) # raises MethodNotYetAvailable\nend\nget '/things' do\n  @scoped\nend\n\n# after\nget '/things' do\n  declared(params, include_missing: false)\nend","handlingStrategy":"validation","validationCode":"# In before blocks, use raw params; declared is only legal in the action\nbefore do\n  @raw = params # NOT declared(params)\nend","typeGuard":null,"tryCatchPattern":"begin\n  declared(params, include_missing: false)\nrescue Grape::Exceptions::MethodNotYetAvailable\n  params # fallback: raw params when called too early (e.g. shared helper)\nend","preventionTips":["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."],"tags":["grape","declared","params","lifecycle","before-filter"],"backgroundTag":"helper-before-validation","analyzedSha":"22d7975629846a3c0c7bd2b34e140a7a1b4af8f6","analyzedAt":"2026-08-21T17:03:54.627Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}