kwalify_to_json_schema 0.1.3 → 0.5.0

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: 18d110d9d28e8bd0a1231dd65df8f8f52063c8bf01e96069a74d577c717ec886
4
- data.tar.gz: 1fd58dd04f1e30d48540f7b55a7f10d75aafd186e75e6ce0b9b6f932ec1f5b6d
3
+ metadata.gz: e93f6522625fde6b85d8f46a719a0a4ed2cec61ca281511094e32f1f8b8b8941
4
+ data.tar.gz: 47e10842e281ac6fb393e7c97dfa17ef574cfcc262507cc6e649eeec1d5e39a5
5
5
  SHA512:
6
- metadata.gz: a5989d6935db5941cb5950e4d8db007fbe1a54bbca5b89e48b27f4e67f3b0747a52d0f95b67ca3c11bb57dcbf07d62a047fff57ef75e7342ffbf5c4652a0d3e2
7
- data.tar.gz: 2acbfa4dd347768e076c02629de054b4c9ddfc5adbbb931ad0fdad9354e105f039e24658ffb1781204a55f1b1032412bc497535a00b323ec3f053bed96d3ec0b
6
+ metadata.gz: 46e85a186ef82f070f7d370f7ac33cc3c8ab68a866cb3fd342650b4d3d42c7dcce3d42072cfd9597e8accddbd979fa50421066842693fff6e735cb325773f0cd
7
+ data.tar.gz: '0799db65393f0084351df69fd34ceef0c3382d8a82ec3ef710c8b1ef041187bf9dd44201ac4ca25abace773ee07a0610048971d4f8b23b190db5fea8002ed87d'
@@ -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"
@@ -25,11 +25,14 @@ module KwalifyToJsonSchema
25
25
 
26
26
  desc "convert KWALIFY_SCHEMA_FILE, RESULT_FILE",
27
27
  "Convert a Kwalify schema file to a JSON schema file. The result file extension will decide the format: .json or .yaml"
28
- option :issues_to_description,
29
- :type => :boolean,
30
- :default => false,
31
- :desc => "Will append any conversion issue to the schema description"
32
- option :custom_processing,
28
+ option(*Options.cli_option(Options::ID))
29
+ option(*Options.cli_option(Options::TITLE))
30
+ option(*Options.cli_option(Options::DESCRIPTION))
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))
35
+ option Options::CUSTOM_PROCESSING,
33
36
  :type => :string,
34
37
  :desc => <<~DESC
35
38
  Allows to provide a pre/post processing file on handled schemas.
@@ -38,10 +41,8 @@ module KwalifyToJsonSchema
38
41
  DESC
39
42
 
40
43
  def convert(kwalify_schema_file, result_file)
41
- opts = {
42
- Options::ISSUES_TO_DESCRIPTION => options[:issues_to_description],
43
- Options::CUSTOM_PROCESSING => custom_processing(options),
44
- }
44
+ opts = options.dup
45
+ opts[Options::CUSTOM_PROCESSING] = custom_processing(options)
45
46
  KwalifyToJsonSchema.convert_file(kwalify_schema_file, result_file, opts)
46
47
  end
47
48
 
@@ -49,10 +50,7 @@ module KwalifyToJsonSchema
49
50
 
50
51
  desc "convert_dir KWALIFY_SCHEMA_DIR, RESULT_DIR",
51
52
  "Convert all the Kwalify schema from a directory to a JSON schema"
52
- option :issues_to_description,
53
- :type => :boolean,
54
- :default => false,
55
- :desc => "Will append any conversion issue to the schema description"
53
+ option(*Options.cli_option(:issues_to_description))
56
54
  option :format,
57
55
  :type => :string,
58
56
  :enum => ["json", "yaml"],
@@ -63,7 +61,9 @@ module KwalifyToJsonSchema
63
61
  :default => false,
64
62
  :desc => "Process files recursively",
65
63
  :long_desc => ""
66
- option :custom_processing,
64
+ option(*Options.cli_option(Options::SCHEMA_VERSION))
65
+ option(*Options.cli_option(Options::VERBOSE))
66
+ option Options::CUSTOM_PROCESSING,
67
67
  :type => :string,
68
68
  :desc => <<~DESC
69
69
  Allows to provide a pre/post processing file on handled schemas.
@@ -72,11 +72,9 @@ module KwalifyToJsonSchema
72
72
  DESC
73
73
 
74
74
  def convert_dir(kwalify_schema_dir, result_dir)
75
- opts = {
76
- Options::ISSUES_TO_DESCRIPTION => options[:issues_to_description],
77
- Options::CUSTOM_PROCESSING => custom_processing(options),
78
- }
79
-
75
+ opts = options.dup
76
+ opts[Options::CUSTOM_PROCESSING] = custom_processing(options)
77
+
80
78
  path = [kwalify_schema_dir, options["recursive"] ? "**" : nil, "*.yaml"].compact
81
79
  Dir.glob(File.join(*path)).each { |kwalify_schema_file|
82
80
  result_file = File.join(result_dir, File.basename(kwalify_schema_file, File.extname(kwalify_schema_file))) + ".#{options["format"]}"
@@ -98,7 +96,7 @@ module KwalifyToJsonSchema
98
96
  begin
99
97
  processing_class = Object.const_get :CustomProcessing
100
98
  custom_processing = processing_class.new
101
- rescue NameError => e
99
+ rescue NameError
102
100
  raise "The 'CustomProcessing' module must be defined in #{pf}"
103
101
  end
104
102
  end
@@ -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
@@ -19,17 +19,21 @@ module KwalifyToJsonSchema
19
19
  attr_reader :issues
20
20
 
21
21
  # Converter options:
22
- # | Name | Type | Default value| Description |
23
- # |-----------------------|--------|--------------|-----------------------------------------------------|
24
- # | :id | String | nil | The JSON schema identifier |
25
- # | :title | String | nil | The JSON schema title |
26
- # | :description | String | nil | The JSON schema description |
27
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
28
- # | :custom_processing | Object | nil | To customize the conversion |
22
+ # | Name | Type | Default value| Description |
23
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
24
+ # | :id | string | nil | The JSON schema identifier |
25
+ # | :title | string | nil | The JSON schema title |
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 |
28
+ # | :issues_to_stderr | boolean| false | To write the issuses standard error output |
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,9 +47,13 @@ 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
 
53
+ # Override description if given in option
54
+ json_schema["description"] = options.description if options.description
55
+ STDERR.puts issues if options.issues_to_stderr?
56
+
49
57
  postprocess(json_schema)
50
58
  end
51
59
 
@@ -53,21 +61,22 @@ module KwalifyToJsonSchema
53
61
 
54
62
  def root
55
63
  {
56
- "$schema" => SCHEMA,
57
- "$id" => options.id,
64
+ "$schema" => SCHEMA % options.schema_version,
65
+ "id" => options.id,
58
66
  "title" => options.title,
59
- "description" => options.description,
60
67
  }.reject { |k, v| v.nil? }
61
68
  end
62
69
 
63
70
  # @param target Json schema target
64
71
  # @param kelem Kwalify element
65
- def process(target, kelem)
72
+ def process(target, kelem, path = [])
66
73
 
67
74
  # Add description if available
68
75
  target["description"] = kelem["desc"] if kelem["desc"]
76
+ ktype = kelem["type"]
77
+ path += [ktype] if ktype
69
78
 
70
- case ktype = kelem["type"]
79
+ case ktype
71
80
  when "map"
72
81
  target["type"] = "object"
73
82
  target["additionalProperties"] = false
@@ -76,7 +85,12 @@ module KwalifyToJsonSchema
76
85
  if mapping.is_a? Hash
77
86
  properties = target["properties"] = {}
78
87
  mapping.each_pair { |name, e|
79
- 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])
80
94
  required << name if e["required"] == true
81
95
  }
82
96
  target["required"] = required unless required.empty?
@@ -103,13 +117,13 @@ module KwalifyToJsonSchema
103
117
  target["type"] = "boolean"
104
118
  when "date"
105
119
  # TODO
106
- new_issue Limitations::DATE_TYPE_NOT_IMPLEMENTED
120
+ new_issue path, Limitations::DATE_TYPE_NOT_IMPLEMENTED
107
121
  when "time"
108
122
  # TODO
109
- new_issue Limitations::TIME_TYPE_NOT_IMPLEMENTED
123
+ new_issue path, Limitations::TIME_TYPE_NOT_IMPLEMENTED
110
124
  when "timestamp"
111
125
  # TODO
112
- new_issue Limitations::TIMESTAMP_TYPE_NOT_IMPLEMENTED
126
+ new_issue path, Limitations::TIMESTAMP_TYPE_NOT_IMPLEMENTED
113
127
  when "scalar"
114
128
  # Use one of
115
129
  target["oneOf"] = [
@@ -152,7 +166,7 @@ module KwalifyToJsonSchema
152
166
  end
153
167
  end
154
168
 
155
- new_issue UNIQUE_NOT_SUPPORTED if kelem["unique"]
169
+ new_issue path, Limitations::UNIQUE_NOT_SUPPORTED if kelem["unique"]
156
170
 
157
171
  target
158
172
  end
@@ -169,8 +183,8 @@ module KwalifyToJsonSchema
169
183
  ep.postprocess(json_schema)
170
184
  end
171
185
 
172
- def new_issue(description)
173
- @issues << description
186
+ def new_issue(path, description)
187
+ @issues << Issue.new(path, description)
174
188
  end
175
189
  end
176
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
@@ -5,17 +5,23 @@ module KwalifyToJsonSchema
5
5
  # The file extension is used to select the format: .json or .yaml.
6
6
  # Other extension will fallback to JSON.
7
7
  # Converter options:
8
- # | Name | Type | Default value| Description |
9
- # |-----------------------|--------|--------------|-----------------------------------------------------|
10
- # | :id | String | nil | The JSON schema identifier |
11
- # | :title | String | nil | The JSON schema title |
12
- # | :description | String | nil | The JSON schema description |
13
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
14
- # | :custom_processing | Object | nil | To customize the conversion |
8
+ # | Name | Type | Default value| Description |
9
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
10
+ # | :id | string | nil | The JSON schema identifier |
11
+ # | :title | string | nil | The JSON schema title |
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 |
14
+ # | :issues_to_stderr | boolean| false | To write the issuses standard error output |
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
@@ -28,19 +34,23 @@ module KwalifyToJsonSchema
28
34
  # The source and destination strings can be JSON or YAML.
29
35
  # Other extension will fallback to JSON.
30
36
  # Converter options:
31
- # | Name | Type | Default value| Description |
32
- # |-----------------------|--------|--------------|-----------------------------------------------------|
33
- # | :id | String | nil | The JSON schema identifier |
34
- # | :title | String | nil | The JSON schema title |
35
- # | :description | String | nil | The JSON schema description |
36
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
37
- # | :custom_processing | Object | nil | To customize the conversion |
37
+ # | Name | Type | Default value| Description |
38
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
39
+ # | :id | string | nil | The JSON schema identifier |
40
+ # | :title | string | nil | The JSON schema title |
41
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
42
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
43
+ # | :issues_to_stderr | boolean| false | To write the issuses standard error output |
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
@@ -2,27 +2,37 @@ module KwalifyToJsonSchema
2
2
  # The possible options for the conversion and the associated accessors
3
3
  class Options
4
4
  # Converter options:
5
- # | Name | Type | Default value| Description |
6
- # |-----------------------|--------|--------------|-----------------------------------------------------|
7
- # | :id | String | nil | The JSON schema identifier |
8
- # | :title | String | nil | The JSON schema title |
9
- # | :description | String | nil | The JSON schema description |
10
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
11
- # | :custom_processing | Object | nil | To customize the conversion |
5
+ # | Name | Type | Default value| Description |
6
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
7
+ # | :id | string | nil | The JSON schema identifier |
8
+ # | :title | string | nil | The JSON schema title |
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 |
11
+ # | :issues_to_stderr | boolean| false | To write the issuses standard error output |
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
- ID # The JSON schema identifier [String] (nil)
15
- TITLE # The JSON schema title [String] (nil)
16
- DESCRIPTION # The JSON schema description [String] (nil)
17
- ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [Boolean] (false)
18
- CUSTOM_PROCESSING # To customize the conversion [Object] (nil)
17
+ ID # The JSON schema identifier [string] (nil)
18
+ TITLE # The JSON schema title [string] (nil)
19
+ DESCRIPTION # The JSON schema description. If not given the Kwalify description will be used if present [string] (nil)
20
+ ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [boolean] (false)
21
+ ISSUES_TO_STDERR # To write the issuses standard error output [boolean] (false)
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
@@ -40,13 +50,13 @@ module KwalifyToJsonSchema
40
50
  default_value = eval(default_value)
41
51
 
42
52
  # Create read accessor
43
- attr_reader_name = "#{name}#{type == "Boolean" ? "?" : ""}"
53
+ attr_reader_name = "#{name}#{type == "boolean" ? "?" : ""}"
44
54
 
45
55
  # Array entry as Hash for the option
46
56
  {
47
57
  const_name: const_name,
48
58
  const_name_full: "#{Options.name}::#{const_name}",
49
- name: name,
59
+ name: name.to_sym,
50
60
  description: description,
51
61
  type: type,
52
62
  default_value: default_value,
@@ -55,6 +65,11 @@ module KwalifyToJsonSchema
55
65
  }.compact
56
66
  end
57
67
 
68
+ # Same as :parse but give a Hash with the name as key
69
+ def self.parse_hash
70
+ parse.map { |e| [e[:name], e] }.to_h
71
+ end
72
+
58
73
  # Setup the constants and methods for the options
59
74
  # Example: ID will lead to get ID constant and :id method
60
75
  def self.setup
@@ -69,6 +84,12 @@ module KwalifyToJsonSchema
69
84
  }
70
85
  end
71
86
 
87
+ # Get description for option name
88
+ def self.cli_option(name)
89
+ o = parse_hash[name]
90
+ [o[:name], :type => o[:type].to_sym, :default => o[:default_value], :desc => o[:description]]
91
+ end
92
+
72
93
  setup
73
94
  end
74
95
  end
@@ -25,7 +25,7 @@ module KwalifyToJsonSchema
25
25
 
26
26
  # @return a Hash giving serialization/deserialization module and methods for a format (json/yaml)
27
27
  def self.serialization_for_format(format)
28
- serializer = { "json" => Json, "yaml" => Yaml }[format] || Json
28
+ { "json" => Json, "yaml" => Yaml }[format] || Json
29
29
  end
30
30
 
31
31
  class Language
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.1.3
4
+ version: 0.5.0
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-06-16 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