valideizer 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0bd366477b47fe01040eeda2773d34cfe36b36e9eebb56582993583c261025b
4
- data.tar.gz: 2384ce4ca9f2747158d57efae38648c1ae2d6852368f836b698734c89bcddfea
3
+ metadata.gz: 7ef60decc451d279d677b03f23f19c08949373e402172b287233ce4a465dcb3a
4
+ data.tar.gz: cf07107455d7494378b5a6fbf10f7ad34a6a8b6e10e7d7411ab39fe164e0e740
5
5
  SHA512:
6
- metadata.gz: aa229fac2d83b9932a0215ef162bb40ae122e654dd13fb926ea8a9a147f59715de933347739d30fc329ab4dc62682573f86aad18f542ac7c17963598bc2cb0d9
7
- data.tar.gz: 55cad297da865296d710fdec62d74137770151d1e3d0058c4bae52df9a197eab14a478ed03651b2acdc9d51e93b9834d4f6c7febeee81c3d842c872e9e255be9
6
+ metadata.gz: 0c0747d2d6c23106d0de6eb6170ac79b2b0af1ba17a5219595cb8511406654496774fd8fb4b454342176c13662b3d71801c3b3439897cebce11c38630a55a90a
7
+ data.tar.gz: 56158376a80e583dfb06a47e938ebc829862bb9cbf3afef85f134387260c9f0421013967c4968fd4fade78bd84da68a87e215eeb32a66e331715aed36300ad79
@@ -0,0 +1,20 @@
1
+ module Valideizer
2
+ module Caster
3
+ def cast_from_json(value)
4
+ JSON.parse(value) rescue nil
5
+ end
6
+
7
+ def cast_to_integer(value)
8
+ value.to_i rescue nil
9
+ end
10
+
11
+ def cast_to_float(value)
12
+ value.to_f rescue nil
13
+ end
14
+
15
+ def cast_to_bool(value)
16
+ return false if ['0', 'false'].include? value.to_s.downcase
17
+ return true if ['1', 'true'].include? value.to_s.downcase
18
+ end
19
+ end
20
+ end
@@ -1,9 +1,11 @@
1
1
  require 'valideizer/rules'
2
2
  require 'valideizer/error_printer'
3
+ require 'valideizer/caster'
3
4
 
4
5
  module Valideizer
5
6
  class Core
6
7
  include Valideizer::Rules
8
+ include Valideizer::Caster
7
9
  include Valideizer::ErrorPrinter
8
10
 
9
11
  def initialize
@@ -20,6 +22,7 @@ module Valideizer
20
22
  def valideized?(params)
21
23
  reinit_errrors
22
24
  setup_defaults(params)
25
+
23
26
  params.each do |param, value|
24
27
  next unless nil_check(param, value)
25
28
  @rules[param.to_s].each do |type, constraint|
@@ -32,7 +35,7 @@ module Valideizer
32
35
  end
33
36
 
34
37
  if @errors.empty?
35
- type_cast params if @type_cast
38
+ recast(params)
36
39
  true
37
40
  else
38
41
  false
@@ -87,6 +90,30 @@ module Valideizer
87
90
  nil_rule.nil? ? false : nil_rule.last
88
91
  end
89
92
 
93
+ def recast(params)
94
+ @rules.each do |param, rules|
95
+ next if params[param.to_s].nil? && params[param.to_sym].nil?
96
+
97
+ type_rule = rules.find { |r, _c| r == :type }
98
+
99
+ if type_rule
100
+ constraint = type_rule.last
101
+ param = param.to_sym if params.include? param.to_sym
102
+ value = params[param]
103
+
104
+ if validate(value, :type, constraint)
105
+ params[param] = case constraint
106
+ when :json then cast_from_json value
107
+ when :bool then cast_to_bool value
108
+ when :float then cast_to_float value
109
+ when :integer then cast_to_integer value
110
+ else value
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
90
117
  def build_error_messages
91
118
  @error_messages = print_errors
92
119
  end
@@ -20,7 +20,7 @@ module Valideizer
20
20
  when :ot then "Should be other than #{constraint}."
21
21
  when :range then "Out of range. Should be in #{constraint.to_s} range."
22
22
  when :enum then "Out of enum. Possible values #{constraint.to_s}."
23
- when :type then "Should be #{constraint} type. Current type: `#{value}`."
23
+ when :type then "Should be #{constraint} type. Current type: `#{value.class}`."
24
24
  when :array_type then "Should be array of #{constraint} type."
25
25
  when :custom_type then "Should be #{constraint} type."
26
26
  when :regexp then "Couldn't be matched by #{constraint}."
@@ -21,23 +21,23 @@ module Valideizer
21
21
  default
22
22
  ]
23
23
 
24
- def validate(param, rule, constraint)
24
+ def validate(value, rule, constraint)
25
25
  begin
26
26
  case rule
27
- when :eql then validate_eql param, constraint
28
- when :gt then validate_gt param, constraint
29
- when :gte then validate_gte param, constraint
30
- when :lt then validate_lt param, constraint
31
- when :lte then validate_lte param, constraint
32
- when :ot then validate_ot param, constraint
33
- when :range then validate_range param, constraint
34
- when :enum then validate_enum param, constraint
35
- when :type then validate_type param, constraint
36
- when :array_type then validate_array_type param, constraint
37
- when :custom_type then validate_custom_type param, constraint
38
- when :regexp then validate_regexp param, constraint
39
- when :length then validate_length param, constraint
40
- when :active_record then validate_active_record param, constraint
27
+ when :eql then validate_eql value, constraint
28
+ when :gt then validate_gt value, constraint
29
+ when :gte then validate_gte value, constraint
30
+ when :lt then validate_lt value, constraint
31
+ when :lte then validate_lte value, constraint
32
+ when :ot then validate_ot value, constraint
33
+ when :range then validate_range value, constraint
34
+ when :enum then validate_enum value, constraint
35
+ when :type then validate_type value, constraint
36
+ when :array_type then validate_array_type value, constraint
37
+ when :custom_type then validate_custom_type value, constraint
38
+ when :regexp then validate_regexp value, constraint
39
+ when :length then validate_length value, constraint
40
+ when :active_record then validate_active_record value, constraint
41
41
  else true
42
42
  end
43
43
  rescue
@@ -47,72 +47,90 @@ module Valideizer
47
47
 
48
48
  private
49
49
 
50
- def validate_eql(param, constraint)
51
- param == constraint
50
+ def validate_eql(value, constraint)
51
+ value == constraint
52
52
  end
53
53
 
54
- def validate_gt(param, constraint)
55
- param > constraint
54
+ def validate_gt(value, constraint)
55
+ value > constraint
56
56
  end
57
57
 
58
- def validate_gte(param, constraint)
59
- param >= constraint
58
+ def validate_gte(value, constraint)
59
+ value >= constraint
60
60
  end
61
61
 
62
- def validate_lt(param, constraint)
63
- param < constraint
62
+ def validate_lt(value, constraint)
63
+ value < constraint
64
64
  end
65
65
 
66
- def validate_lte(param, constraint)
67
- param <= constraint
66
+ def validate_lte(value, constraint)
67
+ value <= constraint
68
68
  end
69
69
 
70
- def validate_ot(param, constraint)
71
- param != constraint
70
+ def validate_ot(value, constraint)
71
+ value != constraint
72
72
  end
73
73
 
74
- def validate_range(param, constraint)
74
+ def validate_range(value, constraint)
75
75
  raise 'Must be a range' unless constraint.is_a? Range
76
- constraint.include? param
76
+ constraint.include? value
77
77
  end
78
78
 
79
- def validate_enum(param, constraint)
79
+ def validate_enum(value, constraint)
80
80
  raise 'Must be an array' unless constraint.is_a? Array
81
- constraint.include? param
81
+ constraint.include? value
82
82
  end
83
83
 
84
- def validate_type(param, constraint)
84
+ def validate_type(value, constraint)
85
85
  if constraint.is_a? Array
86
- constraint.each { |type| return true if type_check(param, type)}
86
+ constraint.each { |type| return true if type_check(value, type)}
87
87
  else
88
- type_check(param, constraint)
88
+ type_check(value, constraint)
89
89
  end
90
90
  end
91
91
 
92
- def type_check(param, type)
92
+ def type_check(value, type)
93
93
  case type
94
- when :integer then param.is_a? Integer
95
- when :float then param.is_a? Float
96
- when :string then param.is_a? String
97
- when :array then param.is_a? Array
98
- when :hash then param.is_a? Hash
99
- when :bool then bool_check(param)
100
- when :json then json_check(param)
101
- else raise "Wrong check type #{param}"
94
+ when :string then value.is_a? String
95
+ when :array then value.is_a? Array
96
+ when :hash then value.is_a? Hash
97
+ when :integer then integer_check(value)
98
+ when :float then float_check(value)
99
+ when :bool then bool_check(value)
100
+ when :json then json_check(value)
101
+ else raise "Wrong check type #{value}"
102
102
  end
103
103
  end
104
104
 
105
- def bool_check(param)
106
- [0, 1].include?(param) || ['true', 'false'].include?(param.to_s.downcase)
105
+ def integer_check(value)
106
+ casted_value = value.to_i rescue nil
107
+ if casted_value && (casted_value == 0 && value.to_s.strip == '0' || casted_value != 0)
108
+ true
109
+ else
110
+ false
111
+ end
112
+ end
113
+
114
+ def float_check(value)
115
+ casted_value = value.to_f rescue nil
116
+ if casted_value && (casted_value == 0 && value.to_s.strip == '0' || casted_value != 0)
117
+ true
118
+ else
119
+ false
120
+ end
121
+ end
122
+
123
+ def bool_check(value)
124
+ ['0', '1'].include?(value.to_s.strip) || ['true', 'false'].include?(value.to_s.downcase.strip)
107
125
  end
108
126
 
109
- def json_check(param)
110
- [Hash, Array].include?((JSON.parse param rescue nil).class)
127
+ def json_check(value)
128
+ [Hash, Array].include?((JSON.parse value rescue nil).class)
111
129
  end
112
130
 
113
- def validate_array_type(param, constraint)
114
- if param.is_a? Array
115
- param.each do |v|
131
+ def validate_array_type(value, constraint)
132
+ if value.is_a? Array
133
+ value.each do |v|
116
134
  if v.is_a?(Array)
117
135
  validate_array_type(v, constraint)
118
136
  else
@@ -126,25 +144,25 @@ module Valideizer
126
144
  end
127
145
  end
128
146
 
129
- def validate_regexp(param, regexp)
130
- raise 'Must be a string' unless param.is_a? String
131
- param.match? regexp
147
+ def validate_regexp(value, regexp)
148
+ raise 'Must be a string' unless value.is_a? String
149
+ value.match? regexp
132
150
  end
133
151
 
134
- def validate_custom_type(param, constraint)
135
- param.is_a? constraint
152
+ def validate_custom_type(value, constraint)
153
+ value.is_a? constraint
136
154
  end
137
155
 
138
- def validate_length(param, constraint)
139
- if [Array, Hash, String].include? param.class
156
+ def validate_length(value, constraint)
157
+ if [Array, Hash, String].include? value.class
140
158
  if constraint.is_a? Hash
141
159
  raise 'Hash params can not be empty.' if constraint.empty?
142
160
  res = true
143
- res &= param.length >= constraint[:min] unless constraint[:min].nil?
144
- res &= param.length <= constraint[:max] unless constraint[:max].nil?
161
+ res &= value.length >= constraint[:min] unless constraint[:min].nil?
162
+ res &= value.length <= constraint[:max] unless constraint[:max].nil?
145
163
  res
146
164
  elsif constraint.is_a? Range
147
- constraint.include? param.length
165
+ constraint.include? value.length
148
166
  else
149
167
  raise 'Wrong constraint for :length option. Must be range or hash {min: 0, max: 10}'
150
168
  end
@@ -153,10 +171,10 @@ module Valideizer
153
171
  end
154
172
  end
155
173
 
156
- def validate_active_record(param, constraint)
174
+ def validate_active_record(value, constraint)
157
175
  klass = constraint
158
176
  if klass.is_a? ActiveModel || (klass = constraint.to_s.constantize).is_a?(ActiveModel)
159
- klass.find_by_id(param).present?
177
+ klass.find_by_id(value).present?
160
178
  else
161
179
  raise "#{constraint} is not a valid ActiveRecord model"
162
180
  end
@@ -1,3 +1,3 @@
1
1
  module Valideizer
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
data/lib/valideizer.rb CHANGED
@@ -4,6 +4,7 @@ require_relative 'valideizer/rails.rb'
4
4
  require_relative 'valideizer/version.rb'
5
5
  require_relative 'valideizer/error_printer.rb'
6
6
  require_relative 'valideizer/holder.rb'
7
+ require_relative 'valideizer/caster.rb'
7
8
 
8
9
  module Valideizer; end
9
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valideizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur 'ArtK0DE' Korochansky
@@ -32,6 +32,7 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - lib/valideizer.rb
35
+ - lib/valideizer/caster.rb
35
36
  - lib/valideizer/core.rb
36
37
  - lib/valideizer/error_printer.rb
37
38
  - lib/valideizer/holder.rb