{"id":18000126,"url":"https://github.com/kr00lix/cast_params","last_synced_at":"2025-03-26T07:32:03.833Z","repository":{"id":57482345,"uuid":"127125324","full_name":"Kr00lIX/cast_params","owner":"Kr00lIX","description":"CastParams creates plug for casting params to defined types.","archived":false,"fork":false,"pushed_at":"2023-11-15T11:09:37.000Z","size":72,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T11:09:47.641Z","etag":null,"topics":["elixir","phoenix-elixir","plug"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kr00lIX.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-28T10:41:13.000Z","updated_at":"2023-05-14T14:40:28.000Z","dependencies_parsed_at":"2024-10-29T23:30:04.517Z","dependency_job_id":null,"html_url":"https://github.com/Kr00lIX/cast_params","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.16000000000000003","last_synced_commit":"3e57d41afee04139cb959e2835f459912753f102"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kr00lIX%2Fcast_params","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kr00lIX%2Fcast_params/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kr00lIX%2Fcast_params/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kr00lIX%2Fcast_params/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kr00lIX","download_url":"https://codeload.github.com/Kr00lIX/cast_params/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245611754,"owners_count":20643892,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","phoenix-elixir","plug"],"created_at":"2024-10-29T23:09:44.925Z","updated_at":"2025-03-26T07:32:02.833Z","avatar_url":"https://github.com/Kr00lIX.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CastParams\n-----\n[![Build Status][shield-travis]][travis-ci]\n[![Coverage Status][shield-coveralls]][docs]\n[![Version][shield-version]][hexpm]\n[![License][shield-license]][hexpm]\n\nCastParams is a powerful library for Elixir's [Plug][plug] that allows you to define parameter types and automatically cast incoming parameters to their respective types in a plug for `Phoenix.Controller` or `Plug.Router`. This simplifies the process of handling incoming parameters and reduces the amount of manual type checking and casting you need to do in your code.\n\n\n## Features\n* **Type Casting**: Automatically cast incoming parameters to their defined types. This eliminates the need for manual type checking and casting in your code.\n* **Nullify Parameters**: If a parameter does not exist, it will be set to null. This prevents errors caused by undefined parameters.\n* **Required Types**: You can mark a type as required by ending it with a `!`. If a required parameter does not exist, an exception will be raised.\n* **Namespace Support**: CastParams supports namespaced parameters like `user[name]`.\n\n## Usage\n\n### Phoenix Framework\n\n1. Add `use CastParams` inside your Phoenix controller or in `web.ex`. This will extend the controller module with the `cast_params/1` macro which allows you to define parameter types.\n2. Use the `cast_params` macro to define the types for your parameters. You can define types for all actions or for specific actions.\n3. In your action functions, you can now access the casted parameters directly from the params map.\n\nHere is an example:\n```elixir\ndefmodule AccountController do\n\n  use AppWeb, :controller\n  use CastParams\n  \n  # Define parameter types for all actions\n  # :category_id is a required integer parameter (CastParams.NotFound will be raised if it does not exist)\n  # :weight is a float parameter (will be set to nil if it does not exist)\n  cast_params category_id: :integer!, weight: :float\n\n  # Define parameter types for the show action\n  # :name is a required string parameter\n  # :terms is a boolean parameter  \n  cast_params name: :string!, terms: :boolean when action == :show\n\n  # Define parameter types for the create action\n  # user[name] is a string parameter\n  # user[subscribe] is a boolean parameter\n  cast_params user: [name: :string, subscribe: :boolean] when action == :create\n\n  # The index action receives the prepared parameters\n  def index(conn, %{\"category_id\" =\u003e category_id, \"weight\" =\u003e weight} = params) do\n    # Here you can use the casted parameters directly\n    # For example, you might use the category_id to fetch a category from the database\n    category = Repo.get(Category, category_id)\n    # Then you might use the weight to filter products in that category\n    products = Repo.all(from p in Product, where: p.category_id == ^category_id and p.weight \u003c= ^weight)\n    # Then render the index view with the products\n    render(conn, \"index.html\", products: products)\n  end\n\n  # The show action also receives the prepared parameters\n  def show(conn, %{\"category_id\" =\u003e category_id, \"terms\" =\u003e terms, \"weight\" =\u003e weight}) do      \n    # Here you might use the category_id and terms to fetch a specific product\n    product = Repo.get_by(Product, category_id: category_id, terms: terms)\n    # Then render the show view with the product\n    render(conn, \"show.html\", product: product)\n  end\n\n  # The create action receives the prepared parameters\n  def create(conn, %{\"user\" =\u003e %{\"name\" =\u003e name, \"subscribe\" =\u003e subscribe}) do\n        # Here you might use the name and subscribe parameters to create a new user\n    user = User.changeset(%User{}, %{name: name, subscribe: subscribe})\n    case Repo.insert(user) do\n      {:ok, user} -\u003e redirect(conn, to: user_path(conn, :show, user))\n      {:error, changeset} -\u003e render(conn, \"new.html\", changeset: changeset)\n    end\n  end\nend\n```\n\n### Other Plug.Router Apps\nFor other applications using `Plug.Router`, call the `cast_params` anytime after calling the `:match` and `:dispatch` plugs:\n\n```elixir\ndefmodule MyApp.Router do\n  use Plug.Router\n  use CastParams\n  plug :match\n  plug :dispatch\n\n  cast_params(category_id: :integer, terms: :boolean)\n  cast_params(name: :string)\n\n  get(\"/\", do: send_resp(conn, 200, \"ok\"))\nend\n```\n\n## Supported Types\nEach type can ending with a `!` to mark the parameter as required.\n\n* *`:boolean`*\n* *`:integer`* \n* *`:string`* \n* *`:float`* \n* *`:decimal`*\n\n\n## Installation\n[Available in Hex](https://hex.pm/packages/cast_params), the package can be installed by adding `cast_params`to your list of dependencies in mix.exs:\n\n```elixir\ndef deps do\n  [\n    {:cast_params, \"~\u003e 0.0.5\"} \n  ]\nend\n```\n\n## Common Issues and Solutions\nIf you encounter any issues while using CastParams, please check this section first. If your issue is not listed here, feel free to open an issue on the GitHub repository.\n\n## Contribution\nContributions are always welcome! Feel free to send your PR with proposals, improvements, or corrections.\n\n\n## License\nThis software is licensed under [the MIT license](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkr00lix%2Fcast_params","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkr00lix%2Fcast_params","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkr00lix%2Fcast_params/lists"}