json-schema-generator 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjRkOWJiNTNmMDM2ZmM2YzE2YmJiZWE4ZmFkNzg5MzllMGQ5Y2IyOA==
5
+ data.tar.gz: !binary |-
6
+ NDM3NWE3NTc4OTIwN2ZjNTJlMjJiYTg5YWRiMTczYTJkN2ZiOTc5NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZTE3YWI4MmQ0YjAxMTNhZGMxZGJkNzRlNWZiN2Q2ODViMWQ4YjM5MjcyMWZj
10
+ YjE3ZmIxNjVmY2U2Yzc5MDM3M2VhMTQ4ZDlmMDYzM2NkOTUzMmQ0ZmY4YzIz
11
+ N2QzODUzYzhjOTRkYzlkYjFkY2NjN2NjYjMyMWRhYjVkNzQwMDk=
12
+ data.tar.gz: !binary |-
13
+ MDNlMDU2M2E5YWQ0MzdhNTVhMDA2MjQ5NTczZDZlMjQ2NDMxYjZlN2Y5NTQ0
14
+ MWFjY2MwZmRiOTY2NjVhMDEzNTEwN2YxMjA5YTVmN2YwZDNlMTViYTNkMGFk
15
+ NzFmNzM2ZTg0MDE3YzE3YjgwMDg4N2ZkNGEzYmMyZGE2MDE5ZDc=
data/README.md CHANGED
@@ -48,7 +48,7 @@ JSON::SchemaGenerate has the following features or assumptions for generating "b
48
48
  * **Defaults**: Can generate default values.
49
49
  * **Descriptions**: Can generate a description indicating where the schema came from.
50
50
  * **Features/Assumptions**:
51
- * **Detecting optional properties:** if you have an array of hashes, then will detect which hash keys exist in every instance of the hash and which ones only exist in some, and use this data to decide if the key is required or optional in the schema.
51
+ * **Detecting optional properties:** if you are using arrays (even arrays with complex nested types), the generator will use all available data to figure detect if a field is optional.
52
52
  * **Assume required:** in all other cases, I assume everything I find in the sample is required. I believe it is better to generate a schema that is too strict than too lenient. It is easy to review and fix false negatives, by updating the schema to mark those items as optional. A false positive will go unnoticed and will not point you towards a solution.
53
53
  * **Detect types:** I detect objects, arrays, strings, integers, numbers and booleans.
54
54
 
@@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "jsonpath"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
23
25
  spec.add_development_dependency "rspec"
24
26
  spec.add_development_dependency "json-schema"
25
- spec.add_development_dependency "jsonpath"
26
27
  spec.add_development_dependency "travis"
27
28
  end
@@ -1,4 +1,5 @@
1
1
  require 'json/schema_generator/statement_group'
2
+ require 'json/schema_generator/brute_force_required_search'
2
3
 
3
4
  module JSON
4
5
  class SchemaGenerator
@@ -33,6 +34,8 @@ module JSON
33
34
 
34
35
  def generate raw_data
35
36
  data = JSON.load(raw_data)
37
+ @brute_search = BruteForceRequiredSearch.new data
38
+
36
39
  statement_group = StatementGroup.new
37
40
  statement_group.add "\"$schema\": \"http://json-schema.org/#{@version}/schema#\""
38
41
  statement_group.add "\"description\": \"Generated from #{@name} with shasum #{Digest::SHA1.hexdigest raw_data}\""
@@ -103,7 +106,9 @@ module JSON
103
106
  def create_hash_properties(data, required_keys)
104
107
  statement_group = StatementGroup.new "properties"
105
108
  data.collect do |k,v|
109
+ @brute_search.push k,v
106
110
  statement_group.add create_values k, v, required_keys
111
+ @brute_search.pop
107
112
  end
108
113
  statement_group
109
114
  end
@@ -124,7 +129,7 @@ module JSON
124
129
 
125
130
  def detect_required(collection)
126
131
  begin
127
- required_keys = collection.map(&:keys).inject{|required,keys| required & keys }
132
+ required_keys = @brute_search.find_required
128
133
  rescue
129
134
  if collection.respond_to? :keys
130
135
  required_keys = collection.keys
@@ -0,0 +1,48 @@
1
+ require 'jsonpath'
2
+
3
+ module JSON
4
+ class SchemaGenerator
5
+ class BruteForceRequiredSearch
6
+ def initialize(data)
7
+ @data = data.dup
8
+ @json_path = ['$']
9
+ end
10
+
11
+ def push(key, value)
12
+ if value.is_a? Array
13
+ @json_path.push "#{key}[*]"
14
+ else
15
+ @json_path.push key
16
+ end
17
+ end
18
+
19
+ def pop
20
+ @json_path.pop
21
+ end
22
+
23
+ def current_path
24
+ @json_path.join '.'
25
+ end
26
+
27
+ def search_path search_key
28
+ current_path.gsub(/\[\*\]$/, "[?(@.#{search_key})]")
29
+ end
30
+
31
+ def required? child_key
32
+ JsonPath.new(search_path(child_key)).on(@data).count == JsonPath.new(current_path).on(@data).count
33
+ end
34
+
35
+ def child_keys
36
+ JsonPath.new(current_path).on(@data).map(&:keys).flatten.uniq
37
+ end
38
+
39
+ def find_required
40
+ required = []
41
+ child_keys.each do |child_key|
42
+ required << child_key if required? child_key
43
+ end
44
+ required
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  class SchemaGenerator
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,52 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
5
- prerelease:
4
+ version: 0.0.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - Max Lincoln
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-08 00:00:00.000000000 Z
11
+ date: 2013-11-18 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: bundler
14
+ name: jsonpath
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - ! '>='
20
18
  - !ruby/object:Gem::Version
21
- version: '1.3'
22
- type: :development
19
+ version: '0'
20
+ type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - ! '>='
28
25
  - !ruby/object:Gem::Version
29
- version: '1.3'
26
+ version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
- name: rake
28
+ name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ~>
36
32
  - !ruby/object:Gem::Version
37
- version: '0'
33
+ version: '1.3'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ~>
44
39
  - !ruby/object:Gem::Version
45
- version: '0'
40
+ version: '1.3'
46
41
  - !ruby/object:Gem::Dependency
47
- name: rspec
42
+ name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,15 +48,13 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
- name: json-schema
56
+ name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,15 +62,13 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
- name: jsonpath
70
+ name: json-schema
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: travis
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ! '>='
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ! '>='
108
95
  - !ruby/object:Gem::Version
@@ -126,6 +113,7 @@ files:
126
113
  - json-schema-generator.gemspec
127
114
  - lib/json-schema-generator.rb
128
115
  - lib/json/schema_generator.rb
116
+ - lib/json/schema_generator/brute_force_required_search.rb
129
117
  - lib/json/schema_generator/statement_group.rb
130
118
  - lib/json/schema_generator/version.rb
131
119
  - lib/json/schema_generator_cli.rb
@@ -135,32 +123,25 @@ files:
135
123
  homepage: ''
136
124
  licenses:
137
125
  - MIT
126
+ metadata: {}
138
127
  post_install_message:
139
128
  rdoc_options: []
140
129
  require_paths:
141
130
  - lib
142
131
  required_ruby_version: !ruby/object:Gem::Requirement
143
- none: false
144
132
  requirements:
145
133
  - - ! '>='
146
134
  - !ruby/object:Gem::Version
147
135
  version: '0'
148
- segments:
149
- - 0
150
- hash: 1712601133400832444
151
136
  required_rubygems_version: !ruby/object:Gem::Requirement
152
- none: false
153
137
  requirements:
154
138
  - - ! '>='
155
139
  - !ruby/object:Gem::Version
156
140
  version: '0'
157
- segments:
158
- - 0
159
- hash: 1712601133400832444
160
141
  requirements: []
161
142
  rubyforge_project:
162
- rubygems_version: 1.8.23
143
+ rubygems_version: 2.1.11
163
144
  signing_key:
164
- specification_version: 3
145
+ specification_version: 4
165
146
  summary: A very basic json-schema generator
166
147
  test_files: []