params_validator 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # params_validator ![Continuous Integration](https://secure.travis-ci.org/chdorner/epubinfo.png?branch=master)
1
+ # params_validator [![Continuous Integration](https://secure.travis-ci.org/chdorner/epubinfo.png?branch=master)](https://secure.travis-ci.org/chdorner/params_validator) [![Dependencies](https://gemnasium.com/chdorner/epubinfo.png)](https://gemnasium.com/chdorner/params_validator)
2
2
 
3
3
  A DSL for validating request parameters, raises exceptions when validation failed. Currently only supports Rails.
4
4
 
@@ -96,6 +96,36 @@ Basic usage:
96
96
  * `ids=1`
97
97
  * `ids[1]=true`
98
98
 
99
+ ### Presence
100
+
101
+ validate_params_for :search, { :q => { :with => [:presence] } }
102
+
103
+ **Valid:**
104
+
105
+ * `q=term`
106
+
107
+ **Invalid:**
108
+
109
+ * `q=`
110
+ * `somethingelse=term`
111
+
112
+ ## Validation errors
113
+
114
+ When validation of parameters fails, this gem is raising a `ParamsValidator::InvalidParamsException` error which contains an `errors` hash with further information.
115
+
116
+ The `errors` hash is modeled after the Rails model validation errors like this:
117
+
118
+ {
119
+ :field_name => 'error description',
120
+ :integer_field => 'is not of type integer'
121
+ }
122
+
123
+ As briefly described in the example at the beginning, the easiest way to rescue from validation errors is with a `rescue_from` block inside your controller, like this:
124
+
125
+ rescue_from ParamsValidator::InvalidParamsException do |exception|
126
+ render :json => { :errors => exception.errors }, :status => :bad_request
127
+ end
128
+
99
129
  ## Contributing to params_validator
100
130
 
101
131
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
@@ -2,8 +2,10 @@ require 'bundler/setup'
2
2
  Bundler.require(:default)
3
3
 
4
4
  module ParamsValidator
5
- class InvalidParamsException < Exception; end
6
5
  class InvalidValidatorException < Exception; end
6
+ class InvalidParamsException < Exception
7
+ attr_accessor :errors
8
+ end
7
9
  end
8
10
 
9
11
  require 'params_validator/class_methods'
@@ -3,19 +3,25 @@ module ParamsValidator
3
3
  extend ActiveSupport::Inflector
4
4
 
5
5
  def self.validate_params(params, definition)
6
+ errors = {}
6
7
  definition.each do |field, validation_definition|
7
8
  validation_definition[:with].each do |validator_name|
8
9
  camelized_validator_name = self.camelize(validator_name)
9
10
  begin
10
11
  validator = constantize("ParamsValidator::Validator::#{camelized_validator_name}")
11
12
  unless validator.valid?(params[field.to_s])
12
- raise InvalidParamsException.new
13
+ errors[field] = validator.error_message
13
14
  end
14
15
  rescue NameError
15
16
  raise InvalidValidatorException.new(validator_name)
16
17
  end
17
18
  end
18
19
  end
20
+ if errors.count > 0
21
+ exception = InvalidParamsException.new
22
+ exception.errors = errors
23
+ raise exception
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -1,6 +1,3 @@
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'
1
+ require 'params_validator/validator/type'
2
+ require 'params_validator/validator/presence'
6
3
 
@@ -1,8 +1,10 @@
1
1
  module ParamsValidator
2
2
  module Validator
3
- module TypeArray
3
+ module Presence
4
+ def self.error_message; "is empty"; end
5
+
4
6
  def self.valid?(value)
5
- value.kind_of? Array
7
+ value.present?
6
8
  end
7
9
  end
8
10
  end
@@ -0,0 +1,64 @@
1
+ module ParamsValidator
2
+ module Validator
3
+ module TypeInteger
4
+ def self.error_message; "is not of type integer"; end
5
+
6
+ def self.valid?(value)
7
+ return true unless Presence.valid?(value)
8
+
9
+ begin
10
+ Integer(value)
11
+ true
12
+ rescue
13
+ false
14
+ end
15
+ end
16
+ end
17
+
18
+ module TypeFloat
19
+ def self.error_message; "is not of type float"; end
20
+
21
+ def self.valid?(value)
22
+ return true unless Presence.valid?(value)
23
+
24
+ begin
25
+ Float(value)
26
+ true
27
+ rescue
28
+ false
29
+ end
30
+ end
31
+ end
32
+
33
+ module TypeString
34
+ def self.error_message; "is not of type string"; end
35
+
36
+ def self.valid?(value)
37
+ return true unless Presence.valid?(value)
38
+
39
+ value.kind_of? String
40
+ end
41
+ end
42
+
43
+ module TypeArray
44
+ def self.error_message; "is not of type array"; end
45
+
46
+ def self.valid?(value)
47
+ return true unless Presence.valid?(value)
48
+
49
+ value.kind_of? Array
50
+ end
51
+ end
52
+
53
+ module TypeHash
54
+ def self.error_message; "is not of type hash"; end
55
+
56
+ def self.valid?(value)
57
+ return true unless Presence.valid?(value)
58
+
59
+ value.kind_of? Hash
60
+ end
61
+ end
62
+ end
63
+ end
64
+
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.1.0
4
+ version: 0.1.1
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: 2012-04-15 00:00:00.000000000 Z
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70160681777260 !ruby/object:Gem::Requirement
16
+ requirement: &70244496983080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,21 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70160681777260
24
+ version_requirements: *70244496983080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70160681793780 !ruby/object:Gem::Requirement
27
+ requirement: &70244496981740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 2.8.0
32
+ version: 2.9.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70160681793780
35
+ version_requirements: *70244496981740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: yard
38
- requirement: &70160681789960 !ruby/object:Gem::Requirement
38
+ requirement: &70244496980260 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.7'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70160681789960
46
+ version_requirements: *70244496980260
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &70160681812280 !ruby/object:Gem::Requirement
49
+ requirement: &70244496978500 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '3.12'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70160681812280
57
+ version_requirements: *70244496978500
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &70160681810740 !ruby/object:Gem::Requirement
60
+ requirement: &70244496977340 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70160681810740
68
+ version_requirements: *70244496977340
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &70160681822060 !ruby/object:Gem::Requirement
71
+ requirement: &70244496976340 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.8.3
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70160681822060
79
+ version_requirements: *70244496976340
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard
82
- requirement: &70160681819620 !ruby/object:Gem::Requirement
82
+ requirement: &70244496975260 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70160681819620
90
+ version_requirements: *70244496975260
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard-rspec
93
- requirement: &70160681830320 !ruby/object:Gem::Requirement
93
+ requirement: &70244497000340 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70160681830320
101
+ version_requirements: *70244497000340
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rb-fsevent
104
- requirement: &70160681826500 !ruby/object:Gem::Requirement
104
+ requirement: &70244496998380 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70160681826500
112
+ version_requirements: *70244496998380
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: growl
115
- requirement: &70160681823660 !ruby/object:Gem::Requirement
115
+ requirement: &70244496996880 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70160681823660
123
+ version_requirements: *70244496996880
124
124
  description:
125
125
  email: christof@chdorner.me
126
126
  executables: []
@@ -134,11 +134,8 @@ files:
134
134
  - lib/params_validator/filter.rb
135
135
  - lib/params_validator/railtie.rb
136
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
137
+ - lib/params_validator/validator/presence.rb
138
+ - lib/params_validator/validator/type.rb
142
139
  - LICENSE.txt
143
140
  - README.md
144
141
  homepage: https://github.com/chdorner/params_validator
@@ -156,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
153
  version: '0'
157
154
  segments:
158
155
  - 0
159
- hash: -4216775152249830731
156
+ hash: 1545211467612620897
160
157
  required_rubygems_version: !ruby/object:Gem::Requirement
161
158
  none: false
162
159
  requirements:
@@ -1,15 +0,0 @@
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
-
@@ -1,10 +0,0 @@
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
-
@@ -1,15 +0,0 @@
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
-
@@ -1,10 +0,0 @@
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
-