params_validator 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -19,7 +19,7 @@ Basic usage:
19
19
  render :text => 'Error', :status => :bad_request
20
20
  end
21
21
 
22
- validate_params_for :index, { :count => { :with => [:type_integer] } }
22
+ validate_params_for :index, { :count => { :_with => [:type_integer] } }
23
23
  def index
24
24
  @articles = Article.limit(params[:count])
25
25
  end
@@ -29,7 +29,7 @@ Basic usage:
29
29
 
30
30
  ### TypeInteger
31
31
 
32
- validate_params_for :index, { :count => { :with => [:type_integer] } }
32
+ validate_params_for :index, { :count => { :_with => [:type_integer] } }
33
33
 
34
34
  **Valid:**
35
35
 
@@ -43,7 +43,7 @@ Basic usage:
43
43
 
44
44
  ### TypeFloat
45
45
 
46
- validate_params_for :index, { :position => { :with => [:type_float] } }
46
+ validate_params_for :index, { :position => { :_with => [:type_float] } }
47
47
 
48
48
  **Valid:**
49
49
 
@@ -58,7 +58,7 @@ Basic usage:
58
58
 
59
59
  ### TypeString
60
60
 
61
- validate_params_for :index, { :name => { :with => [:type_string] } }
61
+ validate_params_for :index, { :name => { :_with => [:type_string] } }
62
62
 
63
63
  **Valid:**
64
64
 
@@ -72,7 +72,7 @@ Basic usage:
72
72
 
73
73
  ### TypeHash
74
74
 
75
- validate_params_for :index, { :options => { :with => [:type_hash] } }
75
+ validate_params_for :index, { :options => { :_with => [:type_hash] } }
76
76
 
77
77
  **Valid:**
78
78
 
@@ -85,7 +85,7 @@ Basic usage:
85
85
 
86
86
  ### TypeArray
87
87
 
88
- validate_params_for :index, { :ids => { :with => [:type_array] } }
88
+ validate_params_for :index, { :ids => { :_with => [:type_array] } }
89
89
 
90
90
  **Valid:**
91
91
 
@@ -98,7 +98,7 @@ Basic usage:
98
98
 
99
99
  ### Presence
100
100
 
101
- validate_params_for :search, { :q => { :with => [:presence] } }
101
+ validate_params_for :search, { :q => { :_with => [:presence] } }
102
102
 
103
103
  **Valid:**
104
104
 
@@ -5,16 +5,10 @@ module ParamsValidator
5
5
  def self.validate_params(params, definition)
6
6
  errors = {}
7
7
  definition.each do |field, validation_definition|
8
- validation_definition[:with].each do |validator_name|
9
- camelized_validator_name = self.camelize(validator_name)
10
- begin
11
- validator = constantize("ParamsValidator::Validator::#{camelized_validator_name}")
12
- unless validator.valid?(params[field.to_s])
13
- errors[field] = validator.error_message
14
- end
15
- rescue NameError
16
- raise InvalidValidatorException.new(validator_name)
17
- end
8
+ errors = validate_field(field, params, validation_definition, errors)
9
+
10
+ validation_definition.reject {|k,v| k == :_with }.each do |nested_field, nested_validation_definition|
11
+ errors = validate_field(nested_field, params[field.to_s], nested_validation_definition, errors)
18
12
  end
19
13
  end
20
14
  if errors.count > 0
@@ -23,6 +17,25 @@ module ParamsValidator
23
17
  raise exception
24
18
  end
25
19
  end
20
+
21
+ private
22
+
23
+ def self.validate_field(field, params, validation_definition, errors)
24
+ return errors unless validation_definition.has_key? :_with
25
+ validation_definition[:_with].each do |validator_name|
26
+ camelized_validator_name = self.camelize(validator_name)
27
+ begin
28
+ validator = constantize("ParamsValidator::Validator::#{camelized_validator_name}")
29
+ value = params.is_a?(Hash) ? params[field.to_s] : nil
30
+ unless validator.valid?(value)
31
+ errors[field] = validator.error_message
32
+ end
33
+ rescue NameError
34
+ raise InvalidValidatorException.new(validator_name)
35
+ end
36
+ end
37
+ errors
38
+ end
26
39
  end
27
40
  end
28
41
 
@@ -1,63 +1,50 @@
1
1
  module ParamsValidator
2
2
  module Validator
3
- module TypeInteger
4
- def self.error_message; "is not of type integer"; end
5
3
 
6
- def self.valid?(value)
7
- return true unless Presence.valid?(value)
4
+ private
5
+
6
+ module Base
7
+ private
8
+ def define_type_validator(type_name, &block)
9
+ @type_name = type_name
10
+ @block = block
8
11
 
9
- begin
10
- Integer(value)
11
- true
12
- rescue
13
- false
12
+ def self.error_message
13
+ "is not of type #{@type_name.to_s.downcase}"
14
+ end
15
+
16
+ def self.valid?(value)
17
+ return true unless Presence.valid?(value)
18
+ return !!@block.call(value) rescue false
14
19
  end
15
20
  end
16
21
  end
17
22
 
18
- module TypeFloat
19
- def self.error_message; "is not of type float"; end
23
+ public
20
24
 
21
- def self.valid?(value)
22
- return true unless Presence.valid?(value)
25
+ module TypeInteger
26
+ extend Base
27
+ define_type_validator('integer') { |value| Integer(value) }
28
+ end
23
29
 
24
- begin
25
- Float(value)
26
- true
27
- rescue
28
- false
29
- end
30
- end
30
+ module TypeFloat
31
+ extend Base
32
+ define_type_validator('float') { |value| Float(value) }
31
33
  end
32
34
 
33
35
  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
36
+ extend Base
37
+ define_type_validator('string') { |value| value.kind_of? String }
41
38
  end
42
39
 
43
40
  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
41
+ extend Base
42
+ define_type_validator('array') { |value| value.kind_of? Array }
51
43
  end
52
44
 
53
45
  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
46
+ extend Base
47
+ define_type_validator('hash') { |value| value.kind_of? Hash }
61
48
  end
62
49
  end
63
50
  end
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.1
4
+ version: 0.2.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: 2012-04-17 00:00:00.000000000 Z
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70244496983080 !ruby/object:Gem::Requirement
16
+ requirement: &70160878293820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70244496983080
24
+ version_requirements: *70160878293820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70244496981740 !ruby/object:Gem::Requirement
27
+ requirement: &70160878293300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.9.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70244496981740
35
+ version_requirements: *70160878293300
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: yard
38
- requirement: &70244496980260 !ruby/object:Gem::Requirement
38
+ requirement: &70160878292800 !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: *70244496980260
46
+ version_requirements: *70160878292800
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &70244496978500 !ruby/object:Gem::Requirement
49
+ requirement: &70160878292320 !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: *70244496978500
57
+ version_requirements: *70160878292320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &70244496977340 !ruby/object:Gem::Requirement
60
+ requirement: &70160878291840 !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: *70244496977340
68
+ version_requirements: *70160878291840
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &70244496976340 !ruby/object:Gem::Requirement
71
+ requirement: &70160878291320 !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: *70244496976340
79
+ version_requirements: *70160878291320
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard
82
- requirement: &70244496975260 !ruby/object:Gem::Requirement
82
+ requirement: &70160878290780 !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: *70244496975260
90
+ version_requirements: *70160878290780
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard-rspec
93
- requirement: &70244497000340 !ruby/object:Gem::Requirement
93
+ requirement: &70160878290240 !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: *70244497000340
101
+ version_requirements: *70160878290240
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rb-fsevent
104
- requirement: &70244496998380 !ruby/object:Gem::Requirement
104
+ requirement: &70160878289720 !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: *70244496998380
112
+ version_requirements: *70160878289720
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: growl
115
- requirement: &70244496996880 !ruby/object:Gem::Requirement
115
+ requirement: &70160878305580 !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: *70244496996880
123
+ version_requirements: *70160878305580
124
124
  description:
125
125
  email: christof@chdorner.me
126
126
  executables: []
@@ -153,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  segments:
155
155
  - 0
156
- hash: 1545211467612620897
156
+ hash: 464833905745855690
157
157
  required_rubygems_version: !ruby/object:Gem::Requirement
158
158
  none: false
159
159
  requirements: