Module: Ask::Schema::JsonOutput

Included in:
Ask::Schema
Defined in:
lib/ask/schema/json_output.rb

Overview

Generates JSON Schema output from a Schema class definition.

Instance Method Summary collapse

Instance Method Details

#to_json(*_args) ⇒ String

Generate a pretty-printed JSON string of the schema.

Returns:

  • (String)

    Pretty-printed JSON

Raises:



43
44
45
46
# File 'lib/ask/schema/json_output.rb', line 43

def to_json(*_args)
  validate!
  JSON.pretty_generate(to_json_schema)
end

#to_json_schemaHash

Generate a hash representation of the JSON Schema.

Validates the schema before generating output. The returned hash includes :name, :description, and :schema keys.

Returns:

  • (Hash)

    The JSON Schema representation

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ask/schema/json_output.rb', line 16

def to_json_schema
  validate!

  schema_hash = {
    type: "object",
    properties: self.class.properties,
    required: self.class.required_properties,
    additionalProperties: self.class.additional_properties
  }

  schema_hash[:strict] = self.class.strict unless self.class.strict.nil?

  schema_hash["$defs"] = self.class.definitions unless self.class.definitions.empty?

  self.class.send(:merge_conditions, schema_hash, self.class)

  {
    name: @name,
    description: @description || self.class.description,
    schema: schema_hash
  }
end