{"id":16381012,"url":"https://github.com/anthonator/ape-rawr","last_synced_at":"2026-06-18T06:32:21.824Z","repository":{"id":9046000,"uuid":"10810239","full_name":"anthonator/ape-rawr","owner":"anthonator","description":"API parameter validation in Rails","archived":false,"fork":false,"pushed_at":"2013-10-30T18:51:18.000Z","size":164,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-21T14:49:56.422Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/anthonator.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-20T02:02:14.000Z","updated_at":"2023-03-17T12:03:00.000Z","dependencies_parsed_at":"2022-07-09T22:30:29.729Z","dependency_job_id":null,"html_url":"https://github.com/anthonator/ape-rawr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anthonator/ape-rawr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Fape-rawr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Fape-rawr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Fape-rawr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Fape-rawr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anthonator","download_url":"https://codeload.github.com/anthonator/ape-rawr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Fape-rawr/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34479552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-18T02:00:06.871Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-10-11T03:53:08.979Z","updated_at":"2026-06-18T06:32:21.807Z","avatar_url":"https://github.com/anthonator.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ApeRawr\n\nEver wanted an easy way to validate your API parameters and spit out clean, developer friendly error messages? Ever notice how there's no good way to do that in Rails?\n\nEnter ApeRawr!\n\n## Contributions\n\nApeRawr is an amalgamation of two awesome gems: Grape and RocketPants. This gem combines the error handling capabilities of RocketPants with the parameter validation of Grape into a single cohesive tool used for making API parameter validation easier. The maintainers of RocketPants and Grape deserve most of the credit for this gem. I just provided the glue.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'ape-rawr'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install ape-rawr\n\n## Usage\n\n### Parameter Validation and Coercion\nYou can define validations and coercion options for you parameters using a params block.\n\n```ruby\nparams do\n  requires :id, type: Integer\n  optional :text, type: String, regexp: /^[a-z]+$/\n  group :media do\n    requires :url\n  end\nend\n\ndef :create\n  ...\nend\n```\n\nWhen a type is specified an implicit validation is done after the coercion to ensure the output type is the one declared.\n\nOptional parameters can have a default value.\n\n```ruby\nparams do\n  options :color, type: String, default: 'blue'\nend\n```\n\nParameters can be nested using `group`. In the above example, this means `params[:media][:url]` is required along with `params[:id]`.\n\nValidations can also be defined for a specific action.\n\n```ruby\nparams :create do\n  ...\nend\n\ndef create\n  ...\nend\n```\n\n### Custom Validators\n\n```ruby\nclass AlphaNumeric \u003c Grape::Validations::Validator\n  def validate_param!(attr_name, params)\n    unless params[attr_name] =~ /^[[:alnum:]]+$/\n      error!(:alpha_numeric_error, :attribute =\u003e attr_name)\n    end\n  end\nend\n```\n\n```ruby\nparams do\n  requires :text, alpha_numeric: true\nend\n```\n\nYou can also create custom classes that take parameters.\n\n```ruby\nclass Length \u003c Grape::Validations::SingleOptionValidator\n  def validate_param!(attr_name, params)\n    unless params[attr_name].length \u003c= @option\n      error!(:length_error, :attribute =\u003e attr_name, :expected_length =\u003e @option)\n    end\n  end\nend\n```\n\n```ruby\nparams do\n  requires :text, length: 140\nend\n```\n\n### Handling Errors\n\nOne of the built in features of ApeRawr is the ability to handle rescuing / controlling exceptions and more importantly to handle mapping exceptions to names, messages and error codes.\n\nThis comes in useful when you wish to automatically convert exceptions such as `ActiveRecord::RecordNotFound` (Note: This case is handled already) to a structured bit of data in the response. Namely, it makes it trivial to generate objects that follow the JSON structure of:\n\n```json\n{\n  \"error\":             \"standard_error_name\",\n  \"error_description\": \"A translated error message describing what happened.\"\n}\n```\n\nIt also adds a facilities to make it easy to add extra information to the response.\n\nApeRawr will also attempt to convert all errors in the controller, defaulting to the `\"system\"` exception name and message as the error description. We also provide a registry to allow throwing exception from their symbolic name like so:\n\n```ruby\nerror! :not_found\n```\n\nIn the controller.\n\nOut of the box, the following exceptions come pre-registered and setup. For each of them, you can either use the error form (`error! :error_key) or you can raise an instance of the exception class like normal.\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eError Key\u003c/th\u003e\n    \u003cth\u003eException Class\u003c/th\u003e\n    \u003cth\u003eHTTP Status\u003c/th\u003e\n    \u003cth\u003eDescription\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:throttled\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::Throttled\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e503 Unavailable\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe user has hit an api throttled error.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:unauthenticated\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::Unauthenticated\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e401 Unauthorized\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe user doesn't have valid authentication details.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:invalid_version\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::Invalidversion\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e404 Not Found\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eAn invalid API version was specified.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:not_implemented\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::NotImplemented\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e503 Unavailable\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe specified endpoint is not yet implemented.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:not_found\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::NotFound\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e404 Not Found\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe given resource could not be found.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:invalid_resource\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::InvalidResource\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e422 Unprocessable Entity\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe given resource was invalid.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:bad_request\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::BadRequest\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e400 Bad Request\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe given request was not as expected.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:conflict\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::Conflict\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e409 Conflict\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe resource was a conflict with the existing version.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:forbidden\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::Forbidden\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e403 Forbidden\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eThe requested action was forbidden.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:presence\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::BadRequest\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e400 Bad Request\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003emissing parameter: %{attribute}\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:coerce\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::BadRequest\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e400 Bad Request\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003einvalid parameter: %{attribute}\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003e:regexp\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003eApeRawr::BadRequest\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ccode\u003e400 Bad Request\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003einvalid parameter: %{attribute}\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nNote that error also excepts a Hash of contextual options, many which will be passed through to the Rails I18N subsystem. E.g:\n\n```ruby\nerror! :throttled, :max_per_hour =\u003e 100\n```\n\nWill look up the translation `ape_rawr.errors.throttled` in your I18N files, and call them with `:max_per_hour` as an argument.\n\nFinally, You can use this to also pass custom values to include in the response, e.g:\n\n```ruby\nerror! :throttled, :metadata =\u003e {:code =\u003e 123}\n```\n\nWill return something similar to:\n\n```json\n{\n  \"error\":             \"throttled\",\n  \"error_description\": \"The example error message goes here\",\n  \"code\":              123\n}\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonator%2Fape-rawr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanthonator%2Fape-rawr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonator%2Fape-rawr/lists"}