jsondef 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config_reader.rb +15 -3
  3. data/lib/rules.rb +26 -18
  4. data/lib/verify.rb +22 -5
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27bfc9c6479bc3349fe6297b555e6fac0aa33092
4
- data.tar.gz: 38d3754d0e798d338dd8ceb7243b0b56d7371438
3
+ metadata.gz: 2756f40c259d50404177ef9ea8491267fd606632
4
+ data.tar.gz: 04cc9e350319ab4aa5232a1b331883ae06dc77e7
5
5
  SHA512:
6
- metadata.gz: 00eecc2fbdf266d3b618c6746ab2457df4a8dd50ea5a0abc8dd5bb586fa300764adc18d9a7a5bccee2374b7dc323ec534af8a64584057140694492563dfeda58
7
- data.tar.gz: 122c33e13f141c29b0e966fd66d87f1297c7cfefe70567cfa85a655a605d64da6e68658184248d41feebbb4de95c6408c0a0aef9a4b4a819bbdda8ea6ecba86a
6
+ metadata.gz: fac54aa304dee05dc45a9c3f88d0d094c996bf8264014d7f1e124d90999eacf2b630d31ec2d07b97da5a2097a476dbd235a688d997705e38e9ea883cda451af4
7
+ data.tar.gz: 40c14afd5032a0e1ce96c23011b2168b1d536657d016633ef2d018b483b47a973b4671aa35ed39afef34ce65049332240ee1b4c665d73781aaa3a299a2f0fe57
@@ -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
@@ -27,12 +27,12 @@ end
27
27
 
28
28
  class JsonRuleObjectKey
29
29
 
30
- attr_reader :required, :key, :value
30
+ attr_reader :required, :key, :rule
31
31
 
32
32
  def initialize(key)
33
33
  @key = key
34
34
  @required = true
35
- @value = JsonDef::ANY_TYPE
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
- @value = rule
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
- def initialize
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 JsonRuleNull < JsonRuleSingleValue
80
-
81
- def initialize
82
- end
83
-
90
+ class JsonRuleBoolean < JsonRuleSingleValue
84
91
  end
85
92
 
86
- class JsonRuleBoolean < JsonRuleSingleValue
93
+ class JsonRuleNull < JsonRuleSingleValue
87
94
 
88
95
  def initialize
96
+ super(nil)
89
97
  end
90
98
 
91
99
  end
@@ -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 == nil
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.value == JsonDef::ANY_TYPE
62
+ when key_rule.rule == JsonDef::ANY_TYPE
53
63
  next
54
- when key_rule.value.kind_of?(JsonRuleBase)
55
- return false unless JsonDef.verify(obj[key_rule.key], key_rule.value)
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
 
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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Arato