https://github.com/monterail/gate
Handling user input with ease
https://github.com/monterail/gate
Last synced: 6 months ago
JSON representation
Handling user input with ease
- Host: GitHub
- URL: https://github.com/monterail/gate
- Owner: monterail
- License: mit
- Created: 2015-06-23T12:31:50.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T13:15:20.000Z (about 7 years ago)
- Last Synced: 2024-10-31T14:41:30.610Z (over 1 year ago)
- Language: Ruby
- Homepage: https://github.com/monterail/gate
- Size: 87.9 KB
- Stars: 27
- Watchers: 66
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Gate
[](http://badge.fury.io/rb/gate)
[](https://circleci.com/gh/monterail/gate)
[](https://codeclimate.com/github/monterail/gate)
[](https://codeclimate.com/github/monterail/gate/coverage)
**Gate** is a small wrapper on [dry-validation](http://dry-rb.org/gems/dry-validation/) that integrates it with [Ruby on Rails](https://rubyonrails.org/) and replaces [Strong Params](http://api.rubyonrails.org/classes/ActionController/Parameters.html).
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'gate'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install gate
## Usage
Define [contract](https://dry-rb.org/gems/dry-validation) per action with controller DSL...
```ruby
class ExampleController < ActionController::Base
include Gate::Rails
before_action :verify_contract, if: { |c| c.contract_registered? }
# Define contract just before action method
contract(handler: :handle_invalid_params) do
params do
required(:id).filled
required(:message).hash do
required(:title).filled
optional(:value).maybe(:decimal?)
end
end
end
def foo
# you can access Dry::Validation result with:
claimed_params
end
# handler for invalid params
def handle_invalid_params(_errors)
# errors is Dry::Validation messages hash
head :bad_request
end
end
```
... or as a separate class:
```ruby
class ExampleFooContract < Dry::Validation::Contract
params do
required(:id).filled
required(:message).hash do
required(:title).filled
optional(:value).maybe(:decimal?)
end
end
end
class ExampleController < ActionController::Base
include Gate::Rails
before_action :verify_contract, if: { |c| c.contract_registered? }
contract(ExampleFooContract)
def foo
# you can access Dry::Validation result with:
claimed_params
end
# just use default handler
def handle_invalid_params(_errors)
# errors is Dry::Validation messages hash
head :bad_request
end
end
```
## Configuration
*TODO*
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
To 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
1. Fork it ( https://github.com/[my-github-username]/gate/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request