jsondef 0.1.1 → 0.1.2
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 +15 -3
- data/lib/rules.rb +26 -18
- data/lib/verify.rb +22 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2756f40c259d50404177ef9ea8491267fd606632
|
4
|
+
data.tar.gz: 04cc9e350319ab4aa5232a1b331883ae06dc77e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fac54aa304dee05dc45a9c3f88d0d094c996bf8264014d7f1e124d90999eacf2b630d31ec2d07b97da5a2097a476dbd235a688d997705e38e9ea883cda451af4
|
7
|
+
data.tar.gz: 40c14afd5032a0e1ce96c23011b2168b1d536657d016633ef2d018b483b47a973b4671aa35ed39afef34ce65049332240ee1b4c665d73781aaa3a299a2f0fe57
|
data/lib/config_reader.rb
CHANGED
@@ -54,21 +54,25 @@ class ConfigReader
|
|
54
54
|
|
55
55
|
def parse_array(conf)
|
56
56
|
rule = JsonRuleArray.new
|
57
|
+
min_val = if conf.has_key?('min') then conf['min'] else nil end
|
58
|
+
max_val = if conf.has_key?('max') then conf['max'] else nil end
|
59
|
+
rule.set_count(min_val, max_val)
|
60
|
+
rule.set_rule(parse_element(conf['rule'])) if conf.has_key?('rule')
|
57
61
|
rule
|
58
62
|
end
|
59
63
|
|
60
64
|
def parse_string(conf)
|
61
|
-
rule = JsonRuleString.new
|
65
|
+
rule = JsonRuleString.new(extract_exact_value(conf))
|
62
66
|
rule
|
63
67
|
end
|
64
68
|
|
65
69
|
def parse_number(conf)
|
66
|
-
rule = JsonRuleNumber.new
|
70
|
+
rule = JsonRuleNumber.new(extract_exact_value(conf))
|
67
71
|
rule
|
68
72
|
end
|
69
73
|
|
70
74
|
def parse_boolean(conf)
|
71
|
-
rule = JsonRuleBoolean.new
|
75
|
+
rule = JsonRuleBoolean.new(extract_exact_value(conf))
|
72
76
|
rule
|
73
77
|
end
|
74
78
|
|
@@ -76,4 +80,12 @@ class ConfigReader
|
|
76
80
|
JsonRuleNull.new
|
77
81
|
end
|
78
82
|
|
83
|
+
def extract_exact_value(conf)
|
84
|
+
if conf.has_key?('value')
|
85
|
+
conf['value']
|
86
|
+
else
|
87
|
+
JsonDef::ANY_VALUE
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
79
91
|
end
|
data/lib/rules.rb
CHANGED
@@ -27,12 +27,12 @@ end
|
|
27
27
|
|
28
28
|
class JsonRuleObjectKey
|
29
29
|
|
30
|
-
attr_reader :required, :key, :
|
30
|
+
attr_reader :required, :key, :rule
|
31
31
|
|
32
32
|
def initialize(key)
|
33
33
|
@key = key
|
34
34
|
@required = true
|
35
|
-
@
|
35
|
+
@rule = JsonDef::ANY_TYPE
|
36
36
|
end
|
37
37
|
|
38
38
|
# @todo rename it to optional! - might be more ruby-ist
|
@@ -42,7 +42,7 @@ class JsonRuleObjectKey
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def set_rule(rule)
|
45
|
-
@
|
45
|
+
@rule = rule
|
46
46
|
self
|
47
47
|
end
|
48
48
|
|
@@ -50,42 +50,50 @@ end
|
|
50
50
|
|
51
51
|
class JsonRuleArray < JsonRuleBase
|
52
52
|
|
53
|
+
attr_reader :min_count, :max_count, :unified_rule
|
54
|
+
|
53
55
|
def initialize
|
56
|
+
@min_count = 0
|
57
|
+
@max_count = -1
|
58
|
+
@unified_rule = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_rule(rule)
|
62
|
+
@unified_rule = rule
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_count(min = nil, max = nil)
|
67
|
+
@min_count = min unless min == nil
|
68
|
+
@max_count = max unless max == nil
|
69
|
+
self
|
54
70
|
end
|
55
71
|
|
56
72
|
end
|
57
73
|
|
58
74
|
class JsonRuleSingleValue < JsonRuleBase
|
59
75
|
|
60
|
-
|
76
|
+
attr_reader :value
|
77
|
+
|
78
|
+
def initialize(value = JsonDef::ANY_VALUE)
|
79
|
+
@value = value
|
61
80
|
end
|
62
81
|
|
63
82
|
end
|
64
83
|
|
65
84
|
class JsonRuleNumber < JsonRuleSingleValue
|
66
|
-
|
67
|
-
def initialize
|
68
|
-
end
|
69
|
-
|
70
85
|
end
|
71
86
|
|
72
87
|
class JsonRuleString < JsonRuleSingleValue
|
73
|
-
|
74
|
-
def initialize
|
75
|
-
end
|
76
|
-
|
77
88
|
end
|
78
89
|
|
79
|
-
class
|
80
|
-
|
81
|
-
def initialize
|
82
|
-
end
|
83
|
-
|
90
|
+
class JsonRuleBoolean < JsonRuleSingleValue
|
84
91
|
end
|
85
92
|
|
86
|
-
class
|
93
|
+
class JsonRuleNull < JsonRuleSingleValue
|
87
94
|
|
88
95
|
def initialize
|
96
|
+
super(nil)
|
89
97
|
end
|
90
98
|
|
91
99
|
end
|
data/lib/verify.rb
CHANGED
@@ -3,6 +3,7 @@ require_relative 'rules.rb'
|
|
3
3
|
module JsonDef
|
4
4
|
|
5
5
|
ANY_TYPE = :wildcard
|
6
|
+
ANY_VALUE = :any_value
|
6
7
|
|
7
8
|
def JsonDef.verify(obj, rule)
|
8
9
|
case rule.class.to_s
|
@@ -23,22 +24,31 @@ module JsonDef
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
27
|
+
def JsonDef.verify_single_value(obj, rule)
|
28
|
+
rule.value == JsonDef::ANY_VALUE || rule.value == obj
|
29
|
+
end
|
30
|
+
|
26
31
|
def JsonDef.verify_string(obj, rule)
|
27
32
|
return false unless obj.kind_of?(String)
|
33
|
+
return false unless JsonDef.verify_single_value(obj, rule)
|
28
34
|
true
|
29
35
|
end
|
30
36
|
|
31
37
|
def JsonDef.verify_number(obj, rule)
|
32
38
|
return false unless obj.kind_of?(Numeric)
|
39
|
+
return false unless JsonDef.verify_single_value(obj, rule)
|
33
40
|
true
|
34
41
|
end
|
35
42
|
|
36
43
|
def JsonDef.verify_bool(obj, rule)
|
37
|
-
obj == true || obj == false
|
44
|
+
return false unless obj == true || obj == false
|
45
|
+
return false unless JsonDef.verify_single_value(obj, rule)
|
46
|
+
true
|
38
47
|
end
|
39
48
|
|
40
49
|
def JsonDef.verify_null(obj, rule)
|
41
|
-
obj
|
50
|
+
return false unless JsonDef.verify_single_value(obj, rule)
|
51
|
+
true
|
42
52
|
end
|
43
53
|
|
44
54
|
def JsonDef.verify_object(obj, rule)
|
@@ -49,10 +59,10 @@ module JsonDef
|
|
49
59
|
return true unless obj.has_key?(key_rule.key)
|
50
60
|
|
51
61
|
case
|
52
|
-
when key_rule.
|
62
|
+
when key_rule.rule == JsonDef::ANY_TYPE
|
53
63
|
next
|
54
|
-
when key_rule.
|
55
|
-
return false unless JsonDef.verify(obj[key_rule.key], key_rule.
|
64
|
+
when key_rule.rule.kind_of?(JsonRuleBase)
|
65
|
+
return false unless JsonDef.verify(obj[key_rule.key], key_rule.rule)
|
56
66
|
else
|
57
67
|
return false
|
58
68
|
end
|
@@ -68,6 +78,13 @@ module JsonDef
|
|
68
78
|
|
69
79
|
def JsonDef.verify_array(obj, rule)
|
70
80
|
return false unless obj.kind_of?(Array)
|
81
|
+
return false unless rule.min_count < 0 || obj.count >= rule.min_count
|
82
|
+
return false unless rule.max_count < 0 || obj.count <= rule.max_count
|
83
|
+
|
84
|
+
if rule.unified_rule
|
85
|
+
return false unless obj.all? { |e| JsonDef.verify(e, rule.unified_rule) }
|
86
|
+
end
|
87
|
+
|
71
88
|
true
|
72
89
|
end
|
73
90
|
|