allowed_params 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +56 -9
- data/lib/allowed_params/helper.rb +8 -4
- data/lib/allowed_params/validator_builder.rb +3 -2
- data/lib/allowed_params/version.rb +1 -1
- metadata +16 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d84fd4bf309d1033e2c8192fb33240efbe49481c
|
4
|
+
data.tar.gz: ca19af6df7e37a423d1ad648b3078d296a249882
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8574c49f9a50508728969a4f75a4e968a239c0a22faebcae4375542885fd7a24dc736c29d1c3c1e3870641ce1d78fa629c573950bea909bb63f9627ee6161f6
|
7
|
+
data.tar.gz: 09ee405c1a72ca1474e63572d8d6f999e8fbb288105e3263eb4b6b45f03187fd1488e01ef8341af1cd111dc99eec8134e12b515cd49ca4e763236ef408024ac7
|
data/README.md
CHANGED
@@ -1,25 +1,53 @@
|
|
1
1
|
# AllowedParams
|
2
2
|
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
This project uses [semantic versioning](http://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
Add it to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'allowed_params'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself with:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
$ gem install allowed_params
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
3
28
|
This gem provides filtering and validations of params
|
29
|
+
|
30
|
+
## Params validation with whitelist
|
4
31
|
|
5
32
|
```ruby
|
6
33
|
|
7
|
-
class
|
34
|
+
class EmployersController < ApplicationController
|
8
35
|
include AllowedParams::Helper
|
9
36
|
|
10
|
-
params do
|
11
|
-
|
12
|
-
|
37
|
+
params whitelist: true do
|
38
|
+
validate :id, presence: true
|
39
|
+
validate :name, presence: true
|
40
|
+
validate :position, inclusion: { in: %w(manager developer) }
|
13
41
|
end
|
14
42
|
def update
|
15
43
|
# do the job
|
16
|
-
end
|
44
|
+
end
|
17
45
|
end
|
18
46
|
|
19
47
|
```
|
20
|
-
|
21
|
-
This will validate
|
22
|
-
|
48
|
+
|
49
|
+
This will validate listed params and raise `AllowedParams::ValidationError` in case of invalid value.
|
50
|
+
All other params are not allowed, and `AllowedParams::NotAllowedError` will be raised if present.
|
23
51
|
|
24
52
|
To allow params on all controllers:
|
25
53
|
|
@@ -27,7 +55,26 @@ To allow params on all controllers:
|
|
27
55
|
|
28
56
|
AllowedParams.config.allowed_params = [:format]
|
29
57
|
|
58
|
+
```
|
59
|
+
|
60
|
+
## Just validation
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class CatsController < ApplicationController
|
64
|
+
include AllowedParams::Helper
|
65
|
+
|
66
|
+
params do
|
67
|
+
validates :name, presence: true
|
68
|
+
validates :kind, inclusion: { in: %w(fluffy bald) }
|
69
|
+
end
|
70
|
+
def update
|
71
|
+
# do the job
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
30
75
|
```
|
31
76
|
|
32
|
-
This
|
77
|
+
This will validate `params[:name], params[:kind]` and raise `AllowedParams::ValidationError` in case of invalid value.
|
78
|
+
All other params will just go through without any checks.
|
33
79
|
|
80
|
+
This project rocks and uses MIT-LICENSE.
|
@@ -8,29 +8,33 @@ module AllowedParams
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
-
def params(&block)
|
11
|
+
def params(opts={}, &block)
|
12
12
|
builder = ::AllowedParams::ValidatorBuilder.new(self)
|
13
13
|
builder.instance_eval(&block) if block_given?
|
14
14
|
@request_params_validator = builder.validator
|
15
|
+
@whitelist = opts.fetch(:whitelist, false)
|
15
16
|
end
|
16
17
|
|
17
18
|
def method_added(method)
|
18
19
|
if instance_variable_get(:@request_params_validator).present?
|
19
20
|
request_params_validator = @request_params_validator
|
20
21
|
@request_params_validator = nil
|
22
|
+
whitelist = @whitelist
|
23
|
+
@whitelist = nil
|
21
24
|
|
22
25
|
before_filter only: [method] do
|
23
26
|
request_params = request_params_validator.new(params)
|
24
27
|
if request_params.invalid?
|
25
28
|
raise ValidationError, request_params.errors.full_messages.first
|
26
29
|
end
|
27
|
-
if request_params.
|
28
|
-
raise NotAllowedError, request_params.
|
30
|
+
if whitelist && request_params.not_white_listed.present?
|
31
|
+
raise NotAllowedError, request_params.not_white_listed.join(', ')
|
29
32
|
end
|
30
33
|
end
|
31
34
|
end
|
35
|
+
|
32
36
|
super
|
33
37
|
end
|
34
38
|
end
|
35
39
|
end
|
36
|
-
end
|
40
|
+
end
|
@@ -10,7 +10,7 @@ module AllowedParams
|
|
10
10
|
@params = {}
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def validate(name, options = {})
|
14
14
|
@params[name] = options
|
15
15
|
end
|
16
16
|
|
@@ -40,9 +40,10 @@ module AllowedParams
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
43
|
+
def not_white_listed
|
44
44
|
@params.keys.map(&:to_s) - members.map(&:to_s)
|
45
45
|
end
|
46
|
+
|
46
47
|
end
|
47
48
|
end
|
48
49
|
end
|
metadata
CHANGED
@@ -1,74 +1,62 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: allowed_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tema Bolshakov
|
8
|
+
- Dmitry Rybakov
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-20 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rails
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- - "
|
18
|
+
- - "~>"
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '3'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: sqlite3
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
20
|
+
version: '4.0'
|
34
21
|
type: :development
|
35
22
|
prerelease: false
|
36
23
|
version_requirements: !ruby/object:Gem::Requirement
|
37
24
|
requirements:
|
38
|
-
- - "
|
25
|
+
- - "~>"
|
39
26
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
27
|
+
version: '4.0'
|
41
28
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
29
|
+
name: sqlite3
|
43
30
|
requirement: !ruby/object:Gem::Requirement
|
44
31
|
requirements:
|
45
|
-
- - "
|
32
|
+
- - "~>"
|
46
33
|
- !ruby/object:Gem::Version
|
47
34
|
version: '0'
|
48
35
|
type: :development
|
49
36
|
prerelease: false
|
50
37
|
version_requirements: !ruby/object:Gem::Requirement
|
51
38
|
requirements:
|
52
|
-
- - "
|
39
|
+
- - "~>"
|
53
40
|
- !ruby/object:Gem::Version
|
54
41
|
version: '0'
|
55
42
|
- !ruby/object:Gem::Dependency
|
56
43
|
name: rspec-rails
|
57
44
|
requirement: !ruby/object:Gem::Requirement
|
58
45
|
requirements:
|
59
|
-
- - "
|
46
|
+
- - "~>"
|
60
47
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
48
|
+
version: '3'
|
62
49
|
type: :development
|
63
50
|
prerelease: false
|
64
51
|
version_requirements: !ruby/object:Gem::Requirement
|
65
52
|
requirements:
|
66
|
-
- - "
|
53
|
+
- - "~>"
|
67
54
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
55
|
+
version: '3'
|
69
56
|
description: Allow to filter and validate params on rails controllers
|
70
57
|
email:
|
71
58
|
- abolshakov@spbtv.com
|
59
|
+
- drybakov@spbtv.com
|
72
60
|
executables: []
|
73
61
|
extensions: []
|
74
62
|
extra_rdoc_files: []
|
@@ -81,7 +69,7 @@ files:
|
|
81
69
|
- lib/allowed_params/model.rb
|
82
70
|
- lib/allowed_params/validator_builder.rb
|
83
71
|
- lib/allowed_params/version.rb
|
84
|
-
homepage:
|
72
|
+
homepage: https://github.com/SPBTV/allowed_params
|
85
73
|
licenses:
|
86
74
|
- MIT
|
87
75
|
metadata: {}
|