kwalify_to_json_schema 0.2.1 → 0.6.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: 2e3e11103f87d4445e81beb29fe9b36d3dc17c0e03b399d2d088d9b1fa8504d8
4
- data.tar.gz: c2e09d2aa94ceb1a5588f9e175b402ee11a3260d6b1441471eec1f304e7b29d6
3
+ metadata.gz: 2bbbe823e0df66f54b21956a628b2a08dad03190f4d2884ee05abe1c6362fba8
4
+ data.tar.gz: ef883dfea6e9cebcee6c0bd95d7470db46d428734dac428cd52f65230af7666a
5
5
  SHA512:
6
- metadata.gz: '09fd74c1ade4d0e9389d46ec6d93d40d9ce797a34bc951eaf0354c3dfe2f2464c2a2849e65b4dc0ebaa682648816eb16b0b79c91ba56f4815d0ac309473e28d8'
7
- data.tar.gz: e78af6fbb9e4c2cd3e17e4168abd3615b5e798ffb318a737b5978a8613af4548a7ac4a36bae783e228b2d9314e0f4815ce6172ccb6a1b3789f8bdde491c89abb
6
+ metadata.gz: 237ff9995ed857260b71851e3499640af68deafcbb65c8103d6e7227ce1a0c774b06d8b3d4cab7ed1f0f54bf74e4122e6dcfe543515db2dc70a2dc971e10e8a0
7
+ data.tar.gz: d30502c9942dd6039c074b3c227b3829fa8624dca103541572487659eda84386ba91f7583742183dbe1f1d8ca6e79f74b8aadfc461e65a1aac07ac576742b0e2
@@ -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,8 +85,18 @@ 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)
82
- required << name if e["required"] == true
88
+ # Handle partial support of mapping default value
89
+ # Only default rule is supported (see http://www.kuwata-lab.com/kwalify/ruby/users-guide.02.html#tips-default)
90
+ if name == "="
91
+ if e.is_a?(Hash)
92
+ process(target["additionalProperties"] = {}, e, path)
93
+ else
94
+ new_issue path, Limitations::ONLY_DEFAULT_RULE_SUPPORTED_FOR_DEFAULT_MAPPING
95
+ end
96
+ else
97
+ process(properties[name] = {}, e, path + [name])
98
+ required << name if e["required"] == true
99
+ end
83
100
  }
84
101
  target["required"] = required unless required.empty?
85
102
  end
@@ -87,7 +104,13 @@ module KwalifyToJsonSchema
87
104
  target["type"] = "array"
88
105
  sequence = kelem["sequence"]
89
106
  if sequence.is_a? Array
90
- process(target["items"] = {}, sequence.first)
107
+ rule = sequence.first
108
+ if rule["unique"]
109
+ target["uniqueItems"] = true
110
+ rule = rule.dup
111
+ rule.delete("unique")
112
+ end
113
+ process(target["items"] = {}, rule)
91
114
  end
92
115
  when "str"
93
116
  target["type"] = "string"
@@ -105,13 +128,13 @@ module KwalifyToJsonSchema
105
128
  target["type"] = "boolean"
106
129
  when "date"
107
130
  # TODO
108
- new_issue Limitations::DATE_TYPE_NOT_IMPLEMENTED
131
+ new_issue path, Limitations::DATE_TYPE_NOT_IMPLEMENTED
109
132
  when "time"
110
133
  # TODO
111
- new_issue Limitations::TIME_TYPE_NOT_IMPLEMENTED
134
+ new_issue path, Limitations::TIME_TYPE_NOT_IMPLEMENTED
112
135
  when "timestamp"
113
136
  # TODO
114
- new_issue Limitations::TIMESTAMP_TYPE_NOT_IMPLEMENTED
137
+ new_issue path, Limitations::TIMESTAMP_TYPE_NOT_IMPLEMENTED
115
138
  when "scalar"
116
139
  # Use one of
117
140
  target["oneOf"] = [
@@ -123,7 +146,7 @@ module KwalifyToJsonSchema
123
146
  when "any"
124
147
  # Don't put type
125
148
  else
126
- new_issue("Unknown Kwalify type #{ktype}")
149
+ new_issue(path, "Unknown Kwalify type #{ktype}")
127
150
  end
128
151
 
129
152
  target["enum"] = kelem["enum"] if kelem["enum"]
@@ -154,7 +177,7 @@ module KwalifyToJsonSchema
154
177
  end
155
178
  end
156
179
 
157
- new_issue Limitations::UNIQUE_NOT_SUPPORTED if kelem["unique"]
180
+ new_issue path, Limitations::UNIQUE_NOT_SUPPORTED_WITHIN_MAPPING if kelem["unique"]
158
181
 
159
182
  target
160
183
  end
@@ -171,8 +194,8 @@ module KwalifyToJsonSchema
171
194
  ep.postprocess(json_schema)
172
195
  end
173
196
 
174
- def new_issue(description)
175
- @issues << description
197
+ def new_issue(path, description)
198
+ @issues << Issue.new(path, description)
176
199
  end
177
200
  end
178
201
  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
@@ -4,6 +4,7 @@ module KwalifyToJsonSchema
4
4
  DATE_TYPE_NOT_IMPLEMENTED = "Kwalify 'date' type is not supported and is ignored"
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
- UNIQUE_NOT_SUPPORTED = "Kwalify 'unique' is not supported by JSON Schema and is ignored"
7
+ UNIQUE_NOT_SUPPORTED_WITHIN_MAPPING = "Kwalify 'unique' within a mapping is not supported by JSON Schema and is ignored"
8
+ ONLY_DEFAULT_RULE_SUPPORTED_FOR_DEFAULT_MAPPING = "Kwalify mapping default value is not supported by JSON Schema and is ignored. Only default rule is supported"
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.1
4
+ version: 0.6.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: 2021-04-15 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