@@ -19,13 +19,19 @@ import Foundation
19
19
/// These types can be objects, but also primitives and arrays. Represents a select subset of an
20
20
/// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
21
21
public class Schema {
22
+ /// Modifiers describing the expected format of a string `Schema`.
22
23
public enum StringFormat {
24
+ /// A custom string format.
23
25
case custom( String )
24
26
}
25
27
28
+ /// Modifiers describing the expected format of an integer `Schema`.
26
29
public enum IntegerFormat {
30
+ /// A 32-bit signed integer.
27
31
case int32
32
+ /// A 64-bit signed integer.
28
33
case int64
34
+ /// A custom integer format.
29
35
case custom( String )
30
36
}
31
37
@@ -105,7 +111,7 @@ public class Schema {
105
111
/// Returns a `Schema` representing a string value.
106
112
///
107
113
/// This schema instructs the model to produce data of type ``DataType/string``, which is suitable
108
- /// for decoding as a Swift `String` (or `String?`, if `` nullable` ` is set to `true`).
114
+ /// for decoding into a Swift `String` (or `String?`, if `nullable` is set to `true`).
109
115
///
110
116
/// > Tip: If a specific set of string values should be generated by the model (for example,
111
117
/// > "north", "south", "east", or "west"), use ``enumeration(values:description:nullable:)``
@@ -134,8 +140,8 @@ public class Schema {
134
140
/// Returns a `Schema` representing an enumeration of string values.
135
141
///
136
142
/// This schema instructs the model to produce data of type ``DataType/string`` with the
137
- /// `` format`` `"enum"`. This data is suitable for decoding as a Swift `String` (or `String?`,
138
- /// if `` nullable` ` is set to `true`), or an `enum` with strings as raw values.
143
+ /// `format` `"enum"`. This data is suitable for decoding into a Swift `String` (or `String?`,
144
+ /// if `nullable` is set to `true`), or an `enum` with strings as raw values.
139
145
///
140
146
/// **Example:**
141
147
/// The values `["north", "south", "east", "west"]` for an enumation of directions.
@@ -166,8 +172,8 @@ public class Schema {
166
172
/// Returns a `Schema` representing a single-precision floating-point number.
167
173
///
168
174
/// This schema instructs the model to produce data of type ``DataType/number`` with the
169
- /// `` format`` `"float"`, which is suitable for decoding as a Swift `Float` (or `Float?`, if
170
- /// `` nullable` ` is set to `true`).
175
+ /// `format` `"float"`, which is suitable for decoding into a Swift `Float` (or `Float?`, if
176
+ /// `nullable` is set to `true`).
171
177
///
172
178
/// > Important: This `Schema` provides a hint to the model that it should generate a
173
179
/// > single-precision floating-point number, a `float`, but only guarantees that the value will
@@ -190,8 +196,8 @@ public class Schema {
190
196
/// Returns a `Schema` representing a double-precision floating-point number.
191
197
///
192
198
/// This schema instructs the model to produce data of type ``DataType/number`` with the
193
- /// `` format`` `"double"`, which is suitable for decoding as a Swift `Double` (or `Double?`, if
194
- /// `` nullable` ` is set to `true`).
199
+ /// `format` `"double"`, which is suitable for decoding into a Swift `Double` (or `Double?`, if
200
+ /// `nullable` is set to `true`).
195
201
///
196
202
/// > Important: This `Schema` provides a hint to the model that it should generate a
197
203
/// > double-precision floating-point number, a `double`, but only guarantees that the value will
@@ -214,13 +220,13 @@ public class Schema {
214
220
/// Returns a `Schema` representing an integer value.
215
221
///
216
222
/// This schema instructs the model to produce data of type ``DataType/integer``, which is
217
- /// suitable for decoding as a Swift `Int` (or `Int?`, if `` nullable` ` is set to `true`) or other
223
+ /// suitable for decoding into a Swift `Int` (or `Int?`, if `nullable` is set to `true`) or other
218
224
/// integer types (such as `Int32`) based on the expected size of values being generated.
219
225
///
220
- /// > Important: If a `` format` ` of ``IntegerFormat/int32`` or ``IntegerFormat/int64`` is
226
+ /// > Important: If a `format` of ``IntegerFormat/int32`` or ``IntegerFormat/int64`` is
221
227
/// > specified, this provides a hint to the model that it should generate 32-bit or 64-bit
222
228
/// > integers but this `Schema` only guarantees that the value will be an integer. Therefore, it
223
- /// > is *possible* that decoding as an `Int32` could overflow even if a `` format` ` of
229
+ /// > is *possible* that decoding into an `Int32` could overflow even if a `format` of
224
230
/// > ``IntegerFormat/int32`` is specified.
225
231
///
226
232
/// - Parameters:
@@ -241,15 +247,70 @@ public class Schema {
241
247
)
242
248
}
243
249
250
+ /// Returns a `Schema` representing a boolean value.
251
+ ///
252
+ /// This schema instructs the model to produce data of type ``DataType/boolean``, which is
253
+ /// suitable for decoding into a Swift `Bool` (or `Bool?`, if `nullable` is set to `true`).
254
+ ///
255
+ /// - Parameters:
256
+ /// - description: An optional description of what the boolean should contain or represent; may
257
+ /// use Markdown format.
258
+ /// - nullable: If `true`, instructs the model that it may return `null` instead of a boolean;
259
+ /// defaults to `false`, enforcing that a boolean is returned.
244
260
public static func boolean( description: String ? = nil , nullable: Bool = false ) -> Schema {
245
261
return self . init ( type: . boolean, description: description, nullable: nullable)
246
262
}
247
263
264
+ /// Returns a `Schema` representing an array.
265
+ ///
266
+ /// This schema instructs the model to produce data of type ``DataType/array``, which has elements
267
+ /// of any other ``DataType`` (including nested ``DataType/array``s). This data is suitable for
268
+ /// decoding into many Swift collection types, including `Array`, holding elements of types
269
+ /// suitable for decoding from the respective `items` type.
270
+ ///
271
+ /// - Parameters:
272
+ /// - items: The `Schema` of the elements that the array will hold.
273
+ /// - description: An optional description of what the array should contain or represent; may
274
+ /// use Markdown format.
275
+ /// - nullable: If `true`, instructs the model that it may return `null` instead of an array;
276
+ /// defaults to `false`, enforcing that an array is returned.
248
277
public static func array( items: Schema , description: String ? = nil ,
249
278
nullable: Bool = false ) -> Schema {
250
279
return self . init ( type: . array, description: description, nullable: nullable, items: items)
251
280
}
252
281
282
+ /// Returns a `Schema` representing an object.
283
+ ///
284
+ /// This schema instructs the model to produce data of type ``DataType/object``, which has keys
285
+ /// of type ``DataType/string`` and values of any other ``DataType`` (including nested
286
+ /// ``DataType/object``s). This data is suitable for decoding into Swift keyed collection types,
287
+ /// including `Dictionary`, or other custom `struct` or `class` types.
288
+ ///
289
+ /// **Example:** A `City` could be represented with the following object `Schema`.
290
+ /// ```
291
+ /// Schema.object(properties: [
292
+ /// "name" : .string(),
293
+ /// "population": .integer()
294
+ /// ])
295
+ /// ```
296
+ /// The generated data could be decoded into a Swift native type:
297
+ /// ```
298
+ /// struct City: Decodable {
299
+ /// let name: String
300
+ /// let population: Int
301
+ /// }
302
+ /// ```
303
+ ///
304
+ /// - Parameters:
305
+ /// - properties: A dictionary containing the object's property names as keys and their
306
+ /// respective `Schema`s as values.
307
+ /// - optionalProperties: A list of property names that may be be omitted in objects generated
308
+ /// by the model; these names must correspond to the keys provided in the `properties`
309
+ /// dictionary and may be an empty list.
310
+ /// - description: An optional description of what the object should contain or represent; may
311
+ /// use Markdown format.
312
+ /// - nullable: If `true`, instructs the model that it may return `null` instead of an object;
313
+ /// defaults to `false`, enforcing that an object is returned.
253
314
public static func object( properties: [ String : Schema ] , optionalProperties: [ String ] = [ ] ,
254
315
description: String ? = nil , nullable: Bool = false ) -> Schema {
255
316
var requiredProperties = Set ( properties. keys)
0 commit comments