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
-
#any_of_schema(description: nil, &block) ⇒ Hash
Build an
anyOfschema fragment. -
#array_schema(description: nil, of: nil, min_items: nil, max_items: nil, &block) ⇒ Hash
Build an array schema fragment.
-
#boolean_schema(description: nil) ⇒ Hash
Build a boolean schema fragment.
-
#integer_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil, enum: nil) ⇒ Hash
Build an integer schema fragment.
-
#null_schema(description: nil) ⇒ Hash
Build a null schema fragment.
-
#number_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil, enum: nil) ⇒ Hash
Build a number schema fragment.
-
#object_schema(description: nil, of: nil, reference: nil, &block) ⇒ Hash
Build an object schema fragment, either inline or via reference.
-
#one_of_schema(description: nil, &block) ⇒ Hash
Build a
oneOfschema fragment. -
#string_schema(description: nil, enum: nil, min_length: nil, max_length: nil, pattern: nil, format: nil) ⇒ Hash
Build a string schema fragment.
Instance Method Details
#any_of_schema(description: nil, &block) ⇒ Hash
Build an anyOf 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.
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.
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.
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.
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.
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.
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.
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.
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 |