valideizer 1.1.9 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/valideizer/caster.rb +33 -0
- data/lib/valideizer/core.rb +41 -21
- data/lib/valideizer/error_printer.rb +2 -1
- data/lib/valideizer/holder.rb +13 -0
- data/lib/valideizer/rails.rb +49 -19
- data/lib/valideizer/rules.rb +21 -27
- data/lib/valideizer/rules_checker.rb +90 -0
- data/lib/valideizer/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af9eb189f03b2ee23f2f688fc05ba140828d27c994b8e09c9ab3c148a2510cce
|
4
|
+
data.tar.gz: 27197cedb634dcf3a3dcb95599270c7ce7764507f55f147c8ee9921b9fb9d063
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49debeda89e3f5f1a538b0ea67d2b9dfb27a725c67069a012e8e2d1a52d8119cbae42b6bffb776665d029c03c6ca4a5cd58f678175213d767febedf987da0ff9
|
7
|
+
data.tar.gz: 19b27f84237bf5baae8319d427b686504b7933aa6d76f40f2696f828bc6f6693bb2e1c105916506e0e47a5dd8ba2d4283a1099daed972e1e594cb8f361bc73a0
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module Valideizer
|
4
|
+
module Caster
|
5
|
+
def cast_from_json(value)
|
6
|
+
JSON.parse(value) rescue nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def cast_to_integer(value)
|
10
|
+
value.to_i rescue nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def cast_to_float(value)
|
14
|
+
value.to_f rescue nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def cast_to_time(value)
|
18
|
+
Time.parse(value) rescue nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def cast_to_time_with_format(value, format)
|
22
|
+
Time.strptime(value, format) rescue nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def cast_to_boolean(value)
|
26
|
+
if %w(1 true).include?(value.to_s.downcase)
|
27
|
+
true
|
28
|
+
elsif %w(0 false).include?(value.to_s.downcase)
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/valideizer/core.rb
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
require 'valideizer/rules'
|
2
2
|
require 'valideizer/error_printer'
|
3
3
|
require 'valideizer/caster'
|
4
|
+
require 'valideizer/rules_checker'
|
4
5
|
|
5
6
|
module Valideizer
|
6
7
|
class Core
|
7
8
|
include Valideizer::Rules
|
9
|
+
include Valideizer::RulesChecker
|
8
10
|
include Valideizer::Caster
|
9
11
|
include Valideizer::ErrorPrinter
|
10
12
|
|
11
|
-
|
13
|
+
attr_accessor :rules
|
14
|
+
|
15
|
+
def initialize(autocast: true)
|
16
|
+
@autocast = autocast
|
17
|
+
@rules = {}
|
18
|
+
reinit_errrors
|
19
|
+
end
|
20
|
+
|
21
|
+
# Cleanes rules and errors
|
22
|
+
def clean!
|
12
23
|
@rules = {}
|
13
24
|
reinit_errrors
|
14
25
|
end
|
15
26
|
|
27
|
+
# Adds new rule for validation
|
16
28
|
def add_rule(param, *rules)
|
29
|
+
check_rule_set(rules[0])
|
17
30
|
@rules[param.to_s] = rules[0]
|
18
31
|
end
|
19
32
|
|
20
33
|
alias valideize add_rule
|
21
34
|
|
35
|
+
# Validates and recasts params
|
22
36
|
def valideized?(params)
|
23
37
|
pre_process params
|
24
38
|
|
25
39
|
params.each do |param, value|
|
26
|
-
next unless
|
40
|
+
next unless null_check(param, value)
|
27
41
|
@rules[param.to_s].each do |type, constraint|
|
28
42
|
begin
|
29
43
|
push_error(param, value, type, constraint) unless validate(value, type, constraint)
|
@@ -41,6 +55,7 @@ module Valideizer
|
|
41
55
|
end
|
42
56
|
end
|
43
57
|
|
58
|
+
# Prints error messages
|
44
59
|
def errors
|
45
60
|
build_error_messages
|
46
61
|
@error_messages
|
@@ -80,17 +95,17 @@ module Valideizer
|
|
80
95
|
end
|
81
96
|
end
|
82
97
|
|
83
|
-
def
|
84
|
-
if !value.nil? || value.nil? &&
|
98
|
+
def null_check(param, value)
|
99
|
+
if !value.nil? || value.nil? && has_allow_null_rule(param)
|
85
100
|
true
|
86
101
|
else
|
87
|
-
push_error(param, :
|
102
|
+
push_error(param, :null, nil, nil)
|
88
103
|
false
|
89
104
|
end
|
90
105
|
end
|
91
106
|
|
92
|
-
def
|
93
|
-
nil_rule = @rules[param.to_s]&.find { |r, _c| r == :
|
107
|
+
def has_allow_null_rule(param)
|
108
|
+
nil_rule = @rules[param.to_s]&.find { |r, _c| r == :null }
|
94
109
|
nil_rule.nil? ? false : nil_rule.last
|
95
110
|
end
|
96
111
|
|
@@ -102,36 +117,41 @@ module Valideizer
|
|
102
117
|
type_rule = rules.find { |r, _c| r == :type }
|
103
118
|
recast(params, key, type_rule.last) if type_rule
|
104
119
|
|
120
|
+
datetime_rule = rules.find { |r, _c| r == :format }
|
121
|
+
params[key] = cast_to_time_with_format(params[key],
|
122
|
+
datetime_rule.last) if datetime_rule
|
123
|
+
|
105
124
|
regexp_rule = rules.find { |r, _c| r == :regexp }
|
106
125
|
regexp_groups_substitution(params, key, regexp_rule.last) if regexp_rule
|
107
126
|
end
|
108
127
|
end
|
109
128
|
|
110
|
-
|
111
129
|
def recast(params, key, type)
|
112
130
|
value = params[key]
|
113
131
|
params[key] = case type
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
132
|
+
when :json then cast_from_json value
|
133
|
+
when :boolean then cast_to_boolean value
|
134
|
+
when :float then cast_to_float value
|
135
|
+
when :integer then cast_to_integer value
|
136
|
+
when :datetime then cast_to_time value
|
137
|
+
else value
|
138
|
+
end
|
120
139
|
end
|
121
140
|
|
122
141
|
def regexp_groups_substitution(params, key, regexp)
|
123
142
|
value = params[key]
|
124
|
-
|
125
143
|
matched = value.match regexp
|
126
144
|
return if matched.nil?
|
127
145
|
|
128
146
|
params[key] = if matched.named_captures.any?
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
147
|
+
matched.named_captures
|
148
|
+
elsif matched.captures.count > 1
|
149
|
+
matched.captures
|
150
|
+
elsif matched.captures.count == 1
|
151
|
+
matched.captures[0]
|
152
|
+
else
|
153
|
+
value
|
154
|
+
end
|
135
155
|
end
|
136
156
|
|
137
157
|
def build_error_messages
|
@@ -26,7 +26,8 @@ module Valideizer
|
|
26
26
|
when :regexp then "Couldn't be matched by #{constraint}."
|
27
27
|
when :length then "Length must be #{constraint}. Current value: `#{value}`, length: #{value.length}."
|
28
28
|
when :active_record then "Couldn't find #{constraint} with ID=#{value}."
|
29
|
-
when :
|
29
|
+
when :null then "Can not be nil or empty."
|
30
|
+
when :format then "Don't match current time pattern #{constraint}"
|
30
31
|
else ''
|
31
32
|
end
|
32
33
|
end
|
data/lib/valideizer/holder.rb
CHANGED
@@ -4,12 +4,25 @@ module Valideizer
|
|
4
4
|
class Holder
|
5
5
|
include Singleton
|
6
6
|
|
7
|
+
CALLBACK_TYPE = 'callback'.freeze
|
8
|
+
METHOD_TYPE = 'render_method'.freeze
|
9
|
+
RENDER_TYPE = 'render_block'.freeze
|
10
|
+
|
7
11
|
attr_accessor :valideizers
|
8
12
|
attr_accessor :callback_method
|
9
13
|
attr_accessor :callback_controller
|
14
|
+
attr_accessor :render_method
|
15
|
+
attr_accessor :render_controller
|
16
|
+
attr_accessor :render_block
|
17
|
+
attr_accessor :output_type
|
18
|
+
attr_accessor :status
|
10
19
|
|
11
20
|
def initialize
|
12
21
|
@valideizers = {}
|
13
22
|
end
|
23
|
+
|
24
|
+
def raise_exception
|
25
|
+
raise "You've already defined #{@output_type}"
|
26
|
+
end
|
14
27
|
end
|
15
28
|
end
|
data/lib/valideizer/rails.rb
CHANGED
@@ -10,26 +10,50 @@ module Valideizer
|
|
10
10
|
base.before_action :valideize!
|
11
11
|
|
12
12
|
base.class_eval do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
class << self
|
14
|
+
def valideize(*methods, &options_block)
|
15
|
+
methods.each do |method|
|
16
|
+
method = method.to_s
|
17
|
+
holder = Valideizer::Holder.instance
|
18
|
+
holder.valideizers[controller_path] = {} if holder.valideizers[controller_path].nil?
|
17
19
|
|
18
|
-
|
20
|
+
# raise "Action #{method} is not defined in #{controller_name}" unless action_methods.include? method.to_s
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
+
if holder.valideizers[controller_path][method].nil?
|
23
|
+
holder.valideizers[controller_path][method] = Valideizer::Core.new
|
24
|
+
end
|
25
|
+
|
26
|
+
valideizer = holder.valideizers[controller_path][method]
|
27
|
+
valideizer.instance_eval(&options_block)
|
28
|
+
end
|
22
29
|
end
|
23
30
|
|
24
|
-
|
25
|
-
|
26
|
-
|
31
|
+
def valideizer_callback(method_name)
|
32
|
+
holder = Valideizer::Holder.instance
|
33
|
+
unless holder.output_type.present?
|
34
|
+
# holder.raise_exception
|
35
|
+
holder.callback_controller = controller_path
|
36
|
+
holder.callback_method = method_name.to_s
|
37
|
+
holder.output_type = Valideizer::Holder::CALLBACK_TYPE
|
38
|
+
end
|
39
|
+
end
|
27
40
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
41
|
+
def valideizer_method(method_name)
|
42
|
+
holder = Valideizer::Holder.instance
|
43
|
+
unless holder.output_type.present?
|
44
|
+
holder.render_controller = controller_path
|
45
|
+
holder.render_method = method_name.to_s
|
46
|
+
holder.output_type = Valideizer::Holder::METHOD_TYPE
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def valideizer_render(status = 400, &block)
|
51
|
+
holder = Valideizer::Holder.instance
|
52
|
+
unless holder.output_type.present?
|
53
|
+
holder.status = status
|
54
|
+
holder.render_block = block
|
55
|
+
holder.output_type = Valideizer::Holder::RENDER_TYPE
|
56
|
+
end
|
33
57
|
end
|
34
58
|
end
|
35
59
|
end
|
@@ -37,11 +61,17 @@ module Valideizer
|
|
37
61
|
|
38
62
|
def valideize!
|
39
63
|
valideizer = holder.valideizers[controller_path][action_name] rescue nil
|
40
|
-
return
|
64
|
+
return if valideizer.nil?
|
41
65
|
|
42
|
-
unless valideizer.
|
43
|
-
|
44
|
-
|
66
|
+
unless valideizer.valideized?(params)
|
67
|
+
case holder.output_type
|
68
|
+
when Valideizer::Holder::CALLBACK_TYPE
|
69
|
+
redirect_to controller: holder.callback_controller, action: holder.callback_method, errors: valideizer.errors
|
70
|
+
when Valideizer::Holder::RENDER_TYPE
|
71
|
+
render json: instance_exec(valideizer.errors, &holder.render_block), status: holder.status
|
72
|
+
else
|
73
|
+
render json: valideizer.errors, status: 400
|
74
|
+
end
|
45
75
|
end
|
46
76
|
end
|
47
77
|
|
data/lib/valideizer/rules.rb
CHANGED
@@ -1,26 +1,8 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'time'
|
2
3
|
|
3
4
|
module Valideizer
|
4
5
|
module Rules
|
5
|
-
RULES = %i[
|
6
|
-
eql
|
7
|
-
gt
|
8
|
-
gte
|
9
|
-
lt
|
10
|
-
lte
|
11
|
-
ot
|
12
|
-
range
|
13
|
-
enum
|
14
|
-
type
|
15
|
-
array_type
|
16
|
-
custom_type
|
17
|
-
active_record
|
18
|
-
length
|
19
|
-
regexp
|
20
|
-
nil
|
21
|
-
default
|
22
|
-
]
|
23
|
-
|
24
6
|
def validate(value, rule, constraint)
|
25
7
|
begin
|
26
8
|
case rule
|
@@ -38,6 +20,7 @@ module Valideizer
|
|
38
20
|
when :regexp then validate_regexp value, constraint
|
39
21
|
when :length then validate_length value, constraint
|
40
22
|
when :active_record then validate_active_record value, constraint
|
23
|
+
when :format then validate_time_format value, constraint
|
41
24
|
else true
|
42
25
|
end
|
43
26
|
rescue
|
@@ -99,17 +82,23 @@ module Valideizer
|
|
99
82
|
|
100
83
|
def type_check(value, type)
|
101
84
|
case type
|
102
|
-
when :string
|
103
|
-
when :array
|
104
|
-
when :hash
|
105
|
-
when :integer
|
106
|
-
when :float
|
107
|
-
when :
|
108
|
-
when :json
|
85
|
+
when :string then value.is_a? String
|
86
|
+
when :array then value.is_a? Array
|
87
|
+
when :hash then value.is_a? Hash
|
88
|
+
when :integer then integer_check(value)
|
89
|
+
when :float then float_check(value)
|
90
|
+
when :boolean then boolean_check(value)
|
91
|
+
when :json then json_check(value)
|
92
|
+
when :datetime then date_time_check(value)
|
109
93
|
else raise "Wrong check type #{value}"
|
110
94
|
end
|
111
95
|
end
|
112
96
|
|
97
|
+
def date_time_check(value)
|
98
|
+
Time.parse(value) rescue return(false)
|
99
|
+
true
|
100
|
+
end
|
101
|
+
|
113
102
|
def integer_check(value)
|
114
103
|
casted_value = value.to_i rescue nil
|
115
104
|
if casted_value && (casted_value == 0 && value.to_s.strip == '0' || casted_value != 0)
|
@@ -128,7 +117,7 @@ module Valideizer
|
|
128
117
|
end
|
129
118
|
end
|
130
119
|
|
131
|
-
def
|
120
|
+
def boolean_check(value)
|
132
121
|
['0', '1'].include?(value.to_s.strip) || ['true', 'false'].include?(value.to_s.downcase.strip)
|
133
122
|
end
|
134
123
|
|
@@ -187,5 +176,10 @@ module Valideizer
|
|
187
176
|
raise "#{constraint} is not a valid ActiveRecord model"
|
188
177
|
end
|
189
178
|
end
|
179
|
+
|
180
|
+
def validate_time_format(value, constraint)
|
181
|
+
Time.strptime(value, constraint) rescue return(false)
|
182
|
+
true
|
183
|
+
end
|
190
184
|
end
|
191
185
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Valideizer
|
2
|
+
module RulesChecker
|
3
|
+
RULES_EXCEPTIONS = %i[null default]
|
4
|
+
|
5
|
+
RULES_W_FRIENDLY = {
|
6
|
+
eql: %i[type default null],
|
7
|
+
ot: %i[type default null],
|
8
|
+
gt: %i[type default null lt lte],
|
9
|
+
gte: %i[type default null lt lte],
|
10
|
+
lt: %i[type default null gt gte],
|
11
|
+
lte: %i[type default null gt gte],
|
12
|
+
range: %i[type default null],
|
13
|
+
enum: %i[type default null],
|
14
|
+
type: %i[default null eql ot gt gte lt lte range enum regexp length format],
|
15
|
+
array_type: %i[default null length],
|
16
|
+
custom_type: %i[default null],
|
17
|
+
active_record: %i[default null],
|
18
|
+
length: %i[type array],
|
19
|
+
regexp: %i[type default null],
|
20
|
+
null: %i[all],
|
21
|
+
default: %i[all],
|
22
|
+
format: %i[type default null],
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
VALID_TYPE_RULES = {
|
26
|
+
integer: %i[eql gt gte lt lte ot range enum null default],
|
27
|
+
float: %i[eql gt gte lt lte ot range enum null default],
|
28
|
+
string: %i[enum length eql ot regexp null default],
|
29
|
+
datetime: %i[null default format],
|
30
|
+
json: %i[null default],
|
31
|
+
boolean: %i[null default],
|
32
|
+
hash: %i[null default length],
|
33
|
+
array: %i[null default length],
|
34
|
+
}.freeze
|
35
|
+
|
36
|
+
def check_rule_set(rules)
|
37
|
+
errors = []
|
38
|
+
check_wrong_rules(rules, errors)
|
39
|
+
check_conflicting_rules(rules, errors)
|
40
|
+
check_type_rules(rules, errors)
|
41
|
+
|
42
|
+
raise errors.join('. ') if errors.any?
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def check_wrong_rules(rules, errors)
|
48
|
+
rules.each_key do |rule_name|
|
49
|
+
unless RULES_W_FRIENDLY.include? rule_name
|
50
|
+
errors << "Wrong rule: :#{rule_name}"
|
51
|
+
rules.delete rule_name
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def check_conflicting_rules(rules, errors)
|
58
|
+
rules.each_key do |rule_name|
|
59
|
+
incompatibles = []
|
60
|
+
next if RULES_EXCEPTIONS.include?(rule_name)
|
61
|
+
rules.each_key do |check_rule|
|
62
|
+
next if rule_name == check_rule || RULES_EXCEPTIONS.include?(check_rule)
|
63
|
+
incompatibles << check_rule unless RULES_W_FRIENDLY[rule_name].include?(check_rule)
|
64
|
+
end
|
65
|
+
if incompatibles.any?
|
66
|
+
errors << ":#{rule_name} incompatible with #{incompatibles.join(', ')} rules"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def check_type_rules(rules, errors)
|
72
|
+
return unless rules.include?(:type)
|
73
|
+
|
74
|
+
unless VALID_TYPE_RULES.include? rules[:type]
|
75
|
+
errors << ":#{rules[:type]} isn't avalaible type"
|
76
|
+
return
|
77
|
+
end
|
78
|
+
|
79
|
+
incompatibles = []
|
80
|
+
rules.each_key do |rule_name|
|
81
|
+
next if rule_name == :type || RULES_EXCEPTIONS.include?(rule_name)
|
82
|
+
incompatibles << rule_name unless VALID_TYPE_RULES[rules[:type]].include? rule_name
|
83
|
+
end
|
84
|
+
|
85
|
+
if incompatibles.any?
|
86
|
+
errors << "Type :#{rules[:type]} is incompatible with #{incompatibles.join(", ")} parameters"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/valideizer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valideizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur 'ArtK0DE' Korochansky
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -32,11 +32,13 @@ 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
|
38
39
|
- lib/valideizer/rails.rb
|
39
40
|
- lib/valideizer/rules.rb
|
41
|
+
- lib/valideizer/rules_checker.rb
|
40
42
|
- lib/valideizer/version.rb
|
41
43
|
homepage: https://github.com/artk0de/valideizer
|
42
44
|
licenses:
|
@@ -57,8 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
59
|
- !ruby/object:Gem::Version
|
58
60
|
version: '0'
|
59
61
|
requirements: []
|
60
|
-
|
61
|
-
rubygems_version: 2.7.9
|
62
|
+
rubygems_version: 3.0.3
|
62
63
|
signing_key:
|
63
64
|
specification_version: 4
|
64
65
|
summary: Small Gem to validate parameters
|