{"id":18327723,"url":"https://github.com/tiev/sinatra-dry_param","last_synced_at":"2025-04-06T01:32:10.503Z","repository":{"id":52415094,"uuid":"183196768","full_name":"tiev/sinatra-dry_param","owner":"tiev","description":"Validate sinatra params with the strong dry-schema","archived":false,"fork":false,"pushed_at":"2023-08-22T15:19:01.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-24T04:38:36.219Z","etag":null,"topics":["params","sinatra","validation"],"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/tiev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2019-04-24T09:36:11.000Z","updated_at":"2024-03-20T13:53:14.000Z","dependencies_parsed_at":"2024-11-05T19:12:14.818Z","dependency_job_id":"d235a947-0874-4107-886a-ce7457ecfc4a","html_url":"https://github.com/tiev/sinatra-dry_param","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/tiev%2Fsinatra-dry_param","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiev%2Fsinatra-dry_param/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiev%2Fsinatra-dry_param/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiev%2Fsinatra-dry_param/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiev","download_url":"https://codeload.github.com/tiev/sinatra-dry_param/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423475,"owners_count":20936621,"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":["params","sinatra","validation"],"created_at":"2024-11-05T19:12:05.896Z","updated_at":"2025-04-06T01:32:08.552Z","avatar_url":"https://github.com/tiev.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sinatra::DryParam\n\nThis sinatra extension provides easy interfaces for validating params. It serves both schema validation and strong-params feature.\n\nValidation is processed by [dry-schema](https://github.com/dry-rb/dry-schema), provides robust type and schema validation.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'sinatra-dry_param'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install sinatra-dry_param\n\n## Usage\n\nRegister and use the extension in your sinatra app, for classic style sinatra app:\n\n\n```ruby\nrequire 'sinatra'\nrequire 'sinatra/dry_param'\n\nparams do\n  required(:name).filled(:string)\nend\n\nget '/hello' do\n  data = validate_params\n  h \"Hello #{data[:name]}!\"\nend\n```\n\nOr for modular style sinatra app:\n\n```ruby\nrequire 'sinatra/base'\nrequire 'sinatra/dry_param'\n\nclass App \u003c Sinatra::Base\n  register Sinatra::DryParam\n\n  params do\n    required(:name).filled(:string)\n  end\n\n  get '/hello' do\n    data = validate_params\n    h \"Hello #{data[:name]}!\"\n  end\nend\n```\n\n### Define params schemas\n\nOne or more schemas can be defined using method `::params`, distinguised by names:\n\n```ruby\nclass App \u003c Sinatra::Base\n  register Sinatra::DryParam\n\n  params do\n    # dry-schema definitions\n  end\n\n  params :paging do\n    # different name as :paging\n  end\n\n  params do\n    # Override the previous schema in whole class scope.\n    # Should only override parent class' schemas.\n  end\n\n  get '/' do\n    validate_params                          # Use the default schema to validate params\n    validate_params :dry, params             # Same with above line, the default schema name is :dry\n    validate_params :paging, params[:paging] # Use the :paging schema to validate one param\n  end\n```\n\n### Schema definition\n\nThe block passed in `::params` is the block passed to `Dry::Schema.Params` of dry-schema.\n\nFor syntax of schema definition, please refer to dry-schema [documentation](https://dry-rb.org/gems/dry-schema/).\n\n### Reusing param schemas\n\nDefined params can be retrieved as app settings, using the schema name with `\"_params\"` suffix.\n\n```ruby\n  settings.dry_params\n  settings.paging_params\n```\n\nAnd we can reuse schemas as how dry-schema is [reused](https://dry-rb.org/gems/dry-schema/reusing-schemas/)\n\n```ruby\n  params :address do\n    required(:city).filled(:string)\n    required(:street).filled(:string)\n    required(:zipcode).filled(:string)\n  end\n\n  params do\n    required(:email).filled(:string)\n    required(:name).filled(:string)\n    required(:address).hash(settings.address_params)\n  end\n\n  post '/users' do\n    input = validate_params\n    User.create_with_address(input)\n  end\n```\n\n### Render response\n\nBy default, if `validate_params` failed, the action is halted with status 400 and render errors.\n\n```ruby\nparams do\n  required(:page).filled(:integer, gt?: 0)\n  required(:per_page).filled(:integer, gt?: 0)\nend\n\nget '/' do\n  validate_params\nend\n\n#client\nget '/', page: 0\n#\u003e Response body: { \"page\": ['must be greater than 0'], \"per_page\": ['is missing'] }\n```\n\nYou can customize the behavior with this enable setting `raise_dry_param_exceptions` and catch the exception:\n\n```ruby\nclass App \u003c Sinatra::Base\n  register Sinatra::DryParam\n\n  enable :raise_dry_param_exceptions\n\n  error Sinatra::DryParam::InvalidParamsError do\n    \"Your params is not valid: \" + env['sinatra.error'].results.to_h.to_s\n  end\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/tiev/sinatra-dry_param. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Sinatra::DryParam project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tiev/sinatra-dry_param/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiev%2Fsinatra-dry_param","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiev%2Fsinatra-dry_param","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiev%2Fsinatra-dry_param/lists"}