valideizer 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f3229a51579000464c6d27a4a08ad6d7a76e8634efae33b5e6beff9cbc239ed1
4
+ data.tar.gz: f252e175f557360fc875538340b1bf381d7169a724c4f5117c6af89bfd405928
5
+ SHA512:
6
+ metadata.gz: a6682014a99bb3cdc5b5a72764e132498fb3eb8347e9bd50468bac55d320b03c312d8dd3eb454872a31abdc2dc51638eb46f01bb07767629b198b6034ad022c7
7
+ data.tar.gz: 167c1b26d637267bc9ba90f999d9b56b3d9f60b74be60f20d5878c8fbe972fc8efd8c1e301a38bbbc95a2457ccb53e93a520fa7a1e81532eb1334984d40dd4fe
@@ -0,0 +1,80 @@
1
+ require 'valideizer/rules'
2
+ require 'valideizer/error_printer'
3
+
4
+ module Valideizer
5
+ class Core
6
+ include Valideizer::Rules
7
+ include Valideizer::ErrorPrinter
8
+
9
+ def initialize
10
+ @rules = {}
11
+ reinit_errrors
12
+ end
13
+
14
+ def add_rule(param, *rules)
15
+ @rules[param] = rules[0]
16
+ end
17
+
18
+ alias valideize add_rule
19
+
20
+ def valideized?(params)
21
+ reinit_errrors
22
+ setup_defaults(params)
23
+ params.each do |param, value|
24
+ next unless nil_check(param, value)
25
+
26
+ @rules[param].each do |type, constraint|
27
+ begin
28
+ push_error(param, value, type, constraint) unless validate(value, type, constraint)
29
+ rescue ArgumentError => ex
30
+ puts ex
31
+ end
32
+ end
33
+ end
34
+
35
+ @errors.empty?
36
+ end
37
+
38
+ def errors
39
+ build_error_messages
40
+ @error_messages
41
+ end
42
+
43
+ private
44
+
45
+ def reinit_errrors
46
+ @errors = {}
47
+ @error_messages = []
48
+ end
49
+
50
+ def push_error(param, value, type, constraint)
51
+ @errors[param] = [] unless @errors.member? param
52
+ @errors[param].push(type: type, value: value, constraint: constraint)
53
+ end
54
+
55
+ def setup_defaults(params)
56
+ @rules.each do |param, rules|
57
+ default_rule = rules.find { |r, _c| r == :default }
58
+ params[param] = default_rule.last unless default_rule.nil?
59
+ end
60
+ end
61
+
62
+ def nil_check(param, value)
63
+ if !value.nil? || value.nil? && has_allow_nil_rule(param)
64
+ true
65
+ else
66
+ push_error(param, :nil, nil, nil)
67
+ false
68
+ end
69
+ end
70
+
71
+ def has_allow_nil_rule(param)
72
+ nil_rule = @rules[param].find { |r, _c| r == :nil }
73
+ nil_rule.nil? ? false : nil_rule.last
74
+ end
75
+
76
+ def build_error_messages
77
+ @error_messages = print_errors
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,52 @@
1
+ module Valideizer
2
+ module ErrorPrinter
3
+ PREFIX = "Validation error:".freeze
4
+
5
+ protected
6
+
7
+ def print_errors
8
+ @errors.map { |param_name, errors| errors.each { |error| { message: print_error(param_name, error) }}}
9
+ end
10
+
11
+ private
12
+
13
+ def print_option(type, constraint, value)
14
+ case type
15
+ when :eql then "Should be equal to #{constraint}."
16
+ when :gt then "Should be greater than #{constraint}."
17
+ when :gte then "Should be greater than or equal to #{constraint}."
18
+ when :lt then "Should be less than #{constraint}."
19
+ when :lte then "Should be less than or equal to #{constraint}."
20
+ when :ot then "Should be other than #{constraint}."
21
+ when :range then "Out of range. Should be in #{constraint.to_s} range."
22
+ when :enum then "Out of enum. Possible values #{constraint.to_s}."
23
+ when :type then "Should be #{constraint} type. Current type: `#{value}`."
24
+ when :array_type then "Should be array of #{constraint} type."
25
+ when :custom_type then "Should be #{constraint} type."
26
+ when :regexp then "Couldn't be matched by #{constraint}."
27
+ when :length then "Length must be #{constraint}. Current value: `#{value}`, length: #{value.length}."
28
+ when :active_record then "Couldn't find #{constraint} with ID=#{value}."
29
+ when :nil then "Can not be nil or empty."
30
+ else ''
31
+ end
32
+ end
33
+
34
+ def print_postfix(type, value)
35
+ "Current value: `#{value}`." if %i[type array_type custom_type active_record length].include?(type)
36
+ end
37
+
38
+ # Validation error: :some param. Should be greater or equal than 200. Current value (100).
39
+
40
+ def print_error(param_name, error)
41
+ message = ""
42
+ message << PREFIX
43
+ message << "`#{param_name}`" + "\s" + "param."
44
+ message << "\s"
45
+ message << print_option(error[:type], error[:constraint], error[:value])
46
+ message << "\s"
47
+ message << (print_postfix(error[:type], error[:value]) || '')
48
+
49
+ message
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,32 @@
1
+ require 'valideizer/core'
2
+
3
+ module Valideizer
4
+ module Rails
5
+ def valideize(method, &options_block)
6
+ @valideizers = {} unless @valideizers.present?
7
+
8
+ @valideizers[method] = Valideizer::Core.new
9
+ @valideizers[method].instance_exec(options_block)
10
+ end
11
+
12
+ def valideize!
13
+ valideizer = @valideizers[action_name]
14
+ if valideizer.present? && @valideizer_callback.present?
15
+ redirect_to(controller: controller_name,
16
+ action: @valideizer_callback, errors: valideizer.errors) unless validiezer.valideize?(params)
17
+ end
18
+ end
19
+
20
+ def valideized?(params)
21
+ @valideizers[action_name].valideized? params
22
+ end
23
+
24
+ def valideizer_errors
25
+ @valideizers[action_name].errors
26
+ end
27
+
28
+ def validezier_callback(method_name)
29
+ @valideizer_callback = method_name
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,160 @@
1
+ require 'json'
2
+
3
+ module Valideizer
4
+ 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
+ def validate(param, rule, constraint)
25
+ case rule
26
+ when :eql then validate_eql param, constraint
27
+ when :gt then validate_gt param, constraint
28
+ when :gte then validate_gte param, constraint
29
+ when :lt then validate_lt param, constraint
30
+ when :lte then validate_lte param, constraint
31
+ when :ot then validate_ot param, constraint
32
+ when :range then validate_range param, constraint
33
+ when :enum then validate_enum param, constraint
34
+ when :type then validate_type param, constraint
35
+ when :array_type then validate_array_type param, constraint
36
+ when :custom_type then validate_custom_type param, constraint
37
+ when :regexp then validate_regexp param, constraint
38
+ when :length then validate_length param, constraint
39
+ when :active_record then validate_active_record param, constraint
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def validate_eql(param, constraint)
46
+ param == constraint
47
+ end
48
+
49
+ def validate_gt(param, constraint)
50
+ param > constraint
51
+ end
52
+
53
+ def validate_gte(param, constraint)
54
+ param >= constraint
55
+ end
56
+
57
+ def validate_lt(param, constraint)
58
+ param < constraint
59
+ end
60
+
61
+ def validate_lte(param, constraint)
62
+ param <= constraint
63
+ end
64
+
65
+ def validate_ot(param, constraint)
66
+ param != constraint
67
+ end
68
+
69
+ def validate_range(param, constraint)
70
+ raise 'Must be a range' unless constraint.is_a? Range
71
+ constraint.include? param
72
+ end
73
+
74
+ def validate_enum(param, constraint)
75
+ raise 'Must be an array' unless constraint.is_a? Array
76
+ constraint.include? param
77
+ end
78
+
79
+ def validate_type(param, constraint)
80
+ if constraint.is_a? Array
81
+ constraint.each { |type| return true if type_check(param, type)}
82
+ else
83
+ type_check(param, constraint)
84
+ end
85
+ end
86
+
87
+ def type_check(param, type)
88
+ res = case type
89
+ when :integer then param.is_a? Integer
90
+ when :float then param.is_a? Float
91
+ when :string then param.is_a? String
92
+ when :array then param.is_a? Array
93
+ when :hash then param.is_a? Hash
94
+ when :bool then bool_check(param)
95
+ when :json then json_check(param)
96
+ else raise "Wrong check type #{param}"
97
+ end
98
+ end
99
+
100
+ def bool_check(param)
101
+ [0, 1].include?(param) || ['true', 'false'].include?(param.to_s.downcase)
102
+ end
103
+
104
+ def json_check(param)
105
+ [Hash, Array].include?((JSON.parse param rescue nil).class)
106
+ end
107
+
108
+ def validate_array_type(param, constraint)
109
+ if param.is_a? Array
110
+ param.each do |v|
111
+ if v.is_a?(Array)
112
+ validate_array_type(v, constraint)
113
+ else
114
+ return false unless validate_type(v, constraint)
115
+ end
116
+ end
117
+
118
+ true
119
+ else
120
+ false
121
+ end
122
+ end
123
+
124
+ def validate_regexp(param, regexp)
125
+ raise 'Must be a string' unless param.is_a? String
126
+ param.match? regexp
127
+ end
128
+
129
+ def validate_custom_type(param, constraint)
130
+ param.is_a? constraint
131
+ end
132
+
133
+ def validate_length(param, constraint)
134
+ if [Array, Hash, String].include? param.class
135
+ if constraint.is_a? Hash
136
+ raise 'Hash params can not be empty.' if constraint.empty?
137
+ res = true
138
+ res &= param.length >= constraint[:min] unless constraint[:min].nil?
139
+ res &= param.length <= constraint[:max] unless constraint[:max].nil?
140
+ res
141
+ elsif constraint.is_a? Range
142
+ constraint.include? param.length
143
+ else
144
+ raise 'Wrong constraint for :length option. Must be range or hash {min: 0, max: 10}'
145
+ end
146
+ else
147
+ raise 'Must be Array, Hash or String'
148
+ end
149
+ end
150
+
151
+ def validate_active_record(param, constraint)
152
+ klass = constraint
153
+ if klass.is_a? ActiveModel || (klass = constraint.to_s.constantize).is_a?(ActiveModel)
154
+ klass.find_by_id(param).present?
155
+ else
156
+ raise "#{constraint} is not a valid ActiveRecord model"
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,3 @@
1
+ module Valideizer
2
+ VERSION = "1.0.2"
3
+ end
data/lib/valideizer.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative 'valideizer/core.rb'
2
+ require_relative 'valideizer/rules.rb'
3
+ require_relative 'valideizer/rails.rb'
4
+ require_relative 'valideizer/version.rb'
5
+ require_relative 'valideizer/error_printer.rb'
6
+
7
+ module Valideizer; end
8
+
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valideizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Arthur 'ArtK0DE' Korochansky
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ description: Small Gem to validate parameters. Can be integrated with Rails controllers
28
+ email:
29
+ - art2rik.desperado@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/valideizer.rb
35
+ - lib/valideizer/core.rb
36
+ - lib/valideizer/error_printer.rb
37
+ - lib/valideizer/rails.rb
38
+ - lib/valideizer/rules.rb
39
+ - lib/valideizer/version.rb
40
+ homepage: https://github.com/artk0de/valideizer
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.7.9
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Small Gem to validate parameters
64
+ test_files: []