jsondef 0.0.0 → 0.0.1
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 +66 -0
- data/lib/jsondef.rb +45 -55
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 437c6440ce1877442f035d8c21dcd01f531ed9e8
|
4
|
+
data.tar.gz: 0b0c349bb1096085c4d25698a16def0380af877f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef51c4ca549455f1ef097ddc17c934680091d5aa0c4f9c71dace53bd6217f87f55426dd78a4e3ac86b870156604a17618b8edb7dc1d1d12e249ee1522004e38f
|
7
|
+
data.tar.gz: e7c5784f65f151e0d10f099d3eb9aea4d082c83498159722f49c962741ff21a30d79ba9d3c32e19fa61e70e2e5a2337294fb35bd4b047fceeabdd16cd3a839b3
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'pp'
|
3
|
+
require_relative 'jsondef.rb'
|
4
|
+
|
5
|
+
module ConfigReaderFactory
|
6
|
+
|
7
|
+
def ConfigReaderFactory.fromJsonFile(path)
|
8
|
+
ConfigReader.new(YAML.load(IO.read(path)))
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
class ConfigReader
|
14
|
+
|
15
|
+
attr_reader :rule
|
16
|
+
|
17
|
+
def initialize(conf)
|
18
|
+
@rule = parse_element(conf['rule'])
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_element(conf)
|
22
|
+
case conf['type']
|
23
|
+
when 'object'
|
24
|
+
parse_object(conf)
|
25
|
+
when 'array'
|
26
|
+
parse_array(conf)
|
27
|
+
when 'string'
|
28
|
+
parse_string(conf)
|
29
|
+
when 'number'
|
30
|
+
parse_number(conf)
|
31
|
+
else
|
32
|
+
raise "Unknown rule type: " + conf['type']
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def parse_object(conf)
|
37
|
+
rule = JsonRuleObject.new
|
38
|
+
conf['keys'].each { |k, v| rule.add_key_rule(parse_object_key(k, v)) } if conf.has_key?('keys')
|
39
|
+
|
40
|
+
rule.disallow_other_keys if conf.has_key?('disallow_other_keys') && conf['disallow_other_keys']
|
41
|
+
rule
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse_object_key(key, conf)
|
45
|
+
rule = JsonRuleObjectKey.new(key)
|
46
|
+
rule.set_optional if conf.has_key?('optional') && conf['optional']
|
47
|
+
rule.set_rule(parse_element(conf['rule'])) if conf.has_key?('rule')
|
48
|
+
rule
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_array(conf)
|
52
|
+
rule = JsonRuleArray.new
|
53
|
+
rule
|
54
|
+
end
|
55
|
+
|
56
|
+
def parse_string(conf)
|
57
|
+
rule = JsonRuleString.new
|
58
|
+
rule
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse_number(conf)
|
62
|
+
rule = JsonRuleNumber.new
|
63
|
+
rule
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/jsondef.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
1
|
module JsonDef
|
2
2
|
|
3
|
+
ANY_TYPE = :wildcard
|
4
|
+
|
3
5
|
def JsonDef.verify(obj, rule)
|
4
6
|
case rule.class.to_s
|
5
7
|
when 'JsonRuleObject'
|
6
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)
|
7
15
|
else
|
8
|
-
raise
|
16
|
+
raise "Unknown rule at [verify] level: #{rule.class}"
|
9
17
|
end
|
10
18
|
end
|
11
19
|
|
12
|
-
def JsonDef.
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
when :array
|
21
|
-
value.kind_of?(Array)
|
22
|
-
when :wildcard
|
23
|
-
# Land of satisfaction.
|
24
|
-
true
|
25
|
-
else
|
26
|
-
raise 'Unknown value type declaration'
|
27
|
-
end
|
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
28
|
end
|
29
29
|
|
30
30
|
def JsonDef.verify_object(obj, rule)
|
@@ -35,23 +35,17 @@ module JsonDef
|
|
35
35
|
return true unless obj.has_key?(key_rule.key)
|
36
36
|
|
37
37
|
case
|
38
|
-
when key_rule.value
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
return false unless JsonDef.verify_object(obj[key_rule.key], key_rule.value)
|
43
|
-
|
44
|
-
when key_rule.value.kind_of?(JsonRuleArray)
|
45
|
-
return false unless JsonDef.verify_array(obj[key_rule.key], key_rule.value)
|
46
|
-
|
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)
|
47
42
|
else
|
48
|
-
|
49
|
-
return false unless obj[key_rule.key] == key_rule.value
|
43
|
+
return false
|
50
44
|
end
|
51
45
|
end
|
52
46
|
|
53
|
-
# @todo In case of
|
54
|
-
if rule.
|
47
|
+
# @todo In case of not allowing other keys, check if there are other not-allowed keys.
|
48
|
+
if !rule.allow_other_keys
|
55
49
|
obj.each { |k, v| return false unless rule.keys.member?(k) }
|
56
50
|
end
|
57
51
|
|
@@ -59,28 +53,27 @@ module JsonDef
|
|
59
53
|
end
|
60
54
|
|
61
55
|
def JsonDef.verify_array(obj, rule)
|
62
|
-
return false
|
56
|
+
return false unless obj.kind_of?(Array)
|
63
57
|
true
|
64
58
|
end
|
65
59
|
|
66
60
|
end
|
67
61
|
|
68
|
-
class
|
62
|
+
class JsonRuleBase
|
63
|
+
end
|
69
64
|
|
70
|
-
|
65
|
+
class JsonRuleObject < JsonRuleBase
|
66
|
+
|
67
|
+
attr_reader :key_rules, :keys, :allow_other_keys
|
71
68
|
|
72
69
|
def initialize()
|
73
|
-
@
|
70
|
+
@allow_other_keys = true
|
74
71
|
@key_rules = []
|
75
72
|
@keys = []
|
76
73
|
end
|
77
74
|
|
78
|
-
def
|
79
|
-
@
|
80
|
-
end
|
81
|
-
|
82
|
-
def set_strict
|
83
|
-
@strict = true
|
75
|
+
def disallow_other_keys
|
76
|
+
@allow_other_keys = false
|
84
77
|
self
|
85
78
|
end
|
86
79
|
|
@@ -92,13 +85,6 @@ class JsonRuleObject
|
|
92
85
|
|
93
86
|
end
|
94
87
|
|
95
|
-
class JsonRuleArray
|
96
|
-
|
97
|
-
def initialize
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
88
|
class JsonRuleObjectKey
|
103
89
|
|
104
90
|
attr_reader :required, :key, :value
|
@@ -106,22 +92,26 @@ class JsonRuleObjectKey
|
|
106
92
|
def initialize(key)
|
107
93
|
@key = key
|
108
94
|
@required = true
|
109
|
-
@value =
|
95
|
+
@value = JsonDef::ANY_TYPE
|
110
96
|
end
|
111
97
|
|
112
|
-
def set_optional
|
98
|
+
def set_optional
|
113
99
|
@required = false
|
114
100
|
self
|
115
101
|
end
|
116
102
|
|
117
|
-
def
|
118
|
-
@value = type
|
119
|
-
self
|
120
|
-
end
|
121
|
-
|
122
|
-
def set_value_rule(rule)
|
103
|
+
def set_rule(rule)
|
123
104
|
@value = rule
|
124
105
|
self
|
125
106
|
end
|
126
107
|
|
127
108
|
end
|
109
|
+
|
110
|
+
class JsonRuleArray < JsonRuleBase
|
111
|
+
end
|
112
|
+
|
113
|
+
class JsonRuleNumber < JsonRuleBase
|
114
|
+
end
|
115
|
+
|
116
|
+
class JsonRuleString < JsonRuleBase
|
117
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsondef
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Arato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Can create JSON descriptor and verify if a given object satisfy it.
|
14
14
|
email: it.arato@gmail.com
|
@@ -16,6 +16,7 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- lib/config_reader.rb
|
19
20
|
- lib/jsondef.rb
|
20
21
|
homepage: http://itarato.com
|
21
22
|
licenses:
|