jsondef 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jsondef.rb +127 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8df428945802e90fdf27e24e2d576aacdfec82d
4
+ data.tar.gz: b6e23cb1afc34c5c4cf6f5bb6589b937472bca3a
5
+ SHA512:
6
+ metadata.gz: e718fee725f7dca9c35b839d864a737d1357dcf6007f75bff49ade7fd6e42a6f6b235eb7f7bdee7a1189e3b0379e81002b211a284a682ab9078a455932f20deb
7
+ data.tar.gz: 41e45435df65bede5ca72820c00e6d993b3c8cc23b0bf92411419a44610964d259f914d1b3118e64b8bc5e6c7ae3e5f0eeeaa35ce26f70eb2d3b1e60ed7eb7e2
data/lib/jsondef.rb ADDED
@@ -0,0 +1,127 @@
1
+ module JsonDef
2
+
3
+ def JsonDef.verify(obj, rule)
4
+ case rule.class.to_s
5
+ when 'JsonRuleObject'
6
+ JsonDef.verify_object(obj, rule)
7
+ else
8
+ raise 'Unknown rule at [verify] level'
9
+ end
10
+ end
11
+
12
+ def JsonDef.verify_value_type(value, type)
13
+ case type
14
+ when :object
15
+ value.kind_of?(Hash)
16
+ when :number
17
+ value.kind_of?(Numeric)
18
+ when :string
19
+ value.kind_of?(String)
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
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.kind_of?(Symbol)
39
+ return false unless JsonDef.verify_value_type(obj[key_rule.key], key_rule.value)
40
+
41
+ when key_rule.value.kind_of?(JsonRuleObject)
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
+
47
+ else
48
+ # Expected an exact value given.
49
+ return false unless obj[key_rule.key] == key_rule.value
50
+ end
51
+ end
52
+
53
+ # @todo In case of strict, check if there are other not-allowed keys.
54
+ if rule.strict?
55
+ obj.each { |k, v| return false unless rule.keys.member?(k) }
56
+ end
57
+
58
+ true
59
+ end
60
+
61
+ def JsonDef.verify_array(obj, rule)
62
+ return false if obj.kind_of?(Array)
63
+ true
64
+ end
65
+
66
+ end
67
+
68
+ class JsonRuleObject
69
+
70
+ attr_reader :key_rules, :keys
71
+
72
+ def initialize()
73
+ @strict = false
74
+ @key_rules = []
75
+ @keys = []
76
+ end
77
+
78
+ def strict?
79
+ @strict
80
+ end
81
+
82
+ def set_strict
83
+ @strict = true
84
+ self
85
+ end
86
+
87
+ def add_key_rule(rule)
88
+ @key_rules.push(rule)
89
+ @keys.push(rule.key)
90
+ self
91
+ end
92
+
93
+ end
94
+
95
+ class JsonRuleArray
96
+
97
+ def initialize
98
+ end
99
+
100
+ end
101
+
102
+ class JsonRuleObjectKey
103
+
104
+ attr_reader :required, :key, :value
105
+
106
+ def initialize(key)
107
+ @key = key
108
+ @required = true
109
+ @value = :wildcard
110
+ end
111
+
112
+ def set_optional()
113
+ @required = false
114
+ self
115
+ end
116
+
117
+ def set_value_type(type)
118
+ @value = type
119
+ self
120
+ end
121
+
122
+ def set_value_rule(rule)
123
+ @value = rule
124
+ self
125
+ end
126
+
127
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsondef
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Arato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Can create JSON descriptor and verify if a given object satisfy it.
14
+ email: it.arato@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/jsondef.rb
20
+ homepage: http://itarato.com
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.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: JSON definition and verification lib.
44
+ test_files: []