kwalify_to_json_schema 0.1.2 → 0.4.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: 5cf3ece3c538c1574fff63993e51e8e6eb284c29761ab610cb3b0731853747c7
4
- data.tar.gz: 33e5b9c4cbed92021fd1e5b9502a33f48b502ae7e56cb949ef20f878c459f64f
3
+ metadata.gz: '06494f442be72dab717741fd0515c09e57541add4d42fd0740fd6ec475d18e08'
4
+ data.tar.gz: 0e3969aae90bda694a8beae488ecabfce80ee15ec381eca3f050456a1b7d4d45
5
5
  SHA512:
6
- metadata.gz: 3be33ae75e36dabe85f77d8267ffe98c14def6d8c58243c210c3926e48c5c5257564b57f49d0792e9c5c33d4a2d20c268042305d32fee4bc957a0536a6e1aa75
7
- data.tar.gz: 5d21d177961dedd730b4b2ae73d4225c15a508340440e3cd1f89cbbff24a7c4d56a5e0af524ca48cf690ac4e1042734ca2f00d34a5ba1c5e9513921e0d67a29e
6
+ metadata.gz: b5f7763ebef99718db807a839965f2118be48abe005616147a2a3c038adeb37a82564448faf52e35e6d782ce0666fad36902ed1e3832be8f7c67a7150471dc09
7
+ data.tar.gz: 5f690c9b95207db9329056ecaf74896beca736c634bf67659e16220613fa6cd5368dcc8160bd2b739262acc78026d09aed406853c025d0789509c2f4a6a95d4d
@@ -25,11 +25,12 @@ 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::SCHEMA_VERSION))
33
+ option Options::CUSTOM_PROCESSING,
33
34
  :type => :string,
34
35
  :desc => <<~DESC
35
36
  Allows to provide a pre/post processing file on handled schemas.
@@ -38,10 +39,8 @@ module KwalifyToJsonSchema
38
39
  DESC
39
40
 
40
41
  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
- }
42
+ opts = options.dup
43
+ opts[Options::CUSTOM_PROCESSING] = custom_processing(options)
45
44
  KwalifyToJsonSchema.convert_file(kwalify_schema_file, result_file, opts)
46
45
  end
47
46
 
@@ -49,10 +48,7 @@ module KwalifyToJsonSchema
49
48
 
50
49
  desc "convert_dir KWALIFY_SCHEMA_DIR, RESULT_DIR",
51
50
  "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"
51
+ option(*Options.cli_option(:issues_to_description))
56
52
  option :format,
57
53
  :type => :string,
58
54
  :enum => ["json", "yaml"],
@@ -63,7 +59,8 @@ module KwalifyToJsonSchema
63
59
  :default => false,
64
60
  :desc => "Process files recursively",
65
61
  :long_desc => ""
66
- option :custom_processing,
62
+ option(*Options.cli_option(Options::SCHEMA_VERSION))
63
+ option Options::CUSTOM_PROCESSING,
67
64
  :type => :string,
68
65
  :desc => <<~DESC
69
66
  Allows to provide a pre/post processing file on handled schemas.
@@ -98,7 +95,7 @@ module KwalifyToJsonSchema
98
95
  begin
99
96
  processing_class = Object.const_get :CustomProcessing
100
97
  custom_processing = processing_class.new
101
- rescue NameError => e
98
+ rescue NameError
102
99
  raise "The 'CustomProcessing' module must be defined in #{pf}"
103
100
  end
104
101
  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,13 +19,14 @@ 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
+ # | :custom_processing | object | nil | To customize the conversion |
29
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
29
30
  # --
30
31
  def initialize(options_hash = {})
31
32
  @options = Options.new(options_hash)
@@ -46,6 +47,9 @@ module KwalifyToJsonSchema
46
47
  description << issues.map { |issue| "* #{issue}" }.join("\n")
47
48
  end
48
49
 
50
+ # Override description if given in option
51
+ json_schema["description"] = options.description if options.description
52
+
49
53
  postprocess(json_schema)
50
54
  end
51
55
 
@@ -53,10 +57,9 @@ module KwalifyToJsonSchema
53
57
 
54
58
  def root
55
59
  {
56
- "$schema" => SCHEMA,
57
- "$id" => options.id,
60
+ "$schema" => SCHEMA % options.schema_version,
61
+ "id" => options.id,
58
62
  "title" => options.title,
59
- "description" => options.description,
60
63
  }.reject { |k, v| v.nil? }
61
64
  end
62
65
 
@@ -76,6 +79,7 @@ module KwalifyToJsonSchema
76
79
  if mapping.is_a? Hash
77
80
  properties = target["properties"] = {}
78
81
  mapping.each_pair { |name, e|
82
+ next if name == "=" # Ignore mapping default value
79
83
  process(properties[name] = {}, e)
80
84
  required << name if e["required"] == true
81
85
  }
@@ -152,7 +156,7 @@ module KwalifyToJsonSchema
152
156
  end
153
157
  end
154
158
 
155
- new_issue "'unique' is not supported by JSON Schema" if kelem["unique"]
159
+ new_issue Limitations::UNIQUE_NOT_SUPPORTED if kelem["unique"]
156
160
 
157
161
  target
158
162
  end
@@ -173,6 +177,4 @@ module KwalifyToJsonSchema
173
177
  @issues << description
174
178
  end
175
179
  end
176
-
177
-
178
180
  end
@@ -5,13 +5,14 @@ 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
+ # | :custom_processing | object | nil | To customize the conversion |
15
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
15
16
  # --
16
17
  # @param source Path to Kwalify YAML schema
17
18
  # @param dest Path to resulting JSON schema
@@ -28,13 +29,14 @@ module KwalifyToJsonSchema
28
29
  # The source and destination strings can be JSON or YAML.
29
30
  # Other extension will fallback to JSON.
30
31
  # 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 |
32
+ # | Name | Type | Default value| Description |
33
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
34
+ # | :id | string | nil | The JSON schema identifier |
35
+ # | :title | string | nil | The JSON schema title |
36
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
37
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
38
+ # | :custom_processing | object | nil | To customize the conversion |
39
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
38
40
  # --
39
41
  # @param kwalify_schema Kwalify schema as YAML or JSON
40
42
  # @param source_format format of the source schema
@@ -1,8 +1,9 @@
1
1
  module KwalifyToJsonSchema
2
- # Enumeration of known implementation limitation
2
+ # Enumeration of known implementation limitations
3
3
  module Limitations
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
8
  end
8
9
  end
@@ -2,20 +2,22 @@ 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
+ # | :custom_processing | object | nil | To customize the conversion |
12
+ # | :schema_version | string | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
12
13
  # --
13
14
  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)
15
+ ID # The JSON schema identifier [string] (nil)
16
+ TITLE # The JSON schema title [string] (nil)
17
+ DESCRIPTION # The JSON schema description. If not given the Kwalify description will be used if present [string] (nil)
18
+ ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [boolean] (false)
19
+ CUSTOM_PROCESSING # To customize the conversion [object] (nil)
20
+ SCHEMA_VERSION # JSON schema version. Changing this value only change the value of $schema field[string] ("draft-04")
19
21
  )
20
22
 
21
23
  # The options as Hash
@@ -40,13 +42,13 @@ module KwalifyToJsonSchema
40
42
  default_value = eval(default_value)
41
43
 
42
44
  # Create read accessor
43
- attr_reader_name = "#{name}#{type == "Boolean" ? "?" : ""}"
45
+ attr_reader_name = "#{name}#{type == "boolean" ? "?" : ""}"
44
46
 
45
47
  # Array entry as Hash for the option
46
48
  {
47
49
  const_name: const_name,
48
50
  const_name_full: "#{Options.name}::#{const_name}",
49
- name: name,
51
+ name: name.to_sym,
50
52
  description: description,
51
53
  type: type,
52
54
  default_value: default_value,
@@ -55,6 +57,11 @@ module KwalifyToJsonSchema
55
57
  }.compact
56
58
  end
57
59
 
60
+ # Same as :parse but give a Hash with the name as key
61
+ def self.parse_hash
62
+ parse.map { |e| [e[:name], e] }.to_h
63
+ end
64
+
58
65
  # Setup the constants and methods for the options
59
66
  # Example: ID will lead to get ID constant and :id method
60
67
  def self.setup
@@ -69,6 +76,12 @@ module KwalifyToJsonSchema
69
76
  }
70
77
  end
71
78
 
79
+ # Get description for option name
80
+ def self.cli_option(name)
81
+ o = parse_hash[name]
82
+ [o[:name], :type => o[:type].to_sym, :default => o[:default_value], :desc => o[:description]]
83
+ end
84
+
72
85
  setup
73
86
  end
74
87
  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.2
4
+ version: 0.4.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-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