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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/is_valid.rb +87 -31
  3. metadata +37 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a630e0455a4889ed70aa01d3fda7d1295a44670992d6a2fe0e38376f97db86c
4
- data.tar.gz: f57d9b9b9b48a7e8249f0729c60892ee7892eba630cfc118a9cf9c197f45a833
3
+ metadata.gz: 5f3124527532dea71ceca0bd29c6860041062dc600eea4fadc648a3d0a0a585a
4
+ data.tar.gz: 7bf5c3310a903232b5d1c3b55030774c3bbcfffac924358760cc7f2bf1a59735
5
5
  SHA512:
6
- metadata.gz: 865393e3653a9e33504cc7d3692113e17b50fc0322ad6b991c8b1c22fad52f890260f888fbce59a585d74432cefcfa037e3566670ae797dec87e615b528e297f
7
- data.tar.gz: 39ed0674d51b919649728f74abb025c04bf6e4ef906d3cfa6428500345683fefeef4eff5ee6831e7516cdaa30b5557e7028b859e09d5097662224c1839e608f3
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
- def initialize(templates = {}, rules = {})
5
- @rules = {
6
- integer: '^(\d)+$',
7
- float: '^(\d)+.(\d)+$',
8
- float_with_comma: '^(\d)+,(\d)+$',
9
- word: '^(\w)+$',
10
- words: '^(\w| )+$',
11
- sentence: '^(\w| |,)+(\.|!|\?)$',
12
- boolean: '^(1|0|true|false)$',
13
- url: '^(http://|https://)(.)+\.(.)+$',
14
- email: '^([a-z,A-Z,\-,\_,\.]*)@(.*)+\.(.*)$',
15
- any: '(.*)+',
16
- string: '(.*)+',
17
- nil: nil
18
- }
19
- @rules = @rules.merge(rules)
20
- @templates = templates
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 check(var, rule_name)
24
- if rule_name[-1] == '*'
25
- rule_name = rule_name[0..-2]
26
- res = var.nil?
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
- res ||= @rules[rule_name.to_sym].nil? ? var.nil? : var.to_s.match?(@rules[rule_name.to_sym])
29
- res
87
+ errors.empty? ? true : errors.uniq
30
88
  end
31
89
 
32
- def check_hash(hash_var, template)
90
+ def check_required(hash_var, template_rules)
33
91
  errors = []
34
- rules = @templates[template.to_sym]
35
- if !rules.nil?
36
- hash_var.each do |key, val|
37
- errors << "#{key} is not valid, should be #{rules[key.to_sym]}" unless !rules[key.to_sym].nil? && check(val, rules[key.to_sym])
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.empty? ? true : 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.1.2
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: 2024-05-13 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Ruby Gem to make a validation of a variable or a hash, based on regular
14
- expressions or own predefined validation templates in an easy way. See full documentation
15
- on GitHub.
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
- post_install_message: You can find docs about is_valid gem on https://butteff.ru/en/extensions.html
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 predefined or own easy rules
75
+ summary: Validation of a variable or a hash, based on pre-defined or own easy rules
47
76
  test_files: []