https://github.com/kenchan0130/weak_headers
Validates request.headers in your rails controller
https://github.com/kenchan0130/weak_headers
actioncontroller rails rails5 ruby
Last synced: 5 months ago
JSON representation
Validates request.headers in your rails controller
- Host: GitHub
- URL: https://github.com/kenchan0130/weak_headers
- Owner: kenchan0130
- License: mit
- Created: 2015-09-20T18:46:07.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-30T14:06:43.000Z (over 9 years ago)
- Last Synced: 2025-01-14T11:56:28.315Z (about 1 year ago)
- Topics: actioncontroller, rails, rails5, ruby
- Language: Ruby
- Homepage:
- Size: 20.5 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README

# WeakHeaders
Validates `request.headers` in your rails controller.
## Installation
```ruby
gem 'weak_headers'
```
## Usage
```ruby
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from WeakHeaders::ValidationError do |e|
render json: { message: e.message }, status: 400
end
end
# WeakHeaders provides `header_validates` class method to define validations.
class AuthController < ApplicationController
header_validates :create do
requires 'X-App-Client-Id', except: ["token", "123456"]
optional :'X-App-Id', only: '1'
requires 'X-App-Client-Secret' do |value|
value =~ /\A\w{64}\z/
end
end
def create
auth = Application.authenticate(uid: request.headers['X-App-Client-Id'], secret: request.headers['X-App-Client-Secret'])
render json: { token: auth.token }
end
end
```
### Available validators
- requires
- optional
### Available options
- only
- except
- handler
## Inspired By
- [weak_parameters](https://github.com/r7kamura/weak_parameters)