{"id":16381001,"url":"https://github.com/anthonator/rosebud","last_synced_at":"2025-03-23T03:33:15.123Z","repository":{"id":12023678,"uuid":"14607386","full_name":"anthonator/rosebud","owner":"anthonator","description":"Rails API parameter validation","archived":false,"fork":false,"pushed_at":"2019-01-18T15:46:04.000Z","size":40,"stargazers_count":6,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-25T16:02:33.889Z","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-11-22T03:09:43.000Z","updated_at":"2023-03-17T12:03:12.000Z","dependencies_parsed_at":"2022-08-27T14:45:54.588Z","dependency_job_id":null,"html_url":"https://github.com/anthonator/rosebud","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Frosebud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Frosebud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Frosebud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonator%2Frosebud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anthonator","download_url":"https://codeload.github.com/anthonator/rosebud/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245052659,"owners_count":20553162,"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":[],"created_at":"2024-10-11T03:53:06.587Z","updated_at":"2025-03-23T03:33:14.623Z","avatar_url":"https://github.com/anthonator.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rosebud\n\nRails API parameter validation\n\n[![Build Status](https://travis-ci.org/anthonator/rosebud.png?branch=master)](https://travis-ci.org/anthonator/rosebud) [![Coverage Status](https://coveralls.io/repos/anthonator/rosebud/badge.png?branch=master)](https://coveralls.io/r/anthonator/rosebud?branch=master) [![Code Climate](https://codeclimate.com/github/anthonator/rosebud.png)](https://codeclimate.com/github/anthonator/rosebud)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'rosebud'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rosebud\n\n## Usage\n\n### Getting Started\n\nJust include ```Rosebud``` in your Rails controller.\n\n```ruby\nclass Api::V1::SomeController \u003c Api::V1::ApplicationController\n  include Rosebud\nend\n```\n\n### Validating Parameters\n\nTo validate parameters specify a ```params``` block and choose whether the params are required or optional.\n\n```ruby\nclass Api::V1::SomeController \u003c Api::V1::ApplicationController\n  include Rosebud\n\n  params do\n    requires :name\n    optional :phone_number, regex: /\\d{10}/, default: '5555555555'\n  end\n\n  ...\n\nend\n```\n\nIt's also possible to define validations for a specific action.\n\n```ruby\nclass Api::V1::SomeController \u003c Api::V1::ApplicationController\n  include Rosebud\n\n  params :create do\n    ...\n  end\n\n  def create\n    ...\n  end\nend\n```\n\n### Rendering Errors\n\nWhen a parameter error is raised a JSON response will be rendered.\n\n```ruby\n{\n  \"error\":       \"missing_parameter\",\n  \"description\": \"missing parameter: name\"\n}\n```\n\n### Custom Validators\n\nCurrently the only validators that ship with Rosebud are ```presence```, ```regex``` and ```type```. If you need other validators you can create your own.\n\n```ruby\nclass MyLengthValidator \u003c Rosebud::Validator\n  def validate_param(name, value, options)\n    if value.length \u003c= options\n      error!(:length, param: name, length: value.length, max_length: options)\n    end\n  end\nend\n\nRosebud::Validations.register_validator!(:length, MyLengthValidator)\n```\n\nRosebud uses the [Errawr::Rails](http://www.github.com/anthonator/errawr-rails) gem to raise and render localised errors. Add your custom error message to your locale file.\n\n```yaml\nen:\n  errawr:\n    length:\n      message: %{length} characters is too long for %{param}, should be less than or equal to %{max_length}\n      http_status: 400\n      metadata:\n        additional_info: Kick him in the shins!\n```\n\nAdd a validation using your custom validator:\n\n```ruby\nclass Api::V1::SomeController \u003c Api::V1::ApplicationController\n  include Rosebud\n\n  params :create do\n    requires :name, length: 2\n  end\n\n  def create\n    ...\n  end\nend\n```\n\nThe following error will be rendered for the ```name``` parameter when provided with a value of ```abc```:\n\n```json\n{\n  \"error\":       \"length\",\n  \"description\": \"3 characters is too long for name, should be less than or equal to 2\",\n  \"additional_info\": \"Kick him in the shins!\"\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\n## Credits\n[![Sticksnleaves](http://sticksnleaves-wordpress.herokuapp.com/wp-content/themes/sticksnleaves/images/snl-logo-116x116.png)](http://www.sticksnleaves.com)\n\nRosebud is maintained and funded by [Sticksnleaves](http://www.sticksnleaves.com)\n\nThanks to all of our [contributors](https://github.com/anthonator/rosebud/graphs/contributors)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonator%2Frosebud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanthonator%2Frosebud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonator%2Frosebud/lists"}