Module: Ask::Schema::DSL::Utilities

Included in:
Ask::Schema::DSL
Defined in:
lib/ask/schema/dsl/utilities.rb

Overview

Utility methods for schema definitions, references, and property registration.

Instance Method Summary collapse

Instance Method Details

#define(name) ⇒ Object

Define a named sub-schema for reuse via #reference.

Named definitions appear in the output under ‘$defs`.

Examples:

define(:address) do
  string :street
  string :city
end

Parameters:

  • name (Symbol)

    The definition name (used in $ref)

  • block (Proc)

    DSL block for the sub-schema properties



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ask/schema/dsl/utilities.rb', line 20

def define(name, &)
  sub_schema = Class.new(Schema)
  sub_schema.class_eval(&)

  schema = {
    type: "object",
    properties: sub_schema.properties,
    required: sub_schema.required_properties,
    additionalProperties: sub_schema.additional_properties
  }

  merge_conditions(schema, sub_schema)

  definitions[name] = schema
end

#reference(schema_name) ⇒ Hash{"$ref" => String}

Create a ‘$ref` reference to a named definition or root.

Use with object or array via the :of option, or standalone to produce a reference hash.

Examples:

reference(:address)  # => { "$ref" => "#/$defs/address" }
reference(:root)     # => { "$ref" => "#" }

Parameters:

  • schema_name (Symbol)

    The definition name, or :root for root reference

Returns:

  • (Hash{"$ref" => String})

    The reference



47
48
49
50
51
52
53
# File 'lib/ask/schema/dsl/utilities.rb', line 47

def reference(schema_name)
  if schema_name == :root
    {"$ref" => "#"}
  else
    {"$ref" => "#/$defs/#{schema_name}"}
  end
end