is_valid 0.1.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/is_valid.rb +87 -31
- metadata +37 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f3124527532dea71ceca0bd29c6860041062dc600eea4fadc648a3d0a0a585a
|
4
|
+
data.tar.gz: 7bf5c3310a903232b5d1c3b55030774c3bbcfffac924358760cc7f2bf1a59735
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40cd3210e308bce13e9c4293203d5448ddc1cfac633bd12e593bf5beacd135bea46f870a577f4f6496cc1e11c9d8970e21da1bf3367f852dbdb2c190e1c91003
|
7
|
+
data.tar.gz: fc2e6e5008fad2eca93f14c53dc871f79ce5c12bca995c7b03f4b93132f51a5c5d4aa0b56e15760c26d1112d28babaff35e242987c3791a4a53bdcb5aafdb67c
|
data/lib/is_valid.rb
CHANGED
@@ -1,44 +1,100 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'modules/validators'
|
4
|
+
require_relative 'modules/errors'
|
5
|
+
|
6
|
+
# Ruby Gem to make a validation of a variable or a hash, based on regular expressions
|
7
|
+
# or own pre-defined validation templates in an easy way. See full documentation on GitHub.
|
3
8
|
class IsValid
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
}
|
19
|
-
@
|
20
|
-
@
|
9
|
+
include Validators
|
10
|
+
include Errors
|
11
|
+
|
12
|
+
TYPES = %w[string array hash class module symbol].freeze
|
13
|
+
STRICT_TYPES = %w[integer float].freeze
|
14
|
+
|
15
|
+
@rules = {}
|
16
|
+
@templates = {}
|
17
|
+
@errors = {}
|
18
|
+
@all_keys = false
|
19
|
+
@strict_types = false
|
20
|
+
|
21
|
+
def initialize(settings = {})
|
22
|
+
@templates = settings[:templates] || {}
|
23
|
+
@errors = settings[:errors] || {}
|
24
|
+
@all_keys = settings[:all_keys] || false
|
25
|
+
@strict_types = settings[:strict_types] || false
|
26
|
+
rules = settings[:rules] || {}
|
27
|
+
types_hash = {}
|
28
|
+
TYPES.each do |type|
|
29
|
+
types_hash[type.to_sym] = type
|
30
|
+
end
|
31
|
+
validators = init_rules
|
32
|
+
@rules = validators.merge(rules)
|
33
|
+
@rules = @rules.merge(types_hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
def check(val, rules)
|
37
|
+
validated = rules.is_a?(String) ? check_with_types(val, rules) : check_one_of_rules(val, rules)
|
38
|
+
validated.is_a?(Array) ? false : validated
|
39
|
+
end
|
40
|
+
|
41
|
+
def check_with_types(val, rule_name)
|
42
|
+
return true if rule_name == 'any'
|
43
|
+
return val.nil? if rule_name == 'nil'
|
44
|
+
return val.is_a?(Object.const_get(rule_name.capitalize)) if TYPES.include?(rule_name)
|
45
|
+
return val.is_a?(Object.const_get(rule_name.capitalize)) if @strict_types && STRICT_TYPES.include?(rule_name)
|
46
|
+
return [true, false].include?(val) if @strict_types && rule_name == 'boolean'
|
47
|
+
|
48
|
+
rule = @rules[rule_name.to_sym]
|
49
|
+
val.to_s.match?(rule)
|
21
50
|
end
|
22
51
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
52
|
+
def rule_exists(rule)
|
53
|
+
!rule.nil? && @rules[rule.to_sym].nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
def check_one_of_rules(val, rules, key = 'variable')
|
57
|
+
errors = []
|
58
|
+
rules.each do |rule|
|
59
|
+
if rule_exists(rule)
|
60
|
+
errors << error_no_rule(rule)
|
61
|
+
elsif !check(val, rule)
|
62
|
+
errors << error_not_valid(key, rule, @errors)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
!errors.empty? && errors.length == rules.length ? errors : true
|
66
|
+
end
|
67
|
+
|
68
|
+
def check_hash(hash_var, template, errors = [])
|
69
|
+
template_rules = @templates[template.to_sym]
|
70
|
+
return [error_no_template(template)] if template_rules.nil?
|
71
|
+
|
72
|
+
req_errors = check_required(hash_var, template_rules)
|
73
|
+
return req_errors unless req_errors.empty?
|
74
|
+
|
75
|
+
hash_var.each do |key, val|
|
76
|
+
rules = template_rules[key.to_sym]
|
77
|
+
if @all_keys && rules.nil?
|
78
|
+
errors << error_no_key(key, template)
|
79
|
+
elsif !rules.nil?
|
80
|
+
rules = [rules] if rules.is_a?(String)
|
81
|
+
validated = check_one_of_rules(val, rules, key)
|
82
|
+
errors += validated if validated != true
|
83
|
+
inner = check_hash(val, template, errors) if val.is_a?(Hash)
|
84
|
+
errors += inner if !inner.nil? && inner != true
|
85
|
+
end
|
27
86
|
end
|
28
|
-
|
29
|
-
res
|
87
|
+
errors.empty? ? true : errors.uniq
|
30
88
|
end
|
31
89
|
|
32
|
-
def
|
90
|
+
def check_required(hash_var, template_rules)
|
33
91
|
errors = []
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
errors <<
|
92
|
+
template_rules.each_key do |key|
|
93
|
+
if key.match?('_required$')
|
94
|
+
key = key.to_s.gsub(/_required$/, '')
|
95
|
+
errors << error_required_key(key) if hash_var[key.to_sym].nil?
|
38
96
|
end
|
39
|
-
else
|
40
|
-
p 'template with rules does not exist'
|
41
97
|
end
|
42
|
-
errors
|
98
|
+
errors
|
43
99
|
end
|
44
100
|
end
|
metadata
CHANGED
@@ -1,18 +1,46 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: is_valid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergei Illarionov (www.butteff.ru/en/)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
date: 2025-01-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.69'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.69'
|
41
|
+
description: |-
|
42
|
+
Ruby Gem to make a validation of a variable or a hash, based on regular expressions
|
43
|
+
or own pre-defined validation templates in an easy way. See full documentation on GitHub.
|
16
44
|
email: butteff.ru@gmail.com
|
17
45
|
executables: []
|
18
46
|
extensions: []
|
@@ -24,7 +52,8 @@ licenses:
|
|
24
52
|
- MIT
|
25
53
|
metadata:
|
26
54
|
source_code_uri: https://github.com/butteff/is_valid_ruby_gem
|
27
|
-
|
55
|
+
rubygems_mfa_required: 'true'
|
56
|
+
post_install_message: You can find docs about is_valid gem on https://butteff.ru/en/extensions
|
28
57
|
or on https://github.com/butteff/is_valid_ruby_gem
|
29
58
|
rdoc_options: []
|
30
59
|
require_paths:
|
@@ -43,5 +72,5 @@ requirements: []
|
|
43
72
|
rubygems_version: 3.4.19
|
44
73
|
signing_key:
|
45
74
|
specification_version: 4
|
46
|
-
summary: Validation of a variable or a hash, based on
|
75
|
+
summary: Validation of a variable or a hash, based on pre-defined or own easy rules
|
47
76
|
test_files: []
|