dry-rb / dry-rails

The official dry-rb railtie
https://dry-rb.org/gems/dry-rails
MIT License
270 stars 27 forks source link

cannot define multiple schemas in one controller (safe params) #23

Closed gotar closed 4 years ago

gotar commented 4 years ago

Describe the bug

If schema is defined more than once, it overwrite safe_params. So safe_params are only available and works for last defined schema.

To Reproduce

class UsersController < ApplicationController
  schema(:show) do
    required(:id).value(:integer)
  end

  schema(:new) do
    required(:id).value(:integer)
  end

  def show
    if safe_params.success?
      render json: {id: safe_params[:id], name: "Jane"}
    else
      render json: {errors: safe_params.errors.to_h}
    end
  end

  def new
    if safe_params.success?
      render json: {id: safe_params[:id], name: "Jane"}
    else
      render json: {errors: safe_params.errors.to_h}
    end
  end
end

Expected behavior

safe params works for both defined schema, currently second works, but for :show safe_params are just nil

gotar commented 4 years ago

related PR fix the ISSUE: https://github.com/dry-rb/dry-rails/pull/24