params_validator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -3
- data/lib/params_validator.rb +7 -1
- data/lib/params_validator/class_methods.rb +2 -1
- data/lib/params_validator/filter.rb +26 -14
- data/lib/params_validator/validator.rb +3 -1
- data/lib/params_validator/validator/base.rb +22 -0
- data/lib/params_validator/validator/presence.rb +5 -3
- data/lib/params_validator/validator/type.rb +29 -30
- data/lib/params_validator/validator/whitelist.rb +29 -0
- metadata +77 -25
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# params_validator [![Continuous Integration](https://secure.travis-ci.org/chdorner/
|
1
|
+
# params_validator [![Continuous Integration](https://secure.travis-ci.org/chdorner/params_validator.png?branch=master)](https://secure.travis-ci.org/chdorner/params_validator)
|
2
2
|
|
3
3
|
A DSL for validating request parameters, raises exceptions when validation failed. Currently only supports Rails.
|
4
4
|
|
@@ -109,6 +109,19 @@ Basic usage:
|
|
109
109
|
* `q=`
|
110
110
|
* `somethingelse=term`
|
111
111
|
|
112
|
+
### Whitelist
|
113
|
+
|
114
|
+
validate_params_for :search, { :type => { :_with => [:whitelist], :_whitelist: [:documents, :pictures], :_default: :documents } }
|
115
|
+
|
116
|
+
**Valid:**
|
117
|
+
|
118
|
+
* `type=documents`
|
119
|
+
* `type=pictures`
|
120
|
+
|
121
|
+
**Invalid:**
|
122
|
+
|
123
|
+
* `type=unicorns`
|
124
|
+
|
112
125
|
## Validation errors
|
113
126
|
|
114
127
|
When validation of parameters fails, this gem is raising a `ParamsValidator::InvalidParamsException` error which contains an `errors` hash with further information.
|
@@ -127,7 +140,7 @@ As briefly described in the example at the beginning, the easiest way to rescue
|
|
127
140
|
end
|
128
141
|
|
129
142
|
## Contributing to params_validator
|
130
|
-
|
143
|
+
|
131
144
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
132
145
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
133
146
|
* Fork the project.
|
@@ -138,6 +151,6 @@ As briefly described in the example at the beginning, the easiest way to rescue
|
|
138
151
|
|
139
152
|
## Copyright
|
140
153
|
|
141
|
-
Copyright (c)
|
154
|
+
Copyright (c) 2013 Christof Dorner. See LICENSE.txt for
|
142
155
|
further details.
|
143
156
|
|
data/lib/params_validator.rb
CHANGED
@@ -4,7 +4,13 @@ Bundler.require(:default)
|
|
4
4
|
module ParamsValidator
|
5
5
|
class InvalidValidatorException < Exception; end
|
6
6
|
class InvalidParamsException < Exception
|
7
|
-
|
7
|
+
attr_reader :errors
|
8
|
+
|
9
|
+
def initialize(errors)
|
10
|
+
@errors = errors
|
11
|
+
|
12
|
+
super()
|
13
|
+
end
|
8
14
|
end
|
9
15
|
end
|
10
16
|
|
@@ -14,7 +14,8 @@ module ParamsValidator
|
|
14
14
|
action_filter_name = "validate_params_for_action_#{action}".to_sym
|
15
15
|
|
16
16
|
define_method(action_filter_name) do
|
17
|
-
Filter
|
17
|
+
sanitized_params = Filter.sanitize_params(params, definition)
|
18
|
+
request.parameters.merge!(sanitized_params)
|
18
19
|
end
|
19
20
|
self.before_filter action_filter_name, :only => action
|
20
21
|
end
|
@@ -2,33 +2,45 @@ module ParamsValidator
|
|
2
2
|
module Filter
|
3
3
|
extend ActiveSupport::Inflector
|
4
4
|
|
5
|
-
|
5
|
+
class << self
|
6
|
+
attr_accessor :params
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.sanitize_params(params, definition)
|
10
|
+
self.params = params
|
6
11
|
errors = {}
|
7
12
|
definition.each do |field, validation_definition|
|
8
|
-
errors = validate_field(field,
|
13
|
+
errors = validate_field(field, validation_definition, errors)
|
9
14
|
|
10
|
-
validation_definition.reject {|k,v| k
|
11
|
-
|
15
|
+
validation_definition.reject {|k,v| reserved_keys.include?(k) }.each do |nested_field, nested_validation_definition|
|
16
|
+
sanitize_params(params[field.to_s], { nested_field => nested_validation_definition })
|
12
17
|
end
|
13
18
|
end
|
14
|
-
|
15
|
-
|
16
|
-
exception.errors = errors
|
17
|
-
raise exception
|
18
|
-
end
|
19
|
+
raise InvalidParamsException.new(errors) unless errors.empty?
|
20
|
+
self.params
|
19
21
|
end
|
20
22
|
|
21
23
|
private
|
22
24
|
|
23
|
-
def self.
|
24
|
-
|
25
|
-
|
25
|
+
def self.reserved_keys
|
26
|
+
@reserved_keys ||= [:_default, :_whitelist, :_with].to_set
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.validate_field(field, validation_definition, errors)
|
30
|
+
validators = validation_definition[:_with]
|
31
|
+
return errors unless validators
|
32
|
+
validators.each do |validator_name|
|
26
33
|
camelized_validator_name = self.camelize(validator_name)
|
27
34
|
begin
|
28
|
-
validator =
|
35
|
+
validator = ParamsValidator::Validator.const_get(camelized_validator_name)
|
36
|
+
validator = validator.new(validation_definition)
|
29
37
|
value = params.is_a?(Hash) ? params[field.to_s] : nil
|
30
38
|
unless validator.valid?(value)
|
31
|
-
|
39
|
+
if validator.respond_to?(:default?) && validator.default?
|
40
|
+
params[field] = validator.default
|
41
|
+
else
|
42
|
+
errors[field] = validator.error_message
|
43
|
+
end
|
32
44
|
end
|
33
45
|
rescue NameError
|
34
46
|
raise InvalidValidatorException.new(validator_name)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ParamsValidator
|
2
|
+
module Validator
|
3
|
+
class Base
|
4
|
+
attr_reader :default
|
5
|
+
|
6
|
+
def initialize(definition={})
|
7
|
+
end
|
8
|
+
|
9
|
+
def default?
|
10
|
+
instance_variable_defined?('@default')
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_message
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid?(value)
|
18
|
+
raise NotImplementedError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,50 +1,49 @@
|
|
1
1
|
module ParamsValidator
|
2
2
|
module Validator
|
3
|
+
class Type < Base
|
4
|
+
def error_message
|
5
|
+
"is not of type #{type}"
|
6
|
+
end
|
3
7
|
|
4
|
-
|
8
|
+
def valid?(value)
|
9
|
+
true unless Presence.new.valid?(value)
|
10
|
+
end
|
5
11
|
|
6
|
-
module Base
|
7
12
|
private
|
8
|
-
def define_type_validator(type_name, &block)
|
9
|
-
@type_name = type_name
|
10
|
-
@block = block
|
11
|
-
|
12
|
-
def self.error_message
|
13
|
-
"is not of type #{@type_name.to_s.downcase}"
|
14
|
-
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
return !!@block.call(value) rescue false
|
19
|
-
end
|
14
|
+
def type
|
15
|
+
self.class.to_s.split(/type/i).last.downcase
|
20
16
|
end
|
21
17
|
end
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
define_type_validator('integer') { |value| Integer(value) }
|
19
|
+
class TypeInteger < Type
|
20
|
+
def valid?(value)
|
21
|
+
super || !!Integer(value) rescue false
|
22
|
+
end
|
28
23
|
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
class TypeFloat < Type
|
26
|
+
def valid?(value)
|
27
|
+
super || !!Float(value) rescue false
|
28
|
+
end
|
33
29
|
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
class TypeString < Type
|
32
|
+
def valid?(value)
|
33
|
+
super || value.kind_of?(String)
|
34
|
+
end
|
38
35
|
end
|
39
36
|
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
class TypeArray < Type
|
38
|
+
def valid?(value)
|
39
|
+
super || value.kind_of?(Array)
|
40
|
+
end
|
43
41
|
end
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
class TypeHash < Type
|
44
|
+
def valid?(value)
|
45
|
+
super || value.kind_of?(Hash)
|
46
|
+
end
|
48
47
|
end
|
49
48
|
end
|
50
49
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ParamsValidator
|
2
|
+
module Validator
|
3
|
+
class Whitelist
|
4
|
+
attr_reader :default
|
5
|
+
|
6
|
+
def initialize(definition)
|
7
|
+
@default = definition[:_default] if definition.key?(:_default)
|
8
|
+
@whitelist = definition[:_whitelist].map(&:to_s).to_set
|
9
|
+
end
|
10
|
+
|
11
|
+
def default?
|
12
|
+
instance_variable_defined?('@default')
|
13
|
+
end
|
14
|
+
|
15
|
+
def error_message
|
16
|
+
'is invalid'
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid?(value)
|
20
|
+
whitelist.include?(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :whitelist
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: params_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-06-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ~>
|
@@ -32,21 +37,31 @@ dependencies:
|
|
32
37
|
version: 2.9.0
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.9.0
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: yard
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
42
52
|
- !ruby/object:Gem::Version
|
43
|
-
version: '0.
|
53
|
+
version: '0.8'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: rdoc
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ~>
|
@@ -54,10 +69,15 @@ dependencies:
|
|
54
69
|
version: '3.12'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.12'
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: bundler
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
83
|
- - ! '>='
|
@@ -65,10 +85,15 @@ dependencies:
|
|
65
85
|
version: '0'
|
66
86
|
type: :development
|
67
87
|
prerelease: false
|
68
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: jeweler
|
71
|
-
requirement:
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
72
97
|
none: false
|
73
98
|
requirements:
|
74
99
|
- - ~>
|
@@ -76,10 +101,15 @@ dependencies:
|
|
76
101
|
version: 1.8.3
|
77
102
|
type: :development
|
78
103
|
prerelease: false
|
79
|
-
version_requirements:
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.8.3
|
80
110
|
- !ruby/object:Gem::Dependency
|
81
111
|
name: guard
|
82
|
-
requirement:
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
83
113
|
none: false
|
84
114
|
requirements:
|
85
115
|
- - ! '>='
|
@@ -87,10 +117,15 @@ dependencies:
|
|
87
117
|
version: '0'
|
88
118
|
type: :development
|
89
119
|
prerelease: false
|
90
|
-
version_requirements:
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
91
126
|
- !ruby/object:Gem::Dependency
|
92
127
|
name: guard-rspec
|
93
|
-
requirement:
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
94
129
|
none: false
|
95
130
|
requirements:
|
96
131
|
- - ! '>='
|
@@ -98,10 +133,15 @@ dependencies:
|
|
98
133
|
version: '0'
|
99
134
|
type: :development
|
100
135
|
prerelease: false
|
101
|
-
version_requirements:
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
102
142
|
- !ruby/object:Gem::Dependency
|
103
143
|
name: rb-fsevent
|
104
|
-
requirement:
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
105
145
|
none: false
|
106
146
|
requirements:
|
107
147
|
- - ! '>='
|
@@ -109,10 +149,15 @@ dependencies:
|
|
109
149
|
version: '0'
|
110
150
|
type: :development
|
111
151
|
prerelease: false
|
112
|
-
version_requirements:
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ! '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
113
158
|
- !ruby/object:Gem::Dependency
|
114
159
|
name: growl
|
115
|
-
requirement:
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
116
161
|
none: false
|
117
162
|
requirements:
|
118
163
|
- - ! '>='
|
@@ -120,7 +165,12 @@ dependencies:
|
|
120
165
|
version: '0'
|
121
166
|
type: :development
|
122
167
|
prerelease: false
|
123
|
-
version_requirements:
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ! '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
124
174
|
description:
|
125
175
|
email: christof@chdorner.me
|
126
176
|
executables: []
|
@@ -134,8 +184,10 @@ files:
|
|
134
184
|
- lib/params_validator/filter.rb
|
135
185
|
- lib/params_validator/railtie.rb
|
136
186
|
- lib/params_validator/validator.rb
|
187
|
+
- lib/params_validator/validator/base.rb
|
137
188
|
- lib/params_validator/validator/presence.rb
|
138
189
|
- lib/params_validator/validator/type.rb
|
190
|
+
- lib/params_validator/validator/whitelist.rb
|
139
191
|
- LICENSE.txt
|
140
192
|
- README.md
|
141
193
|
homepage: https://github.com/chdorner/params_validator
|
@@ -153,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
153
205
|
version: '0'
|
154
206
|
segments:
|
155
207
|
- 0
|
156
|
-
hash:
|
208
|
+
hash: -1148739892296269609
|
157
209
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
210
|
none: false
|
159
211
|
requirements:
|
@@ -162,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
214
|
version: '0'
|
163
215
|
requirements: []
|
164
216
|
rubyforge_project:
|
165
|
-
rubygems_version: 1.8.
|
217
|
+
rubygems_version: 1.8.23
|
166
218
|
signing_key:
|
167
219
|
specification_version: 3
|
168
220
|
summary: A DSL for validating request parameters, raises exceptions when validation
|