json-schema-generator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,10 @@
|
|
1
|
+
require 'json/schema_generator/statement_group'
|
2
|
+
|
1
3
|
module JSON
|
2
4
|
class SchemaGenerator
|
5
|
+
DRAFT3 = 'draft-03'
|
6
|
+
DRAFT4 = 'draft-04'
|
7
|
+
|
3
8
|
class << self
|
4
9
|
def generate name, data, version
|
5
10
|
generator = JSON::SchemaGenerator.new name, version
|
@@ -10,92 +15,115 @@ module JSON
|
|
10
15
|
def initialize name, version = 'draft3'
|
11
16
|
@buffer = StringIO.new
|
12
17
|
@name = name
|
13
|
-
@version =
|
14
|
-
@version =
|
18
|
+
@version = DRAFT3 if version == 'draft3'
|
19
|
+
@version = DRAFT4 if version == 'draft4'
|
15
20
|
end
|
16
21
|
|
17
22
|
def generate raw_data
|
18
|
-
# Write header
|
19
|
-
@buffer.puts "{"
|
20
|
-
@buffer.puts "\"$schema\": \"http://json-schema.org/#{@version}/schema#\","
|
21
|
-
@buffer.print '"description": "Generated from '
|
22
|
-
@buffer.print @name
|
23
|
-
@buffer.print " with shasum "
|
24
|
-
@buffer.print Digest::SHA1.hexdigest raw_data
|
25
|
-
@buffer.puts '",'
|
26
|
-
|
27
|
-
#
|
28
23
|
data = JSON.load(raw_data)
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
statement_group = StatementGroup.new
|
25
|
+
statement_group.add "\"$schema\": \"http://json-schema.org/#{@version}/schema#\""
|
26
|
+
statement_group.add "\"description\": \"Generated from #{@name} with shasum #{Digest::SHA1.hexdigest raw_data}\""
|
27
|
+
create_hash(statement_group, data, detect_required(data))
|
28
|
+
@buffer.puts statement_group
|
32
29
|
result
|
33
30
|
end
|
34
31
|
|
35
32
|
protected
|
36
33
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
34
|
+
def create_primitive(statement_group, key, value, required_keys)
|
35
|
+
if required_keys.nil?
|
36
|
+
required = true
|
37
|
+
else
|
38
|
+
required = required_keys.include? key
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
buffer = StringIO.new
|
43
|
-
buffer.puts "\"#{key}\": {"
|
44
|
-
case value
|
45
|
-
when NilClass
|
41
|
+
type = case value
|
46
42
|
when TrueClass, FalseClass
|
47
|
-
|
48
|
-
buffer.puts '"required": true'
|
49
|
-
|
43
|
+
"boolean"
|
50
44
|
when String
|
51
|
-
|
52
|
-
buffer.puts '"required": true'
|
53
|
-
|
45
|
+
"string"
|
54
46
|
when Integer
|
55
|
-
|
56
|
-
buffer.puts '"required": true'
|
57
|
-
|
47
|
+
"integer"
|
58
48
|
when Float
|
59
|
-
|
60
|
-
|
49
|
+
"number"
|
50
|
+
else
|
51
|
+
raise "Unknown Primitive Type for #{key}! #{value.class}"
|
52
|
+
end
|
53
|
+
|
54
|
+
statement_group.add "\"type\": \"#{type}\""
|
55
|
+
statement_group.add "\"required\": #{required}" if @version == DRAFT3
|
56
|
+
# statement_group.add "\"default\": #{value.inspect}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_values(key, value, required_keys = nil, in_array = false)
|
60
|
+
statement_group = StatementGroup.new key
|
61
|
+
# buffer.puts "\"#{key}\": {"
|
62
|
+
case value
|
63
|
+
when NilClass
|
64
|
+
when TrueClass, FalseClass, String, Integer, Float
|
65
|
+
create_primitive(statement_group, key, value, required_keys)
|
61
66
|
when Array
|
62
|
-
|
67
|
+
create_array(statement_group, value, detect_required(value))
|
63
68
|
when Hash
|
64
|
-
|
69
|
+
if in_array
|
70
|
+
create_hash(statement_group, value, required_keys)
|
71
|
+
else
|
72
|
+
create_hash(statement_group, value, detect_required(value))
|
73
|
+
end
|
65
74
|
else
|
66
75
|
raise "Unknown Type for #{key}! #{value.class}"
|
67
76
|
end
|
68
|
-
|
69
|
-
buffer.string
|
77
|
+
statement_group
|
70
78
|
end
|
71
79
|
|
72
|
-
def create_hash(data)
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
80
|
+
def create_hash(statement_group, data, required_keys)
|
81
|
+
# statement_group = StatementGroup.new
|
82
|
+
statement_group.add '"type": "object"'
|
83
|
+
statement_group.add '"required": true' if @version == DRAFT3
|
84
|
+
if @version == DRAFT4
|
85
|
+
required_keys ||= []
|
86
|
+
required_string = required_keys.map(&:inspect).join ', '
|
87
|
+
statement_group.add "\"required\": [#{required_string}]"
|
79
88
|
end
|
80
|
-
|
81
|
-
|
89
|
+
statement_group.add create_hash_properties data, required_keys
|
90
|
+
statement_group
|
91
|
+
end
|
82
92
|
|
83
|
-
|
93
|
+
def create_hash_properties(data, required_keys)
|
94
|
+
statement_group = StatementGroup.new "properties"
|
95
|
+
data.collect do |k,v|
|
96
|
+
statement_group.add create_values k, v, required_keys
|
97
|
+
end
|
98
|
+
statement_group
|
84
99
|
end
|
85
100
|
|
86
|
-
def create_array(data)
|
87
|
-
|
88
|
-
|
89
|
-
|
101
|
+
def create_array(statement_group, data, required_keys)
|
102
|
+
# statement_group = StatementGroup.new
|
103
|
+
statement_group.add '"type": "array"'
|
104
|
+
statement_group.add '"required": true' if @version == DRAFT3
|
90
105
|
if data.size == 0
|
91
|
-
|
106
|
+
statement_group.add '"minItems": 0'
|
92
107
|
else
|
93
|
-
|
108
|
+
statement_group.add '"minItems": 1'
|
94
109
|
end
|
95
|
-
|
96
|
-
|
110
|
+
statement_group.add '"uniqueItems": true'
|
111
|
+
statement_group.add create_values("items", data.first, required_keys, true)
|
97
112
|
|
98
|
-
|
113
|
+
statement_group
|
114
|
+
end
|
115
|
+
|
116
|
+
def detect_required(collection)
|
117
|
+
begin
|
118
|
+
required_keys = collection.map(&:keys).inject{|required,keys| required & keys }
|
119
|
+
rescue
|
120
|
+
if collection.respond_to? :keys
|
121
|
+
required_keys = collection.keys
|
122
|
+
else
|
123
|
+
required_keys = nil
|
124
|
+
end
|
125
|
+
end
|
126
|
+
required_keys
|
99
127
|
end
|
100
128
|
|
101
129
|
def result
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module JSON
|
2
|
+
class SchemaGenerator
|
3
|
+
class StatementGroup
|
4
|
+
def initialize key = nil
|
5
|
+
@key = key
|
6
|
+
@statements = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def add statement
|
10
|
+
@statements << statement
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
buffer = StringIO.new
|
15
|
+
if @key.nil?
|
16
|
+
buffer.puts "{"
|
17
|
+
else
|
18
|
+
buffer.puts "\"#{@key}\": {"
|
19
|
+
end
|
20
|
+
buffer.puts @statements.join ', '
|
21
|
+
buffer.puts "}"
|
22
|
+
buffer.string
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -4,8 +4,9 @@ class JSON::SchemaGeneratorCLI
|
|
4
4
|
end
|
5
5
|
|
6
6
|
def execute!
|
7
|
-
file = ARGV
|
8
|
-
|
7
|
+
file = ARGV.shift
|
8
|
+
version = ARGV.shift || 'draft3'
|
9
|
+
schema = JSON.parse(JSON::SchemaGenerator.generate file, File.read(file), version)
|
9
10
|
@stdout.puts JSON.pretty_generate schema
|
10
11
|
@kernel.exit(0)
|
11
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- json-schema-generator.gemspec
|
127
127
|
- lib/json-schema-generator.rb
|
128
128
|
- lib/json/schema_generator.rb
|
129
|
+
- lib/json/schema_generator/statement_group.rb
|
129
130
|
- lib/json/schema_generator/version.rb
|
130
131
|
- lib/json/schema_generator_cli.rb
|
131
132
|
homepage: ''
|