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 +4 -4
- data/lib/valideizer/caster.rb +20 -0
- data/lib/valideizer/core.rb +28 -1
- data/lib/valideizer/error_printer.rb +1 -1
- data/lib/valideizer/rules.rb +80 -62
- data/lib/valideizer/version.rb +1 -1
- data/lib/valideizer.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ef60decc451d279d677b03f23f19c08949373e402172b287233ce4a465dcb3a
|
4
|
+
data.tar.gz: cf07107455d7494378b5a6fbf10f7ad34a6a8b6e10e7d7411ab39fe164e0e740
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/valideizer/core.rb
CHANGED
@@ -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
|
-
|
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}."
|
data/lib/valideizer/rules.rb
CHANGED
@@ -21,23 +21,23 @@ module Valideizer
|
|
21
21
|
default
|
22
22
|
]
|
23
23
|
|
24
|
-
def validate(
|
24
|
+
def validate(value, rule, constraint)
|
25
25
|
begin
|
26
26
|
case rule
|
27
|
-
when :eql then validate_eql
|
28
|
-
when :gt then validate_gt
|
29
|
-
when :gte then validate_gte
|
30
|
-
when :lt then validate_lt
|
31
|
-
when :lte then validate_lte
|
32
|
-
when :ot then validate_ot
|
33
|
-
when :range then validate_range
|
34
|
-
when :enum then validate_enum
|
35
|
-
when :type then validate_type
|
36
|
-
when :array_type then validate_array_type
|
37
|
-
when :custom_type then validate_custom_type
|
38
|
-
when :regexp then validate_regexp
|
39
|
-
when :length then validate_length
|
40
|
-
when :active_record then validate_active_record
|
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(
|
51
|
-
|
50
|
+
def validate_eql(value, constraint)
|
51
|
+
value == constraint
|
52
52
|
end
|
53
53
|
|
54
|
-
def validate_gt(
|
55
|
-
|
54
|
+
def validate_gt(value, constraint)
|
55
|
+
value > constraint
|
56
56
|
end
|
57
57
|
|
58
|
-
def validate_gte(
|
59
|
-
|
58
|
+
def validate_gte(value, constraint)
|
59
|
+
value >= constraint
|
60
60
|
end
|
61
61
|
|
62
|
-
def validate_lt(
|
63
|
-
|
62
|
+
def validate_lt(value, constraint)
|
63
|
+
value < constraint
|
64
64
|
end
|
65
65
|
|
66
|
-
def validate_lte(
|
67
|
-
|
66
|
+
def validate_lte(value, constraint)
|
67
|
+
value <= constraint
|
68
68
|
end
|
69
69
|
|
70
|
-
def validate_ot(
|
71
|
-
|
70
|
+
def validate_ot(value, constraint)
|
71
|
+
value != constraint
|
72
72
|
end
|
73
73
|
|
74
|
-
def validate_range(
|
74
|
+
def validate_range(value, constraint)
|
75
75
|
raise 'Must be a range' unless constraint.is_a? Range
|
76
|
-
constraint.include?
|
76
|
+
constraint.include? value
|
77
77
|
end
|
78
78
|
|
79
|
-
def validate_enum(
|
79
|
+
def validate_enum(value, constraint)
|
80
80
|
raise 'Must be an array' unless constraint.is_a? Array
|
81
|
-
constraint.include?
|
81
|
+
constraint.include? value
|
82
82
|
end
|
83
83
|
|
84
|
-
def validate_type(
|
84
|
+
def validate_type(value, constraint)
|
85
85
|
if constraint.is_a? Array
|
86
|
-
constraint.each { |type| return true if type_check(
|
86
|
+
constraint.each { |type| return true if type_check(value, type)}
|
87
87
|
else
|
88
|
-
type_check(
|
88
|
+
type_check(value, constraint)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
def type_check(
|
92
|
+
def type_check(value, type)
|
93
93
|
case type
|
94
|
-
when :
|
95
|
-
when :
|
96
|
-
when :
|
97
|
-
when :
|
98
|
-
when :
|
99
|
-
when :bool then bool_check(
|
100
|
-
when :json then json_check(
|
101
|
-
else raise "Wrong check type #{
|
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
|
106
|
-
|
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(
|
110
|
-
[Hash, Array].include?((JSON.parse
|
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(
|
114
|
-
if
|
115
|
-
|
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(
|
130
|
-
raise 'Must be a string' unless
|
131
|
-
|
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(
|
135
|
-
|
152
|
+
def validate_custom_type(value, constraint)
|
153
|
+
value.is_a? constraint
|
136
154
|
end
|
137
155
|
|
138
|
-
def validate_length(
|
139
|
-
if [Array, Hash, String].include?
|
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 &=
|
144
|
-
res &=
|
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?
|
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(
|
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(
|
177
|
+
klass.find_by_id(value).present?
|
160
178
|
else
|
161
179
|
raise "#{constraint} is not a valid ActiveRecord model"
|
162
180
|
end
|
data/lib/valideizer/version.rb
CHANGED
data/lib/valideizer.rb
CHANGED
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.
|
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
|