sinatra-params-validator 0.0.2 → 0.0.3
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.
- data/README.md +22 -4
- data/VERSION +1 -1
- data/lib/rack/validator/sinatra.rb +37 -4
- data/sinatra-params-validator.gemspec +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -66,7 +66,9 @@ class App < Sinatra::Base
|
|
66
66
|
|
67
67
|
validation_required :POST, '/group', :params => [
|
68
68
|
{ :name => :name, :required => true },
|
69
|
-
{ :name => :private, :type => :boolean, :required => true }
|
69
|
+
{ :name => :private, :type => :boolean, :required => true },
|
70
|
+
{ :name => :type, :set => %{public private}, :default => 'public' },
|
71
|
+
{ :name => :param, :action => [ :trim, :downcase ] }
|
70
72
|
]
|
71
73
|
|
72
74
|
post '/group' do
|
@@ -78,23 +80,32 @@ end
|
|
78
80
|
|
79
81
|
Try it out yourself and run this in the root directory `rackup example/app.rb`.
|
80
82
|
|
83
|
+
__NOTE__ Every parameter that is used for that particular request needs to be mentioned in the params
|
84
|
+
array. Every parameter that hasn't been mentioned will be delete and won't be available in the sinatra
|
85
|
+
`params` variable when your sinatra code block is invoked.
|
86
|
+
|
81
87
|
## Validator class
|
82
88
|
|
83
89
|
A class with several methods to validate and clean data from sinatra params holder.
|
84
|
-
Can be used for an adopter for other libraries/frameworks, like rails.
|
90
|
+
Can be used as a basis for an adopter for other libraries/frameworks, like rails.
|
85
91
|
|
86
92
|
### Examples
|
87
93
|
|
88
94
|
Initialize class in lazy mode, this means that once a validation fail the following ones are not executed:
|
89
95
|
|
90
96
|
```ruby
|
97
|
+
require 'rack/validator'
|
98
|
+
|
91
99
|
validator = Rack::Validator.new(params)
|
92
100
|
#required_3 is not present
|
93
101
|
validator.required [:required_1, :required_2, :required_3]
|
94
|
-
validator.trim
|
95
|
-
validator.downcase
|
102
|
+
validator.trim :required_1
|
103
|
+
validator.downcase :required_2
|
104
|
+
validator.downcase :other
|
96
105
|
validator.is_in_range 3, 32, :required_1
|
97
106
|
validator.is_email :contact
|
107
|
+
validator.is_boolean :private
|
108
|
+
validator.is_set %{public private}, :type
|
98
109
|
validator.matches /[a-z]{2}_[A-Z]{2}|[a-z]{2}/i, :locale
|
99
110
|
|
100
111
|
if validator.has_errors?
|
@@ -117,7 +128,14 @@ Available methods:
|
|
117
128
|
* is_in_range
|
118
129
|
* is_less_equal_than
|
119
130
|
* is_email
|
131
|
+
* is_set
|
120
132
|
* is_boolean
|
121
133
|
* matches
|
134
|
+
* clean_parameters
|
122
135
|
|
123
136
|
To see more examples check tests/params_validator_spec.rb
|
137
|
+
|
138
|
+
## Contribution
|
139
|
+
|
140
|
+
If you like the project, fork me and help make sinatra better.
|
141
|
+
Make sure to include test cases and explanation for the implemented feature.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -6,6 +6,7 @@ module Rack
|
|
6
6
|
attr_reader :invalid_params
|
7
7
|
attr_reader :missing_params
|
8
8
|
attr_reader :messages
|
9
|
+
attr_reader :params
|
9
10
|
attr_accessor :lazy_mode
|
10
11
|
|
11
12
|
def initialize(params, lazy_mode = true)
|
@@ -20,12 +21,12 @@ module Rack
|
|
20
21
|
@invalid_params.length > 0 or @missing_params.length > 0
|
21
22
|
end
|
22
23
|
|
23
|
-
def trim
|
24
|
-
@params.
|
24
|
+
def trim(key)
|
25
|
+
@params[key.to_s] = @params[key.to_s].strip if @params[key.to_s]
|
25
26
|
end
|
26
27
|
|
27
|
-
def downcase(
|
28
|
-
|
28
|
+
def downcase(key)
|
29
|
+
@params[key.to_s] = @params[key.to_s].to_s.downcase if @params[key.to_s]
|
29
30
|
end
|
30
31
|
|
31
32
|
def required(keys)
|
@@ -127,6 +128,16 @@ module Rack
|
|
127
128
|
end
|
128
129
|
end
|
129
130
|
|
131
|
+
def is_set(array, key)
|
132
|
+
if lazy_check_disabled
|
133
|
+
key = key.to_s
|
134
|
+
unless array.include? @params[key]
|
135
|
+
@invalid_params.push(key)
|
136
|
+
@messages.push("#{key} does not match #{array}")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
130
141
|
def is_boolean(key)
|
131
142
|
if lazy_check_disabled
|
132
143
|
key = key.to_s
|
@@ -147,6 +158,12 @@ module Rack
|
|
147
158
|
end
|
148
159
|
end
|
149
160
|
|
161
|
+
def clean_parameters(all_parameters)
|
162
|
+
@params.each_key do |key|
|
163
|
+
@params.delete key.to_s unless all_parameters.include? key.to_s
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
150
167
|
private
|
151
168
|
|
152
169
|
def lazy_check_disabled
|
@@ -179,26 +196,34 @@ module Rack
|
|
179
196
|
# TODO: Needs a general cleanup!!!
|
180
197
|
def validate_parameters(options)
|
181
198
|
validator = Rack::Validator.new params, false
|
199
|
+
all_params = [ ]
|
182
200
|
required_params = [ ]
|
183
201
|
integer_params = [ ]
|
184
202
|
float_params = [ ]
|
185
203
|
email_params = [ ]
|
186
204
|
range_params = [ ]
|
205
|
+
set_params = [ ]
|
187
206
|
boolean_params = [ ]
|
188
207
|
matches_params = [ ]
|
189
208
|
default_params = [ ]
|
209
|
+
action_params = [ ]
|
190
210
|
|
191
211
|
options[:params].each do |param|
|
212
|
+
all_params << (param[:name].to_s)
|
192
213
|
required_params << (param[:name]) if param[:required]
|
193
214
|
integer_params << (param) if param[:type] == :integer
|
194
215
|
float_params << (param) if param[:type] == :float
|
195
216
|
email_params << (param) if param[:type] == :email
|
196
217
|
range_params << (param) if param[:range]
|
218
|
+
set_params << (param) if param[:set]
|
197
219
|
boolean_params << (param) if param[:type] == :boolean
|
198
220
|
matches_params << (param) if param[:matches]
|
199
221
|
default_params << (param) if param[:default]
|
222
|
+
action_params << (param) if param[:action]
|
200
223
|
end
|
201
224
|
|
225
|
+
validator.clean_parameters all_params
|
226
|
+
|
202
227
|
validator.required required_params
|
203
228
|
|
204
229
|
integer_params.each do |param|
|
@@ -213,6 +238,9 @@ module Rack
|
|
213
238
|
range_params.each do |param|
|
214
239
|
validator.is_in_range param[:range].first, param[:range].last, param[:name] unless params[param[:name].to_s].nil?
|
215
240
|
end
|
241
|
+
set_params.each do |param|
|
242
|
+
validator.is_set param[:set], param[:name] unless params[param[:name].to_s].nil?
|
243
|
+
end
|
216
244
|
boolean_params.each do |param|
|
217
245
|
validator.is_boolean param[:name] unless params[param[:name].to_s].nil?
|
218
246
|
end
|
@@ -228,6 +256,11 @@ module Rack
|
|
228
256
|
end
|
229
257
|
end
|
230
258
|
|
259
|
+
action_params.each do |param|
|
260
|
+
validator.downcase param[:name] if param[:action].include? :downcase
|
261
|
+
validator.trim param[:name] if param[:action].include? :trim
|
262
|
+
end
|
263
|
+
|
231
264
|
if validator.has_errors?
|
232
265
|
@env['validator.missing'] = validator.missing_params
|
233
266
|
@env['validator.invalid'] = validator.invalid_params
|
@@ -3,7 +3,7 @@ $: << File.dirname(__FILE__) + "/lib"
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "sinatra-params-validator"
|
5
5
|
spec.version = IO.read("VERSION")
|
6
|
-
spec.authors = ["tsov", "
|
6
|
+
spec.authors = ["tsov", "lube8uy"]
|
7
7
|
spec.email = "tsov@me.com"
|
8
8
|
spec.homepage = "http://github.com/tsov/#{spec.name}"
|
9
9
|
spec.summary = "A Sinatra Module to validate incoming parameters"
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-params-validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- tsov
|
9
|
-
-
|
9
|
+
- lube8uy
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-05-
|
13
|
+
date: 2013-05-31 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: This sinatra module validates the incoming parameter and lets you configure
|
16
16
|
the pattern.
|