jsondef 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/config_reader.rb +1 -1
- data/lib/jsondef.rb +3 -117
- data/lib/rules.rb +57 -0
- data/lib/verify.rb +62 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46d05b8439f93fa6b9ddd50761c1e914ba344a94
|
4
|
+
data.tar.gz: bc9aaab652ecd1e496f0adafc634e4777d08b23e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed8a2302c9c9f5c636a5539d087e3aa8c3d30a56b864cfa3fc7be6142c01b4a48a8d9c15d7b6cb1c19c0807aa26fe81b121f61838ae0f99c03d4476c3c380171
|
7
|
+
data.tar.gz: 7d209081b8ef4143d36d604a6acfbcc6ab949443ed3a2312e7cf29ffca55803a64282945433aeddca4d1407e77c4455c100f159bc8e138f46af9a1797872ee00
|
data/lib/config_reader.rb
CHANGED
data/lib/jsondef.rb
CHANGED
@@ -1,117 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def JsonDef.verify(obj, rule)
|
6
|
-
case rule.class.to_s
|
7
|
-
when 'JsonRuleObject'
|
8
|
-
JsonDef.verify_object(obj, rule)
|
9
|
-
when 'JsonRuleArray'
|
10
|
-
JsonDef.verify_array(obj, rule)
|
11
|
-
when 'JsonRuleString'
|
12
|
-
JsonDef.verify_string(obj, rule)
|
13
|
-
when 'JsonRuleNumber'
|
14
|
-
JsonDef.verify_number(obj, rule)
|
15
|
-
else
|
16
|
-
raise "Unknown rule at [verify] level: #{rule.class}"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def JsonDef.verify_string(obj, rule)
|
21
|
-
return false unless obj.kind_of?(String)
|
22
|
-
true
|
23
|
-
end
|
24
|
-
|
25
|
-
def JsonDef.verify_number(obj, rule)
|
26
|
-
return false unless obj.kind_of?(Numeric)
|
27
|
-
true
|
28
|
-
end
|
29
|
-
|
30
|
-
def JsonDef.verify_object(obj, rule)
|
31
|
-
return false unless obj.kind_of?(Hash)
|
32
|
-
|
33
|
-
rule.key_rules.each do |key_rule|
|
34
|
-
return false if key_rule.required && !obj.has_key?(key_rule.key)
|
35
|
-
return true unless obj.has_key?(key_rule.key)
|
36
|
-
|
37
|
-
case
|
38
|
-
when key_rule.value == JsonDef::ANY_TYPE
|
39
|
-
next
|
40
|
-
when key_rule.value.kind_of?(JsonRuleBase)
|
41
|
-
return false unless JsonDef.verify(obj[key_rule.key], key_rule.value)
|
42
|
-
else
|
43
|
-
return false
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# @todo In case of not allowing other keys, check if there are other not-allowed keys.
|
48
|
-
if !rule.allow_other_keys
|
49
|
-
obj.each { |k, v| return false unless rule.keys.member?(k) }
|
50
|
-
end
|
51
|
-
|
52
|
-
true
|
53
|
-
end
|
54
|
-
|
55
|
-
def JsonDef.verify_array(obj, rule)
|
56
|
-
return false unless obj.kind_of?(Array)
|
57
|
-
true
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
class JsonRuleBase
|
63
|
-
end
|
64
|
-
|
65
|
-
class JsonRuleObject < JsonRuleBase
|
66
|
-
|
67
|
-
attr_reader :key_rules, :keys, :allow_other_keys
|
68
|
-
|
69
|
-
def initialize()
|
70
|
-
@allow_other_keys = true
|
71
|
-
@key_rules = []
|
72
|
-
@keys = []
|
73
|
-
end
|
74
|
-
|
75
|
-
def disallow_other_keys
|
76
|
-
@allow_other_keys = false
|
77
|
-
self
|
78
|
-
end
|
79
|
-
|
80
|
-
def add_key_rule(rule)
|
81
|
-
@key_rules.push(rule)
|
82
|
-
@keys.push(rule.key)
|
83
|
-
self
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
class JsonRuleObjectKey
|
89
|
-
|
90
|
-
attr_reader :required, :key, :value
|
91
|
-
|
92
|
-
def initialize(key)
|
93
|
-
@key = key
|
94
|
-
@required = true
|
95
|
-
@value = JsonDef::ANY_TYPE
|
96
|
-
end
|
97
|
-
|
98
|
-
def set_optional
|
99
|
-
@required = false
|
100
|
-
self
|
101
|
-
end
|
102
|
-
|
103
|
-
def set_rule(rule)
|
104
|
-
@value = rule
|
105
|
-
self
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
class JsonRuleArray < JsonRuleBase
|
111
|
-
end
|
112
|
-
|
113
|
-
class JsonRuleNumber < JsonRuleBase
|
114
|
-
end
|
115
|
-
|
116
|
-
class JsonRuleString < JsonRuleBase
|
117
|
-
end
|
1
|
+
require_relative 'verify.rb'
|
2
|
+
require_relative 'config_reader.rb'
|
3
|
+
require_relative 'rules.rb'
|
data/lib/rules.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
class JsonRuleBase
|
3
|
+
end
|
4
|
+
|
5
|
+
class JsonRuleObject < JsonRuleBase
|
6
|
+
|
7
|
+
attr_reader :key_rules, :keys, :allow_other_keys
|
8
|
+
|
9
|
+
def initialize()
|
10
|
+
@allow_other_keys = true
|
11
|
+
@key_rules = []
|
12
|
+
@keys = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def disallow_other_keys
|
16
|
+
@allow_other_keys = false
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_key_rule(rule)
|
21
|
+
@key_rules.push(rule)
|
22
|
+
@keys.push(rule.key)
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class JsonRuleObjectKey
|
29
|
+
|
30
|
+
attr_reader :required, :key, :value
|
31
|
+
|
32
|
+
def initialize(key)
|
33
|
+
@key = key
|
34
|
+
@required = true
|
35
|
+
@value = JsonDef::ANY_TYPE
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_optional
|
39
|
+
@required = false
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def set_rule(rule)
|
44
|
+
@value = rule
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class JsonRuleArray < JsonRuleBase
|
51
|
+
end
|
52
|
+
|
53
|
+
class JsonRuleNumber < JsonRuleBase
|
54
|
+
end
|
55
|
+
|
56
|
+
class JsonRuleString < JsonRuleBase
|
57
|
+
end
|
data/lib/verify.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative 'rules.rb'
|
2
|
+
|
3
|
+
module JsonDef
|
4
|
+
|
5
|
+
ANY_TYPE = :wildcard
|
6
|
+
|
7
|
+
def JsonDef.verify(obj, rule)
|
8
|
+
case rule.class.to_s
|
9
|
+
when 'JsonRuleObject'
|
10
|
+
JsonDef.verify_object(obj, rule)
|
11
|
+
when 'JsonRuleArray'
|
12
|
+
JsonDef.verify_array(obj, rule)
|
13
|
+
when 'JsonRuleString'
|
14
|
+
JsonDef.verify_string(obj, rule)
|
15
|
+
when 'JsonRuleNumber'
|
16
|
+
JsonDef.verify_number(obj, rule)
|
17
|
+
else
|
18
|
+
raise "Unknown rule at [verify] level: #{rule.class}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def JsonDef.verify_string(obj, rule)
|
23
|
+
return false unless obj.kind_of?(String)
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
def JsonDef.verify_number(obj, rule)
|
28
|
+
return false unless obj.kind_of?(Numeric)
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
def JsonDef.verify_object(obj, rule)
|
33
|
+
return false unless obj.kind_of?(Hash)
|
34
|
+
|
35
|
+
rule.key_rules.each do |key_rule|
|
36
|
+
return false if key_rule.required && !obj.has_key?(key_rule.key)
|
37
|
+
return true unless obj.has_key?(key_rule.key)
|
38
|
+
|
39
|
+
case
|
40
|
+
when key_rule.value == JsonDef::ANY_TYPE
|
41
|
+
next
|
42
|
+
when key_rule.value.kind_of?(JsonRuleBase)
|
43
|
+
return false unless JsonDef.verify(obj[key_rule.key], key_rule.value)
|
44
|
+
else
|
45
|
+
return false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @todo In case of not allowing other keys, check if there are other not-allowed keys.
|
50
|
+
if !rule.allow_other_keys
|
51
|
+
obj.each { |k, v| return false unless rule.keys.member?(k) }
|
52
|
+
end
|
53
|
+
|
54
|
+
true
|
55
|
+
end
|
56
|
+
|
57
|
+
def JsonDef.verify_array(obj, rule)
|
58
|
+
return false unless obj.kind_of?(Array)
|
59
|
+
true
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsondef
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Arato
|
@@ -18,7 +18,9 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- lib/config_reader.rb
|
20
20
|
- lib/jsondef.rb
|
21
|
-
|
21
|
+
- lib/rules.rb
|
22
|
+
- lib/verify.rb
|
23
|
+
homepage: https://github.com/itarato/JsonDefRuby
|
22
24
|
licenses:
|
23
25
|
- MIT
|
24
26
|
metadata: {}
|