simple_validator 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/simple_validator.rb +95 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzMzMGRlMTI1ZWUyMDcxY2M5MjczMDk3NjEyMjBjOTE2NzZiZjZjNQ==
5
+ data.tar.gz: !binary |-
6
+ ZTQ2ZmJmMzQwYzJiZTBmZGM0ODI4MzE2MWU5N2I3ZTNlYmUwZjFmYQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NWYyZDk4NTI1MWI0ZGM3YzMxNDQ3NTg5Y2E4YTZiN2IwYTYxZTZmOWViNTli
10
+ YjA4NmFiNzRmODc2ZTIyZmI5MDY0MjkwMDBmMjM1ODg5ODAxMzVkZTY5ZDU3
11
+ YjljYzY1NDM4YTE2YjY0NGM5NzQ1NWMzYjNhMWE0NDM3NzJkMjI=
12
+ data.tar.gz: !binary |-
13
+ MTkzZTIwOTYzZDZiZTgyZmI1MjM3YzdhYzY5ZmU3MDZmZDc4OTdjNzI2Yzlh
14
+ Y2YwZTk3ODg1N2Q0Yjk5ZmUwNTRmN2M2MjFhOTliM2VkYTQwMjhiMWI3Yzk4
15
+ NTFjNTZhZTM1NGU1OGY0Yjk3MDc0NzMxYmNmNTkxMjQ4NTlkNDM=
@@ -0,0 +1,95 @@
1
+ class SimpleValidator
2
+ def check_hash(hash, constraints, cast=false)
3
+ raise ArgumentError unless hash.is_a?(Hash)
4
+ check_hash_constraints(constraints)
5
+
6
+ unknown = []
7
+ missing = []
8
+ invalid = []
9
+
10
+ constraints[:required].each do |constraint|
11
+ if hash.has_key?(constraint[:key])
12
+ if cast
13
+ if check_cast(hash[constraint[:key]], constraint[:type])
14
+ invalid << constraint[:key]
15
+ end
16
+ elsif !hash[constraint[:key]].is_a?(constraint[:type])
17
+ invalid << constraint[:key]
18
+ end
19
+ else
20
+ missing << constraint[:key]
21
+ end
22
+ end
23
+
24
+ hash.each_key do |key|
25
+ required = constraints[:required].any? { |constraint| key.to_sym == constraint[:key] }
26
+ optional = constraints[:optional].any? { |constraint| key.to_sym == constraint[:key] }
27
+
28
+ if !required && !optional
29
+ unknown << key
30
+ end
31
+ end
32
+
33
+ case
34
+ when !invalid.empty? && !unknown.empty? && !missing.empty?
35
+ { :invalid => invalid, :unknown => unknown, :missing => missing }
36
+ when !invalid.empty? && !unknown.empty?
37
+ { :invalid => invalid, :unknown => unknown }
38
+ when !invalid.empty? && !missing.empty?
39
+ { :invalid => invalid, :missing => missing }
40
+ when !unknown.empty? && !missing.empty?
41
+ { :unknown => unknown, :missing => missing }
42
+ when !unknown.empty?
43
+ { :unknown => unknown }
44
+ when !missing.empty?
45
+ { :missing => missing }
46
+ when !invalid.empty?
47
+ { :invalid => invalid }
48
+ end
49
+ end
50
+
51
+ private
52
+ def check_hash_constraints(constraints)
53
+ raise ArgumentError unless constraints.is_a?(Hash)
54
+
55
+ allowed = [:required, :optional]
56
+
57
+ constraints.each_key do |key|
58
+ raise ArgumentError unless allowed.include?(key)
59
+ raise TypeError unless constraints[key].is_a?(Array)
60
+
61
+ constraints[key].each do |constraint|
62
+ raise ArgumentError unless constraint.has_key?(:key) && constraint.has_key?(:type)
63
+ raise TypeError unless constraint[:key].is_a?(Symbol)
64
+ raise TypeError unless constraint[:type].is_a?(Class)
65
+ end
66
+ end
67
+
68
+ allowed.each do |constraint|
69
+ if !constraints.has_key?(constraint)
70
+ constraints[constraint] = []
71
+ end
72
+ end
73
+ end
74
+
75
+ private
76
+ def check_cast(value, type)
77
+ if !value.is_a?(type)
78
+ case
79
+ when type == Date
80
+ begin
81
+ !Date.parse(value)
82
+ rescue
83
+ return true
84
+ end
85
+ when type == Integer
86
+ !value.to_i
87
+ when type == String
88
+ !value.to_s
89
+ else
90
+ raise ArgumentError
91
+ end
92
+ end
93
+ end
94
+ end
95
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Young
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: simple validation
14
+ email: young.c.5690@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/simple_validator.rb
20
+ homepage: https://github.com/chris--young/simple_validator
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: simple validation
44
+ test_files: []