rosebud 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +33 -0
- data/LICENSE.txt +22 -0
- data/README.md +145 -0
- data/Rakefile +10 -0
- data/lib/rosebud.rb +33 -0
- data/lib/rosebud/locale/en.yml +14 -0
- data/lib/rosebud/params_scope.rb +35 -0
- data/lib/rosebud/validations.rb +17 -0
- data/lib/rosebud/validations/presence_validator.rb +11 -0
- data/lib/rosebud/validations/regex_validator.rb +11 -0
- data/lib/rosebud/validations/type_validator.rb +29 -0
- data/lib/rosebud/validator.rb +9 -0
- data/lib/rosebud/version.rb +3 -0
- data/rosebud.gemspec +28 -0
- data/spec/dummy/.gitignore +16 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +4 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/lib/tasks/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
- data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
- data/spec/optional_spec.rb +37 -0
- data/spec/presence_validator_spec.rb +19 -0
- data/spec/regex_validator_spec.rb +11 -0
- data/spec/requires_spec.rb +32 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/dummy_validator.rb +5 -0
- data/spec/support/invalid_subclass_validator.rb +2 -0
- data/spec/support/not_implemented_validator.rb +2 -0
- data/spec/type_validator_spec.rb +15 -0
- data/spec/validations_spec.rb +18 -0
- metadata +253 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: da9a1f5d401c5384749f7052be48eb0e95838e25
|
4
|
+
data.tar.gz: c79edafc75c00b50454731397d8ccdaa0a4605ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 079c027c7bec034a23c64d4fc3c10ddbae749da9bfd16ebd792a369047984e0668086b11e1685b22977e9eb54fe8f23cd7bf3c91552a6189a79428e371c16045
|
7
|
+
data.tar.gz: 7666244fa192a67323daaf7d8fe02d56c2a4f16effde25be2c6785ef3785bc9caa2a4ab23beddfece452e9ae0f1d48bdd5e12023c32b355b65623e68ef13b8dd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in rosebud.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
rails_version = case ENV['RAILS_VERSION'] || 'default'
|
7
|
+
when 'master'
|
8
|
+
{ git: 'https://github.com/rails/rails.git' }
|
9
|
+
when 'default'
|
10
|
+
'~> 4.0'
|
11
|
+
else
|
12
|
+
"~> #{ENV['RAILS_VERSION']}"
|
13
|
+
end
|
14
|
+
|
15
|
+
gem 'rails', rails_version
|
16
|
+
|
17
|
+
platforms :ruby do
|
18
|
+
gem 'sqlite3'
|
19
|
+
end
|
20
|
+
|
21
|
+
platforms :jruby do
|
22
|
+
gem 'activerecord-jdbcsqlite3-adapter'
|
23
|
+
end
|
24
|
+
|
25
|
+
platforms :rbx do
|
26
|
+
gem 'racc'
|
27
|
+
gem 'rubinius-coverage', github: 'rubinius/rubinius-coverage'
|
28
|
+
gem 'rubysl'
|
29
|
+
end
|
30
|
+
|
31
|
+
group :development do
|
32
|
+
gem 'coveralls', require: false
|
33
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Anthony Smith
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# Rosebud
|
2
|
+
|
3
|
+
Rails API parameter validation
|
4
|
+
|
5
|
+
[](https://travis-ci.org/anthonator/rosebud) [](https://coveralls.io/r/anthonator/rosebud?branch=master) [](https://codeclimate.com/github/anthonator/rosebud)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rosebud'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rosebud
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Getting Started
|
24
|
+
|
25
|
+
Just include ```Rosebud``` in your Rails controller.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Api::V1::SomeController < Api::V1::ApplicationController
|
29
|
+
include Rosebud
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
### Validating Parameters
|
34
|
+
|
35
|
+
To validate parameters specify a ```params``` block and choose whether the params are required or optional.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class Api::V1::SomeController < Api::V1::ApplicationController
|
39
|
+
include Rosebud
|
40
|
+
|
41
|
+
params do
|
42
|
+
requires :name
|
43
|
+
optional :phone_number, regex: /\d{10}/, default: '5555555555'
|
44
|
+
end
|
45
|
+
|
46
|
+
...
|
47
|
+
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
It's also possible to define validations for a specific action.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class Api::V1::SomeController < Api::V1::ApplicationController
|
55
|
+
include Rosebud
|
56
|
+
|
57
|
+
params :create do
|
58
|
+
...
|
59
|
+
end
|
60
|
+
|
61
|
+
def create
|
62
|
+
...
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
### Rendering Errors
|
68
|
+
|
69
|
+
When a parameter error is raised a JSON response will be rendered.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
{
|
73
|
+
"error": "missing_parameter",
|
74
|
+
"description": "missing parameter: name"
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
78
|
+
### Custom Validators
|
79
|
+
|
80
|
+
Currently the only validators that ship with Rosebud are ```presence```, ```regex``` and ```type```. If you need other validators you can create your own.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class MyLengthValidator < Rosebud::Validator
|
84
|
+
def validate_param(name, value, options)
|
85
|
+
if value.length <= options
|
86
|
+
error!(:length, param: name, length: value.length, max_length: options)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
Rosebud::Validations.register_validator!(:length, MyLengthValidator)
|
92
|
+
```
|
93
|
+
|
94
|
+
Rosebud 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.
|
95
|
+
|
96
|
+
```yaml
|
97
|
+
en:
|
98
|
+
errawr:
|
99
|
+
length:
|
100
|
+
message: %{length} characters is too long for %{param}, should be less than or equal to %{max_length}
|
101
|
+
http_status: 400
|
102
|
+
metadata:
|
103
|
+
additional_info: Kick him in the shins!
|
104
|
+
```
|
105
|
+
|
106
|
+
Add a validation using your custom validator:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
class Api::V1::SomeController < Api::V1::ApplicationController
|
110
|
+
include Rosebud
|
111
|
+
|
112
|
+
params :create do
|
113
|
+
requires :name, length: 2
|
114
|
+
end
|
115
|
+
|
116
|
+
def create
|
117
|
+
...
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
The following error will be rendered for the ```name``` parameter when provided with a value of ```abc```:
|
123
|
+
|
124
|
+
```json
|
125
|
+
{
|
126
|
+
"error": "length",
|
127
|
+
"description": "3 characters is too long for name, should be less than or equal to 2",
|
128
|
+
"additional_info": "Kcik him in the shins!"
|
129
|
+
}
|
130
|
+
```
|
131
|
+
|
132
|
+
## Contributing
|
133
|
+
|
134
|
+
1. Fork it
|
135
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
136
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
137
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
138
|
+
5. Create new Pull Request
|
139
|
+
|
140
|
+
## Credits
|
141
|
+
[](http://www.sticksnleaves.com)
|
142
|
+
|
143
|
+
Rosebud is maintained and funded by [Sticksnleaves](http://www.sticksnleaves.com)
|
144
|
+
|
145
|
+
Thanks to all of our [contributors](https://github.com/anthonator/rosebud/graphs/contributors)
|
data/Rakefile
ADDED
data/lib/rosebud.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'errawr'
|
2
|
+
require 'errawr/rails'
|
3
|
+
require 'virtus'
|
4
|
+
|
5
|
+
require 'rosebud/params_scope'
|
6
|
+
require 'rosebud/validations'
|
7
|
+
require 'rosebud/validator'
|
8
|
+
require 'rosebud/version'
|
9
|
+
|
10
|
+
I18n.load_path << "#{File.dirname(__FILE__)}/rosebud/locale/en.yml"
|
11
|
+
I18n.reload!
|
12
|
+
|
13
|
+
Dir[File.expand_path('../rosebud/validations/*.rb', __FILE__)].each do |path|
|
14
|
+
require(path)
|
15
|
+
end
|
16
|
+
|
17
|
+
module Rosebud
|
18
|
+
def self.included(base)
|
19
|
+
base.extend(ClassMethods)
|
20
|
+
base.send(:include, Errawr::Rails::Renderable.render_with(Errawr::Rails::Renderers::JSON))
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def params(action = :all, &block)
|
25
|
+
action = action.to_s
|
26
|
+
before_filter do
|
27
|
+
if action == 'all' || params[:action] == action
|
28
|
+
ParamsScope.new(self, params, &block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
en:
|
2
|
+
errawr:
|
3
|
+
presence:
|
4
|
+
message: "missing parameter: %{param}"
|
5
|
+
name: "missing_parameter"
|
6
|
+
http_status: 400
|
7
|
+
type:
|
8
|
+
message: "invalid parameter: %{param}"
|
9
|
+
name: "invalid_parameter"
|
10
|
+
http_status: 400
|
11
|
+
regex:
|
12
|
+
message: "invalid parameter: %{param}"
|
13
|
+
name: "invalid_parameter"
|
14
|
+
http_status: 400
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rosebud
|
2
|
+
class ParamsScope
|
3
|
+
def initialize(controller, params, &block)
|
4
|
+
@controller = controller
|
5
|
+
@params = params
|
6
|
+
@declared_params = []
|
7
|
+
|
8
|
+
instance_eval(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def requires(name, validations = {})
|
12
|
+
name = name.to_sym
|
13
|
+
validations.merge!({ presence: true })
|
14
|
+
|
15
|
+
validate(name, @params[name], validations)
|
16
|
+
end
|
17
|
+
|
18
|
+
def optional(name, options = {})
|
19
|
+
name = name.to_sym
|
20
|
+
if (@params[name].nil? || @params[name] == '') && options.has_key?(:default)
|
21
|
+
@params[name] = options.delete(:default)
|
22
|
+
end
|
23
|
+
validate(name, @params[name], options)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def validate(attribute, value, validations)
|
28
|
+
validations.each do |type, options|
|
29
|
+
validator_class = Validations.validators[type]
|
30
|
+
raise("Validator #{type} is not registered...") unless validator_class
|
31
|
+
validator_class.new.validate_param(attribute, value, options)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rosebud
|
2
|
+
module Validations
|
3
|
+
@validators = {}
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_reader :validators
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.register_validator!(name, klass)
|
10
|
+
raise(ArgumentError, 'Validation class type expected to be a subclass of Rosebud::Validator...') unless klass.ancestors.include?(Validator)
|
11
|
+
raise(NotImplementedError, 'Validator expected validate_param to be implemented...') unless klass.instance_methods.include?(:validate_param)
|
12
|
+
name = name.to_sym
|
13
|
+
Errawr.register!(name)
|
14
|
+
@validators[name] = klass
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Rosebud
|
2
|
+
module Validations
|
3
|
+
class PresenceValidator < Validator
|
4
|
+
def validate_param(name, value, options)
|
5
|
+
error!(:presence, param: name) if value.nil? || value.strip == ''
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
Validations.register_validator!(:presence, PresenceValidator)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Rosebud
|
2
|
+
module Validations
|
3
|
+
class RegexValidator < Validator
|
4
|
+
def validate_param(name, value, pattern)
|
5
|
+
error!(:regex, param: name) if value && (value.to_s =~ pattern).nil?
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
Validations.register_validator!(:regex, RegexValidator)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Rosebud
|
2
|
+
module Validations
|
3
|
+
class TypeValidator < Validator
|
4
|
+
def validate_param(name, value, type)
|
5
|
+
coerced_value = coerce_value(type, value)
|
6
|
+
error!(:type, param: name) unless valid_type?(type, coerced_value)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
def coerce_value(type, value)
|
11
|
+
coercer = Virtus::Attribute.build(type)
|
12
|
+
coercer.coerce(value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid_type?(type, value)
|
16
|
+
return true if value.nil?
|
17
|
+
if type == Virtus::Attribute::Boolean
|
18
|
+
value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
19
|
+
elsif type == Rack::Multipart::UploadedFile
|
20
|
+
value.key?(:tempfile)
|
21
|
+
else
|
22
|
+
value.is_a?(type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Validations.register_validator!(:type, TypeValidator)
|
28
|
+
end
|
29
|
+
end
|
data/rosebud.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rosebud/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rosebud'
|
8
|
+
spec.version = Rosebud::VERSION
|
9
|
+
spec.authors = ['Anthony Smith']
|
10
|
+
spec.email = ['anthony@sticksnleaves.com']
|
11
|
+
spec.description = %q{Rails API parameter validation}
|
12
|
+
spec.summary = %q{Finally! An easy way to validate your Rails API parameters.}
|
13
|
+
spec.homepage = 'http://www.github.com/anthonator/rosebud'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'virtus', '~> 1.0.0'
|
22
|
+
spec.add_runtime_dependency 'errawr', '~> 1.1.5'
|
23
|
+
spec.add_runtime_dependency 'errawr-rails', '~> 1.0.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec-rails'
|
28
|
+
end
|