Class: KwalifyToJsonSchema::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/kwalify_to_json_schema/converter.rb

Overview

Heart of conversion implementation

Example of use:

kwalify_schema = YAML.load(File.read(“kwalify_schema.yaml”))

converter = KwalifyToJsonSchema::Converter.new(options) json_schema = converter.exec(kwalify_schema)

File.write(“json_schema.json”, JSON.pretty_generate(json_schema))

Constant Summary collapse

SCHEMA =
"http://json-schema.org/%s/schema#"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Converter

Converter options: | Name | Type | Default value| Description | |———————–|——–|————–|——————————————————————————————| | :id | string | nil | The JSON schema identifier | | :title | string | nil | The JSON schema title | | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present| | :issues_to_description| boolean| false | To append the issues to the JSON schema description | | :issues_to_stderr | boolean| false | To write the issues to standard error output | | :custom_processing | object | nil | To customize the conversion | | :schema_version | string | “draft-04” | JSON schema version. Changing this value only change the value of $schema field | | :verbose | boolean| false | To be verbose when converting | –

Parameters:

  • options (Options) (defaults to: {})

    or Hash



34
35
36
37
# File 'lib/kwalify_to_json_schema/converter.rb', line 34

def initialize(options = {})
  @options = Options.new(options)
  @issues = Issues.new
end

Instance Attribute Details

#issuesObject (readonly)

Give the list of issues encontered while converting as array of strings.



19
20
21
# File 'lib/kwalify_to_json_schema/converter.rb', line 19

def issues
  @issues
end

#optionsObject (readonly)

The options given used to initialized the converter



17
18
19
# File 'lib/kwalify_to_json_schema/converter.rb', line 17

def options
  @options
end

Instance Method Details

#exec(kwalify_schema) ⇒ Object

Execute the conversion process

Parameters:

  • kwalify_schema

    Kwalify schema as Hash or YAML string to be converted as Hash

Returns:

  • JSON schema as Hash



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kwalify_to_json_schema/converter.rb', line 42

def exec(kwalify_schema)
  kwalify_schema = YAML.load(kwalify_schema) if kwalify_schema.is_a? String
  kwalify_schema = preprocess(kwalify_schema.dup)

  json_schema = process(root, kwalify_schema)
  if issues.any? && options.issues_to_description?
    description = json_schema["description"] ||= ""
    description << "Issues when converting from Kwalify:\n"
    description << issues.descriptions_uniq.map { |description| "* #{description}" }.join("\n")
  end

  # Override description if given in option
  json_schema["description"] = options.description if options.description
  STDERR.puts issues if options.issues_to_stderr?

  postprocess(json_schema)
end