Module: Ask::Schema::DSL::SchemaBuilders

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

Overview

Core schema builders that generate JSON Schema fragments for each type.

Each method returns a Hash representing a JSON Schema fragment for the given type, with all specified constraints.

Instance Method Summary collapse

Instance Method Details

#any_of_schema(description: nil, &block) ⇒ Hash

Build an anyOf schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • block (Proc)

    Block listing alternative schemas

Returns:

  • (Hash)

    JSON Schema fragment



150
151
152
153
154
155
156
157
# File 'lib/ask/schema/dsl/schema_builders.rb', line 150

def any_of_schema(description: nil, &block)
  schemas = collect_schemas_from_block(&block)

  {
    description: description,
    anyOf: schemas
  }.compact
end

#array_schema(description: nil, of: nil, min_items: nil, max_items: nil, &block) ⇒ Hash

Build an array schema fragment.

Items can be specified inline via a block, or via the :of option for simple types, references, or Schema classes.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • of (Symbol, Class, nil) (defaults to: nil)

    Items type (:string, :number, a definition name, or Schema class)

  • min_items (Integer, nil) (defaults to: nil)

    Minimum number of items

  • max_items (Integer, nil) (defaults to: nil)

    Maximum number of items

  • block (Proc)

    Block for complex item schemas

Returns:

  • (Hash)

    JSON Schema fragment



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ask/schema/dsl/schema_builders.rb', line 133

def array_schema(description: nil, of: nil, min_items: nil, max_items: nil, &block)
  items = determine_array_items(of, &block)

  {
    type: "array",
    description: description,
    items: items,
    minItems: min_items,
    maxItems: max_items
  }.compact
end

#boolean_schema(description: nil) ⇒ Hash

Build a boolean schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

Returns:

  • (Hash)

    JSON Schema fragment



70
71
72
# File 'lib/ask/schema/dsl/schema_builders.rb', line 70

def boolean_schema(description: nil)
  {type: "boolean", description: description}.compact
end

#integer_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil, enum: nil) ⇒ Hash

Build an integer schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • minimum (Integer, nil) (defaults to: nil)

    Minimum value

  • maximum (Integer, nil) (defaults to: nil)

    Maximum value

  • multiple_of (Integer, nil) (defaults to: nil)

    Value must be multiple of this

  • enum (Array<Integer>, nil) (defaults to: nil)

    Allowed values

Returns:

  • (Hash)

    JSON Schema fragment



56
57
58
59
60
61
62
63
64
65
# File 'lib/ask/schema/dsl/schema_builders.rb', line 56

def integer_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil, enum: nil)
  {
    type: "integer",
    description: description,
    minimum: minimum,
    maximum: maximum,
    multipleOf: multiple_of,
    enum: enum
  }.compact
end

#null_schema(description: nil) ⇒ Hash

Build a null schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

Returns:

  • (Hash)

    JSON Schema fragment



77
78
79
# File 'lib/ask/schema/dsl/schema_builders.rb', line 77

def null_schema(description: nil)
  {type: "null", description: description}.compact
end

#number_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil, enum: nil) ⇒ Hash

Build a number schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • minimum (Numeric, nil) (defaults to: nil)

    Minimum value

  • maximum (Numeric, nil) (defaults to: nil)

    Maximum value

  • multiple_of (Numeric, nil) (defaults to: nil)

    Value must be multiple of this

  • enum (Array<Numeric>, nil) (defaults to: nil)

    Allowed values

Returns:

  • (Hash)

    JSON Schema fragment



38
39
40
41
42
43
44
45
46
47
# File 'lib/ask/schema/dsl/schema_builders.rb', line 38

def number_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil, enum: nil)
  {
    type: "number",
    description: description,
    minimum: minimum,
    maximum: maximum,
    multipleOf: multiple_of,
    enum: enum
  }.compact
end

#object_schema(description: nil, of: nil, reference: nil, &block) ⇒ Hash

Build an object schema fragment, either inline or via reference.

When called with a block, defines properties inline. When called with :of, creates a reference to a named definition. When called with reference: (deprecated), creates a reference.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • of (Symbol, Class, nil) (defaults to: nil)

    Reference target (definition name or Schema class)

  • reference (Symbol, nil) (defaults to: nil)

    Deprecated: use of instead

  • block (Proc)

    Inline property definitions

Returns:

  • (Hash)

    JSON Schema fragment



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ask/schema/dsl/schema_builders.rb', line 92

def object_schema(description: nil, of: nil, reference: nil, &block)
  if reference
    warn "[DEPRECATION] The `reference` option will be deprecated. Please use `of` instead."
    of = reference
  end

  if of
    determine_object_reference(of, description)
  else
    sub_schema = Class.new(Schema)
    result = sub_schema.class_eval(&block)

    if result.is_a?(Hash) && result["$ref"] && sub_schema.properties.empty?
      result.merge(description ? {description: description} : {})
    elsif schema_class?(result) && sub_schema.properties.empty?
      schema_class_to_inline_schema(result).merge(description ? {description: description} : {})
    else
      schema = {
        type: "object",
        properties: sub_schema.properties,
        required: sub_schema.required_properties,
        additionalProperties: sub_schema.additional_properties,
        description: description
      }.compact

      merge_conditions(schema, sub_schema)
    end
  end
end

#one_of_schema(description: nil, &block) ⇒ Hash

Build a oneOf schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • block (Proc)

    Block listing alternative schemas

Returns:

  • (Hash)

    JSON Schema fragment



164
165
166
167
168
169
170
171
# File 'lib/ask/schema/dsl/schema_builders.rb', line 164

def one_of_schema(description: nil, &block)
  schemas = collect_schemas_from_block(&block)

  {
    description: description,
    oneOf: schemas
  }.compact
end

#string_schema(description: nil, enum: nil, min_length: nil, max_length: nil, pattern: nil, format: nil) ⇒ Hash

Build a string schema fragment.

Parameters:

  • description (String, nil) (defaults to: nil)

    Property description

  • enum (Array<String>, nil) (defaults to: nil)

    Allowed values

  • min_length (Integer, nil) (defaults to: nil)

    Minimum string length

  • max_length (Integer, nil) (defaults to: nil)

    Maximum string length

  • pattern (String, nil) (defaults to: nil)

    Regex pattern for validation

  • format (String, nil) (defaults to: nil)

    String format (e.g., “email”, “uri”)

Returns:

  • (Hash)

    JSON Schema fragment



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ask/schema/dsl/schema_builders.rb', line 19

def string_schema(description: nil, enum: nil, min_length: nil, max_length: nil, pattern: nil, format: nil)
  {
    type: "string",
    enum: enum,
    description: description,
    minLength: min_length,
    maxLength: max_length,
    pattern: pattern,
    format: format
  }.compact
end