Module: Schematist::DSL::SchemaBuilders

Included in:
Schematist::DSL
Defined in:
lib/schematist/dsl/schema_builders.rb

Defined Under Namespace

Classes: SchemaBlock

Instance Method Summary collapse

Instance Method Details

#all_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/schematist/dsl/schema_builders.rb', line 142

def all_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block)
  schema_block = collect_schema_block(&block)

  annotate({
    description: description,
    allOf: schema_block.schemas,
    unevaluatedProperties: unevaluated_properties,
    unevaluatedItems: unevaluated_items
  }.compact, annotations, schema_block)
end

#any_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/schematist/dsl/schema_builders.rb', line 120

def any_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block)
  schema_block = collect_schema_block(&block)

  annotate({
    description: description,
    anyOf: schema_block.schemas,
    unevaluatedProperties: unevaluated_properties,
    unevaluatedItems: unevaluated_items
  }.compact, annotations, schema_block)
end

#array_schema(description: nil, of: nil, min_items: nil, max_items: nil, unique: nil, unevaluated_items: nil, **annotations, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/schematist/dsl/schema_builders.rb', line 87

def array_schema(description: nil, of: nil, min_items: nil, max_items: nil, unique: nil, unevaluated_items: nil, **annotations, &block)
  schema_block = collect_schema_block(&block) if block

  annotate({
    type: "array",
    description: description,
    items: determine_array_items(of, schema_block),
    minItems: min_items,
    maxItems: max_items,
    uniqueItems: unique,
    unevaluatedItems: unevaluated_items
  }.compact, annotations, schema_block)
end

#boolean_schema(description: nil, enum: nil, const: nil, **annotations, &block) ⇒ Object



60
61
62
63
64
# File 'lib/schematist/dsl/schema_builders.rb', line 60

def boolean_schema(description: nil, enum: nil, const: nil, **annotations, &block)
  schema_block = collect_schema_block(&block) if block

  annotate({type: "boolean", description: description, enum: enum, const: const}.compact, annotations, schema_block)
end

#integer_schema(description: nil, minimum: nil, maximum: nil, greater_than: nil, less_than: nil, multiple_of: nil, enum: nil, const: nil, format: nil, **annotations, &block) ⇒ Object



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

def integer_schema(description: nil, minimum: nil, maximum: nil, greater_than: nil, less_than: nil, multiple_of: nil, enum: nil, const: nil, format: nil, **annotations, &block)
  schema_block = collect_schema_block(&block) if block

  annotate({
    type: "integer",
    description: description,
    format: format,
    minimum: minimum,
    maximum: maximum,
    exclusiveMinimum: greater_than,
    exclusiveMaximum: less_than,
    multipleOf: multiple_of,
    enum: enum,
    const: const
  }.compact, annotations, schema_block)
end

#none_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/schematist/dsl/schema_builders.rb', line 153

def none_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block)
  schema_block = collect_schema_block(&block)
  schemas = schema_block.schemas

  annotate({
    description: description,
    not: schemas.size == 1 ? schemas.first : {anyOf: schemas},
    unevaluatedProperties: unevaluated_properties,
    unevaluatedItems: unevaluated_items
  }.compact, annotations, schema_block)
end

#null_schema(description: nil, enum: nil, **annotations, &block) ⇒ Object



66
67
68
69
70
# File 'lib/schematist/dsl/schema_builders.rb', line 66

def null_schema(description: nil, enum: nil, **annotations, &block)
  schema_block = collect_schema_block(&block) if block

  annotate({type: "null", description: description, enum: enum}.compact, annotations, schema_block)
end

#number_schema(description: nil, minimum: nil, maximum: nil, greater_than: nil, less_than: nil, multiple_of: nil, enum: nil, const: nil, format: nil, **annotations, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/schematist/dsl/schema_builders.rb', line 26

def number_schema(description: nil, minimum: nil, maximum: nil, greater_than: nil, less_than: nil, multiple_of: nil, enum: nil, const: nil, format: nil, **annotations, &block)
  schema_block = collect_schema_block(&block) if block

  annotate({
    type: "number",
    description: description,
    format: format,
    minimum: minimum,
    maximum: maximum,
    exclusiveMinimum: greater_than,
    exclusiveMaximum: less_than,
    multipleOf: multiple_of,
    enum: enum,
    const: const
  }.compact, annotations, schema_block)
end

#object_schema(description: nil, of: nil, reference: nil, min_properties: nil, max_properties: nil, unevaluated_properties: nil, **annotations, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/schematist/dsl/schema_builders.rb', line 72

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

  schema = of ? determine_object_reference(of, description) : build_object_schema(description, &block)

  annotate(schema.merge({
    minProperties: min_properties,
    maxProperties: max_properties,
    unevaluatedProperties: unevaluated_properties
  }.compact), annotations)
end

#one_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/schematist/dsl/schema_builders.rb', line 131

def one_of_schema(description: nil, unevaluated_properties: nil, unevaluated_items: nil, **annotations, &block)
  schema_block = collect_schema_block(&block)

  annotate({
    description: description,
    oneOf: schema_block.schemas,
    unevaluatedProperties: unevaluated_properties,
    unevaluatedItems: unevaluated_items
  }.compact, annotations, schema_block)
end

#string_schema(description: nil, enum: nil, const: nil, min_length: nil, max_length: nil, pattern: nil, format: nil, content_encoding: nil, content_media_type: nil, **annotations, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/schematist/dsl/schema_builders.rb', line 9

def string_schema(description: nil, enum: nil, const: nil, min_length: nil, max_length: nil, pattern: nil, format: nil, content_encoding: nil, content_media_type: nil, **annotations, &block)
  schema_block = collect_schema_block(&block) if block

  annotate({
    type: "string",
    enum: enum,
    const: const,
    description: description,
    minLength: min_length,
    maxLength: max_length,
    pattern: pattern,
    format: format,
    contentEncoding: content_encoding,
    contentMediaType: content_media_type
  }.compact, annotations, schema_block)
end

#tuple_schema(description: nil, of: nil, min_items: nil, max_items: nil, unevaluated_items: nil, **annotations, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/schematist/dsl/schema_builders.rb', line 101

def tuple_schema(description: nil, of: nil, min_items: nil, max_items: nil, unevaluated_items: nil, **annotations, &block)
  schema_block = collect_schema_block(&block)
  schemas = schema_block.schemas
  tail = determine_array_items(of) if of

  # A tuple is exactly its prefix unless you give it somewhere for the rest to go
  closed = tail.nil? && unevaluated_items.nil? && max_items.nil?

  annotate({
    type: "array",
    description: description,
    prefixItems: schemas,
    items: tail,
    minItems: min_items || schemas.length,
    maxItems: closed ? schemas.length : max_items,
    unevaluatedItems: unevaluated_items
  }.compact, annotations, schema_block)
end