param_validator 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +22 -0
- data/Rakefile +1 -0
- data/lib/param_validator/base.rb +79 -0
- data/lib/param_validator/controller.rb +31 -0
- data/lib/param_validator/invalid_parameters.rb +5 -0
- data/lib/param_validator/version.rb +3 -0
- data/lib/param_validator.rb +9 -0
- data/param_validator.gemspec +25 -0
- metadata +113 -0
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemdev
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p392
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jason Harrelson
|
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,22 @@
|
|
1
|
+
# ParamValidator
|
2
|
+
|
3
|
+
Validate parameters in Rails controllers or options passed to methods.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'param_validator'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install param_validator
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module ParamValidator
|
2
|
+
class Base
|
3
|
+
|
4
|
+
class_attribute :specification
|
5
|
+
self.specification = {}
|
6
|
+
|
7
|
+
attr_reader :params
|
8
|
+
|
9
|
+
def initialize( params )
|
10
|
+
@params = params
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.inherited( base )
|
14
|
+
self.specification = {}
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid?
|
19
|
+
self.class.specification.each do |param, spec|
|
20
|
+
value = fetch_parameter( param )
|
21
|
+
if spec[:required] && (value.nil? || value.empty?)
|
22
|
+
errors << [param, 'must be present']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
errors.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
def errors
|
30
|
+
@errors ||= []
|
31
|
+
end
|
32
|
+
|
33
|
+
def full_errors
|
34
|
+
errors.map do |param_and_msg|
|
35
|
+
param_and_msg.join( ' ' )
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.validate( param, spec )
|
40
|
+
self.specification = specification.merge( param => spec )
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def fetch_parameter( path )
|
46
|
+
path( params, path )
|
47
|
+
end
|
48
|
+
|
49
|
+
def path( hash, *pathes )
|
50
|
+
target = hash
|
51
|
+
pathes.map! do |p|
|
52
|
+
p.to_s.split(/[\/\.]+/)
|
53
|
+
end
|
54
|
+
pathes.flatten.each do |element|
|
55
|
+
next if (element == nil || element == '')
|
56
|
+
key, index = parse_element__(element)
|
57
|
+
target = target[key] || target[key.to_s]
|
58
|
+
return nil unless target
|
59
|
+
if index
|
60
|
+
raise "target=#{target.inspect} is not array. but specified index value." unless target.is_a?(Array)
|
61
|
+
target = target[index]
|
62
|
+
return nil unless target
|
63
|
+
end
|
64
|
+
end
|
65
|
+
target
|
66
|
+
end
|
67
|
+
|
68
|
+
def parse_element__(elm_string)
|
69
|
+
if elm_string =~ /^(.+)\[(\d+)\]$/
|
70
|
+
[$1.to_sym, $2.to_i]
|
71
|
+
elsif elm_string =~ /^(.+)_(\d+)$/
|
72
|
+
[$1.to_sym, $2.to_i]
|
73
|
+
else
|
74
|
+
[elm_string.to_sym, nil]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ParamValidator
|
2
|
+
module Controller
|
3
|
+
|
4
|
+
def self.included( other_module )
|
5
|
+
other_module.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def validate_parameters( *actions )
|
11
|
+
before_filter :validate_parameters, :only => actions
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_parameters
|
17
|
+
klass_name = [self.class.name.gsub( /Controller/, '' ), "#{action_name.titlecase}ParamValidator"].join( '::' )
|
18
|
+
klass = klass_name.constantize
|
19
|
+
validator = klass.new( params )
|
20
|
+
unless validator.valid?
|
21
|
+
raise ParamValidator::InvalidParameters.new( validator.full_errors.join( ', '))
|
22
|
+
end
|
23
|
+
rescue NameError => e
|
24
|
+
raise if e.is_a?( NoMethodError )
|
25
|
+
raise NotImplementedError,
|
26
|
+
"please implement #{klass_name} param validator"
|
27
|
+
e.backtrace
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'param_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "param_validator"
|
8
|
+
spec.version = ParamValidator::VERSION
|
9
|
+
spec.authors = ["C. Jason Harrelson"]
|
10
|
+
spec.email = ["jason@lookforwardenterprises.com"]
|
11
|
+
spec.description = %q{Validate parameters in Rails controllers.}
|
12
|
+
spec.summary = %q{Validate parameters in Rails controllers.}
|
13
|
+
spec.homepage = ""
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "activesupport", "~> 3"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: param_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- C. Jason Harrelson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
description: Validate parameters in Rails controllers.
|
63
|
+
email:
|
64
|
+
- jason@lookforwardenterprises.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .ruby-gemset
|
71
|
+
- .ruby-version
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/param_validator.rb
|
77
|
+
- lib/param_validator/base.rb
|
78
|
+
- lib/param_validator/controller.rb
|
79
|
+
- lib/param_validator/invalid_parameters.rb
|
80
|
+
- lib/param_validator/version.rb
|
81
|
+
- param_validator.gemspec
|
82
|
+
homepage: ''
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
hash: 4556885692516463923
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
hash: 4556885692516463923
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.25
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Validate parameters in Rails controllers.
|
113
|
+
test_files: []
|