kwalify_to_json_schema 0.2.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0655ac6570cc133063989b7781e8057fe9054ad609a7306411de16d7f3318e20
4
- data.tar.gz: 49e0cb1f1473f3688b4c3a993c0c0c49057581d640d5f0ec0f131152c76e74df
3
+ metadata.gz: b6c69311e6acfcc28fea0bcfaeb66c5378f87a6021bf51e275c55946bf6fe2f4
4
+ data.tar.gz: 1424cced2ddf03ee281266745b9bc5b52a230ac2f99ba58b1cc772eb611ca100
5
5
  SHA512:
6
- metadata.gz: 535ae226d987f26c1d4d8d1348486bfaefe6d37e927a80a4b0db3265018758aea2cc3953010706398c9802fc6c55f8cc083216b1bb3d4a325179aa12ef1baefa
7
- data.tar.gz: fd6257842bc6ec9327152d67f6ae879fc8d5aad8cfc1897d5247b93d6152711a705f53bd244546c8f704f2bcbcf1ce2ccda6455ca3a6ab5c90a8a7710f4d2730
6
+ metadata.gz: 4e517fcf1bef41d0ff88c5d1dab70c7c4322791675cf905598f06aab6320358f9375691f3123584cd9c327f7c8a023e1eaf89f3768f01b68bb801edfeefa7b87
7
+ data.tar.gz: 40efd0e3eb515e9f441cc4a6a0d09bdfca4b2cd993a2a597942f921e27fc463ba72a1294788a1399370c558e66717b9a18f1856ea41b3e8908a88396f9ce806b
@@ -6,6 +6,8 @@ require_relative "kwalify_to_json_schema/kwalify_to_json_schema"
6
6
  require_relative "kwalify_to_json_schema/serialization"
7
7
  require_relative "kwalify_to_json_schema/options"
8
8
  require_relative "kwalify_to_json_schema/limitations"
9
+ require_relative "kwalify_to_json_schema/issue"
10
+ require_relative "kwalify_to_json_schema/issues"
9
11
  require_relative "kwalify_to_json_schema/converter"
10
12
  require_relative "kwalify_to_json_schema/custom_processing"
11
13
  require_relative "kwalify_to_json_schema/cli"
@@ -29,6 +29,9 @@ module KwalifyToJsonSchema
29
29
  option(*Options.cli_option(Options::TITLE))
30
30
  option(*Options.cli_option(Options::DESCRIPTION))
31
31
  option(*Options.cli_option(Options::ISSUES_TO_DESCRIPTION))
32
+ option(*Options.cli_option(Options::ISSUES_TO_STDERR))
33
+ option(*Options.cli_option(Options::SCHEMA_VERSION))
34
+ option(*Options.cli_option(Options::VERBOSE))
32
35
  option Options::CUSTOM_PROCESSING,
33
36
  :type => :string,
34
37
  :desc => <<~DESC
@@ -58,6 +61,8 @@ module KwalifyToJsonSchema
58
61
  :default => false,
59
62
  :desc => "Process files recursively",
60
63
  :long_desc => ""
64
+ option(*Options.cli_option(Options::SCHEMA_VERSION))
65
+ option(*Options.cli_option(Options::VERBOSE))
61
66
  option Options::CUSTOM_PROCESSING,
62
67
  :type => :string,
63
68
  :desc => <<~DESC
@@ -67,11 +72,9 @@ module KwalifyToJsonSchema
67
72
  DESC
68
73
 
69
74
  def convert_dir(kwalify_schema_dir, result_dir)
70
- opts = {
71
- Options::ISSUES_TO_DESCRIPTION => options[:issues_to_description],
72
- Options::CUSTOM_PROCESSING => custom_processing(options),
73
- }
74
-
75
+ opts = options.dup
76
+ opts[Options::CUSTOM_PROCESSING] = custom_processing(options)
77
+
75
78
  path = [kwalify_schema_dir, options["recursive"] ? "**" : nil, "*.yaml"].compact
76
79
  Dir.glob(File.join(*path)).each { |kwalify_schema_file|
77
80
  result_file = File.join(result_dir, File.basename(kwalify_schema_file, File.extname(kwalify_schema_file))) + ".#{options["format"]}"
@@ -11,7 +11,7 @@ module KwalifyToJsonSchema
11
11
  #
12
12
  # File.write("json_schema.json", JSON.pretty_generate(json_schema))
13
13
  class Converter
14
- SCHEMA = "http://json-schema.org/draft-07/schema#"
14
+ SCHEMA = "http://json-schema.org/%s/schema#"
15
15
 
16
16
  # The options given used to initialized the converter
17
17
  attr_reader :options
@@ -24,12 +24,16 @@ module KwalifyToJsonSchema
24
24
  # | :id | string | nil | The JSON schema identifier |
25
25
  # | :title | string | nil | The JSON schema title |
26
26
  # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
27
- # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
27
+ # | :issues_to_description| boolean| false | To append the issues to the JSON schema description |
28
+ # | :issues_to_stderr | boolean| false | To write the issues to standard error output |
28
29
  # | :custom_processing | object | nil | To customize the conversion |
30
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
31
+ # | :verbose | boolean| false | To be verbose when converting |
29
32
  # --
30
- def initialize(options_hash = {})
31
- @options = Options.new(options_hash)
32
- @issues = []
33
+ # @param options {Options} or {Hash}
34
+ def initialize(options = {})
35
+ @options = Options.new(options)
36
+ @issues = Issues.new
33
37
  end
34
38
 
35
39
  # Execute the conversion process
@@ -43,11 +47,12 @@ module KwalifyToJsonSchema
43
47
  if issues.any? && options.issues_to_description?
44
48
  description = json_schema["description"] ||= ""
45
49
  description << "Issues when converting from Kwalify:\n"
46
- description << issues.map { |issue| "* #{issue}" }.join("\n")
50
+ description << issues.descriptions_uniq.map { |description| "* #{description}" }.join("\n")
47
51
  end
48
52
 
49
53
  # Override description if given in option
50
54
  json_schema["description"] = options.description if options.description
55
+ STDERR.puts issues if options.issues_to_stderr?
51
56
 
52
57
  postprocess(json_schema)
53
58
  end
@@ -56,20 +61,22 @@ module KwalifyToJsonSchema
56
61
 
57
62
  def root
58
63
  {
59
- "$schema" => SCHEMA,
60
- "$id" => options.id,
64
+ "$schema" => SCHEMA % options.schema_version,
65
+ "id" => options.id,
61
66
  "title" => options.title,
62
67
  }.reject { |k, v| v.nil? }
63
68
  end
64
69
 
65
70
  # @param target Json schema target
66
71
  # @param kelem Kwalify element
67
- def process(target, kelem)
72
+ def process(target, kelem, path = [])
68
73
 
69
74
  # Add description if available
70
75
  target["description"] = kelem["desc"] if kelem["desc"]
76
+ ktype = kelem["type"]
77
+ path += [ktype] if ktype
71
78
 
72
- case ktype = kelem["type"]
79
+ case ktype
73
80
  when "map"
74
81
  target["type"] = "object"
75
82
  target["additionalProperties"] = false
@@ -78,7 +85,12 @@ module KwalifyToJsonSchema
78
85
  if mapping.is_a? Hash
79
86
  properties = target["properties"] = {}
80
87
  mapping.each_pair { |name, e|
81
- process(properties[name] = {}, e)
88
+ # Ignore mapping default value
89
+ if name == "="
90
+ new_issue path, Limitations::MAPPING_DEFAULT_VALUE_NOT_SUPPORTED
91
+ next
92
+ end
93
+ process(properties[name] = {}, e, path + [name])
82
94
  required << name if e["required"] == true
83
95
  }
84
96
  target["required"] = required unless required.empty?
@@ -105,13 +117,13 @@ module KwalifyToJsonSchema
105
117
  target["type"] = "boolean"
106
118
  when "date"
107
119
  # TODO
108
- new_issue Limitations::DATE_TYPE_NOT_IMPLEMENTED
120
+ new_issue path, Limitations::DATE_TYPE_NOT_IMPLEMENTED
109
121
  when "time"
110
122
  # TODO
111
- new_issue Limitations::TIME_TYPE_NOT_IMPLEMENTED
123
+ new_issue path, Limitations::TIME_TYPE_NOT_IMPLEMENTED
112
124
  when "timestamp"
113
125
  # TODO
114
- new_issue Limitations::TIMESTAMP_TYPE_NOT_IMPLEMENTED
126
+ new_issue path, Limitations::TIMESTAMP_TYPE_NOT_IMPLEMENTED
115
127
  when "scalar"
116
128
  # Use one of
117
129
  target["oneOf"] = [
@@ -123,7 +135,7 @@ module KwalifyToJsonSchema
123
135
  when "any"
124
136
  # Don't put type
125
137
  else
126
- new_issue("Unknown Kwalify type #{ktype}")
138
+ new_issue(path, "Unknown Kwalify type #{ktype}")
127
139
  end
128
140
 
129
141
  target["enum"] = kelem["enum"] if kelem["enum"]
@@ -154,7 +166,7 @@ module KwalifyToJsonSchema
154
166
  end
155
167
  end
156
168
 
157
- new_issue UNIQUE_NOT_SUPPORTED if kelem["unique"]
169
+ new_issue path, Limitations::UNIQUE_NOT_SUPPORTED if kelem["unique"]
158
170
 
159
171
  target
160
172
  end
@@ -171,8 +183,8 @@ module KwalifyToJsonSchema
171
183
  ep.postprocess(json_schema)
172
184
  end
173
185
 
174
- def new_issue(description)
175
- @issues << description
186
+ def new_issue(path, description)
187
+ @issues << Issue.new(path, description)
176
188
  end
177
189
  end
178
190
  end
@@ -0,0 +1,16 @@
1
+ module KwalifyToJsonSchema
2
+ # Used to represent issues encoutered while converting
3
+ class Issue
4
+ attr_reader :path
5
+ attr_reader :description
6
+
7
+ def initialize(path, description)
8
+ @path = path
9
+ @description = description
10
+ end
11
+
12
+ def to_s
13
+ "Issue #{path.join "/"}: #{description}"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module KwalifyToJsonSchema
2
+ # Used to hold issues encoutered while converting
3
+ class Issues < Array
4
+
5
+ # Get an array with only one instance of each description
6
+ def descriptions_uniq
7
+ map(&:description).uniq
8
+ end
9
+ end
10
+ end
@@ -10,12 +10,18 @@ module KwalifyToJsonSchema
10
10
  # | :id | string | nil | The JSON schema identifier |
11
11
  # | :title | string | nil | The JSON schema title |
12
12
  # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
13
- # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
13
+ # | :issues_to_description| boolean| false | To append the issues to the JSON schema description |
14
+ # | :issues_to_stderr | boolean| false | To write the issues to standard error output |
14
15
  # | :custom_processing | object | nil | To customize the conversion |
16
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
17
+ # | :verbose | boolean| false | To be verbose when converting |
15
18
  # --
16
19
  # @param source Path to Kwalify YAML schema
17
20
  # @param dest Path to resulting JSON schema
18
21
  def self.convert_file(source, dest, options = {})
22
+ options = Options.new(options)
23
+ puts "Converting file://#{File.expand_path source } to file://#{File.expand_path dest}" if options.verbose?
24
+
19
25
  # Get a converter
20
26
  converter = Converter.new(options)
21
27
  # Convert
@@ -33,14 +39,18 @@ module KwalifyToJsonSchema
33
39
  # | :id | string | nil | The JSON schema identifier |
34
40
  # | :title | string | nil | The JSON schema title |
35
41
  # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
36
- # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
42
+ # | :issues_to_description| boolean| false | To append the issues to the JSON schema description |
43
+ # | :issues_to_stderr | boolean| false | To write the issues to standard error output |
37
44
  # | :custom_processing | object | nil | To customize the conversion |
45
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
46
+ # | :verbose | boolean| false | To be verbose when converting |
38
47
  # --
39
48
  # @param kwalify_schema Kwalify schema as YAML or JSON
40
49
  # @param source_format format of the source schema
41
50
  # @param dest_format format of the destination schema
42
51
  # @param options
43
52
  def self.convert_string(kwalify_schema, source_format = "yaml", dest_format = "json", options = {})
53
+ options = Options.new(options)
44
54
  # Get a converter
45
55
  converter = Converter.new(options)
46
56
  # Convert
@@ -5,5 +5,6 @@ module KwalifyToJsonSchema
5
5
  TIME_TYPE_NOT_IMPLEMENTED = "Kwalify 'time' type is not supported and is ignored"
6
6
  TIMESTAMP_TYPE_NOT_IMPLEMENTED = "Kwalify 'timestamp' type is not supported and is ignored"
7
7
  UNIQUE_NOT_SUPPORTED = "Kwalify 'unique' is not supported by JSON Schema and is ignored"
8
+ MAPPING_DEFAULT_VALUE_NOT_SUPPORTED = "Kwalify mapping default value is not supported by JSON Schema and is ignored"
8
9
  end
9
10
  end
@@ -7,22 +7,32 @@ module KwalifyToJsonSchema
7
7
  # | :id | string | nil | The JSON schema identifier |
8
8
  # | :title | string | nil | The JSON schema title |
9
9
  # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
10
- # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
10
+ # | :issues_to_description| boolean| false | To append the issues to the JSON schema description |
11
+ # | :issues_to_stderr | boolean| false | To write the issues to standard error output |
11
12
  # | :custom_processing | object | nil | To customize the conversion |
13
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
14
+ # | :verbose | boolean| false | To be verbose when converting |
12
15
  # --
13
16
  DECLARATION = %q(
14
17
  ID # The JSON schema identifier [string] (nil)
15
18
  TITLE # The JSON schema title [string] (nil)
16
19
  DESCRIPTION # The JSON schema description. If not given the Kwalify description will be used if present [string] (nil)
17
- ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [boolean] (false)
20
+ ISSUES_TO_DESCRIPTION # To append the issues to the JSON schema description [boolean] (false)
21
+ ISSUES_TO_STDERR # To write the issues to standard error output [boolean] (false)
18
22
  CUSTOM_PROCESSING # To customize the conversion [object] (nil)
23
+ SCHEMA_VERSION # JSON schema version. Changing this value only change the value of $schema field[string] ("draft-04")
24
+ VERBOSE # To be verbose when converting [boolean] (false)
19
25
  )
20
26
 
21
27
  # The options as Hash
22
28
  attr_reader :options_hash
23
29
 
24
- def initialize(options_hash)
25
- @options_hash = options_hash
30
+ def initialize(options)
31
+ @options_hash = options.is_a?(Options) ? options.options_hash : options
32
+ end
33
+
34
+ def to_s
35
+ YAML.dump("Options" => options_hash)
26
36
  end
27
37
 
28
38
  # Parse options declaration text and give an array of Hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kwalify_to_json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Gamot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 12.3.0
83
- description: Allows to convert Kwalify schemas to JSON schemas Draft 7
83
+ description: Allows to convert Kwalify schemas to JSON schemas
84
84
  email: ''
85
85
  executables:
86
86
  - kwalify_to_json_schema
@@ -92,6 +92,8 @@ files:
92
92
  - lib/kwalify_to_json_schema/cli.rb
93
93
  - lib/kwalify_to_json_schema/converter.rb
94
94
  - lib/kwalify_to_json_schema/custom_processing.rb
95
+ - lib/kwalify_to_json_schema/issue.rb
96
+ - lib/kwalify_to_json_schema/issues.rb
95
97
  - lib/kwalify_to_json_schema/kwalify_to_json_schema.rb
96
98
  - lib/kwalify_to_json_schema/limitations.rb
97
99
  - lib/kwalify_to_json_schema/options.rb
@@ -117,7 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
119
  - !ruby/object:Gem::Version
118
120
  version: '0'
119
121
  requirements: []
120
- rubygems_version: 3.1.2
122
+ rubyforge_project:
123
+ rubygems_version: 2.7.6
121
124
  signing_key:
122
125
  specification_version: 4
123
126
  summary: Kwalify schemas to JSON schemas conversion