params_validator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Christof Dorner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # params_validator ![Continuous Integration](https://secure.travis-ci.org/chdorner/epubinfo.png?branch=master)
2
+
3
+ A DSL for validating request parameters, raises exceptions when validation failed. Currently only supports Rails.
4
+
5
+ Current status is very much alpha, this gem is still under development and not intended to use in production applications.
6
+
7
+ # Installation
8
+
9
+ Add this to your Gemfile and run `bundle install`
10
+
11
+ gem 'params_validator'
12
+
13
+ # Usage
14
+
15
+ Basic usage:
16
+
17
+ class WelcomeController < ActionController::Base
18
+ rescue_from ParamsValidator::InvalidParamsException do |exception|
19
+ render :text => 'Error', :status => :bad_request
20
+ end
21
+
22
+ validate_params_for :index, { :count => { :with => [:type_integer] } }
23
+ def index
24
+ @articles = Article.limit(params[:count])
25
+ end
26
+ end
27
+
28
+ ## Validators
29
+
30
+ ### TypeInteger
31
+
32
+ validate_params_for :index, { :count => { :with => [:type_integer] } }
33
+
34
+ **Valid:**
35
+
36
+ * `count=1`
37
+
38
+ **Invalid:**
39
+
40
+ * `count=something`
41
+ * `count[]=1`
42
+ * `count[key]=value`
43
+
44
+ ### TypeFloat
45
+
46
+ validate_params_for :index, { :position => { :with => [:type_float] } }
47
+
48
+ **Valid:**
49
+
50
+ * `position=0.42`
51
+ * `position=1`
52
+
53
+ **Invalid:**
54
+
55
+ * `position=something`
56
+ * `position[]=0.42`
57
+ * `position[key]=0.42`
58
+
59
+ ### TypeString
60
+
61
+ validate_params_for :index, { :name => { :with => [:type_string] } }
62
+
63
+ **Valid:**
64
+
65
+ * `name=Jack`
66
+ * `name=1`
67
+
68
+ **Invalid:**
69
+
70
+ * `name[]=Jack`
71
+ * `name[key]=Jack`
72
+
73
+ ### TypeHash
74
+
75
+ validate_params_for :index, { :options => { :with => [:type_hash] } }
76
+
77
+ **Valid:**
78
+
79
+ * `options[all]=true`
80
+
81
+ **Invalid:**
82
+
83
+ * `options=all`
84
+ * `options[]=all`
85
+
86
+ ### TypeArray
87
+
88
+ validate_params_for :index, { :ids => { :with => [:type_array] } }
89
+
90
+ **Valid:**
91
+
92
+ * `ids[]=1`
93
+
94
+ **Invalid:**
95
+
96
+ * `ids=1`
97
+ * `ids[1]=true`
98
+
99
+ ## Contributing to params_validator
100
+
101
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
102
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
103
+ * Fork the project.
104
+ * Start a feature/bugfix branch.
105
+ * Commit and push until you are happy with your contribution.
106
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
107
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
108
+
109
+ ## Copyright
110
+
111
+ Copyright (c) 2012 Christof Dorner. See LICENSE.txt for
112
+ further details.
113
+
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default)
3
+
4
+ module ParamsValidator
5
+ class InvalidParamsException < Exception; end
6
+ class InvalidValidatorException < Exception; end
7
+ end
8
+
9
+ require 'params_validator/class_methods'
10
+ require 'params_validator/filter'
11
+ require 'params_validator/railtie'
12
+ require 'params_validator/validator'
13
+
@@ -0,0 +1,24 @@
1
+ module ParamsValidator
2
+ module ClassMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :params_validation_definitions
7
+ end
8
+
9
+ module ClassMethods
10
+ def validate_params_for(action, definition)
11
+ self.params_validation_definitions ||= {}
12
+ self.params_validation_definitions[action.to_sym] = definition
13
+
14
+ action_filter_name = "validate_params_for_action_#{action}".to_sym
15
+
16
+ define_method(action_filter_name) do
17
+ Filter::validate_params(params, definition)
18
+ end
19
+ self.before_filter action_filter_name, :only => action
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,22 @@
1
+ module ParamsValidator
2
+ module Filter
3
+ extend ActiveSupport::Inflector
4
+
5
+ def self.validate_params(params, definition)
6
+ definition.each do |field, validation_definition|
7
+ validation_definition[:with].each do |validator_name|
8
+ camelized_validator_name = self.camelize(validator_name)
9
+ begin
10
+ validator = constantize("ParamsValidator::Validator::#{camelized_validator_name}")
11
+ unless validator.valid?(params[field.to_s])
12
+ raise InvalidParamsException.new
13
+ end
14
+ rescue NameError
15
+ raise InvalidValidatorException.new(validator_name)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,10 @@
1
+ module ParamsValidator
2
+ class Railtie < Rails::Railtie
3
+ initializer 'params_validator.insert_into_action_controller' do
4
+ ActiveSupport.on_load :action_controller do
5
+ ActionController::Base.send(:include, ::ParamsValidator::ClassMethods)
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,6 @@
1
+ require 'params_validator/validator/type_integer'
2
+ require 'params_validator/validator/type_float'
3
+ require 'params_validator/validator/type_string'
4
+ require 'params_validator/validator/type_hash'
5
+ require 'params_validator/validator/type_array'
6
+
@@ -0,0 +1,10 @@
1
+ module ParamsValidator
2
+ module Validator
3
+ module TypeArray
4
+ def self.valid?(value)
5
+ value.kind_of? Array
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,15 @@
1
+ module ParamsValidator
2
+ module Validator
3
+ module TypeFloat
4
+ def self.valid?(value)
5
+ begin
6
+ Float(value)
7
+ true
8
+ rescue
9
+ false
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,10 @@
1
+ module ParamsValidator
2
+ module Validator
3
+ module TypeHash
4
+ def self.valid?(value)
5
+ value.kind_of? Hash
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,15 @@
1
+ module ParamsValidator
2
+ module Validator
3
+ module TypeInteger
4
+ def self.valid?(value)
5
+ begin
6
+ Integer(value)
7
+ true
8
+ rescue
9
+ false
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,10 @@
1
+ module ParamsValidator
2
+ module Validator
3
+ module TypeString
4
+ def self.valid?(value)
5
+ value.kind_of? String
6
+ end
7
+ end
8
+ end
9
+ end
10
+
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: params_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christof Dorner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70160681777260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70160681777260
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70160681793780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70160681793780
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &70160681789960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.7'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70160681789960
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &70160681812280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70160681812280
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &70160681810740 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70160681810740
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &70160681822060 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70160681822060
80
+ - !ruby/object:Gem::Dependency
81
+ name: guard
82
+ requirement: &70160681819620 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70160681819620
91
+ - !ruby/object:Gem::Dependency
92
+ name: guard-rspec
93
+ requirement: &70160681830320 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70160681830320
102
+ - !ruby/object:Gem::Dependency
103
+ name: rb-fsevent
104
+ requirement: &70160681826500 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70160681826500
113
+ - !ruby/object:Gem::Dependency
114
+ name: growl
115
+ requirement: &70160681823660 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70160681823660
124
+ description:
125
+ email: christof@chdorner.me
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files:
129
+ - LICENSE.txt
130
+ - README.md
131
+ files:
132
+ - lib/params_validator.rb
133
+ - lib/params_validator/class_methods.rb
134
+ - lib/params_validator/filter.rb
135
+ - lib/params_validator/railtie.rb
136
+ - lib/params_validator/validator.rb
137
+ - lib/params_validator/validator/type_array.rb
138
+ - lib/params_validator/validator/type_float.rb
139
+ - lib/params_validator/validator/type_hash.rb
140
+ - lib/params_validator/validator/type_integer.rb
141
+ - lib/params_validator/validator/type_string.rb
142
+ - LICENSE.txt
143
+ - README.md
144
+ homepage: https://github.com/chdorner/params_validator
145
+ licenses:
146
+ - MIT
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ segments:
158
+ - 0
159
+ hash: -4216775152249830731
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 1.8.11
169
+ signing_key:
170
+ specification_version: 3
171
+ summary: A DSL for validating request parameters, raises exceptions when validation
172
+ failed. Currently only supports Rails.
173
+ test_files: []