Schema.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// A `Schema` object allows the definition of input and output data types.
  16. ///
  17. /// These types can be objects, but also primitives and arrays. Represents a select subset of an
  18. /// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
  19. public class Schema {
  20. /// The data type.
  21. let type: DataType
  22. /// The format of the data.
  23. let format: String?
  24. /// A brief description of the parameter.
  25. let description: String?
  26. /// Indicates if the value may be null.
  27. let nullable: Bool?
  28. /// Possible values of the element of type ``DataType/string`` with "enum" format.
  29. let enumValues: [String]?
  30. /// Schema of the elements of type ``DataType/array``.
  31. let items: Schema?
  32. /// Properties of type ``DataType/object``.
  33. let properties: [String: Schema]?
  34. /// Required properties of type ``DataType/object``.
  35. let requiredProperties: [String]?
  36. /// Constructs a new `Schema`.
  37. ///
  38. /// - Parameters:
  39. /// - type: The data type.
  40. /// - format: The format of the data; used only for primitive datatypes.
  41. /// Supported formats:
  42. /// - ``DataType/integer``: int32, int64
  43. /// - ``DataType/number``: float, double
  44. /// - ``DataType/string``: enum
  45. /// - description: A brief description of the parameter; may be formatted as Markdown.
  46. /// - nullable: Indicates if the value may be null.
  47. /// - enumValues: Possible values of the element of type ``DataType/string`` with "enum" format.
  48. /// For example, an enum `Direction` may be defined as `["EAST", NORTH", "SOUTH", "WEST"]`.
  49. /// - items: Schema of the elements of type ``DataType/array``.
  50. /// - properties: Properties of type ``DataType/object``.
  51. /// - requiredProperties: Required properties of type ``DataType/object``.
  52. public init(type: DataType, format: String? = nil, description: String? = nil,
  53. nullable: Bool? = nil,
  54. enumValues: [String]? = nil, items: Schema? = nil,
  55. properties: [String: Schema]? = nil,
  56. requiredProperties: [String]? = nil) {
  57. self.type = type
  58. self.format = format
  59. self.description = description
  60. self.nullable = nullable
  61. self.enumValues = enumValues
  62. self.items = items
  63. self.properties = properties
  64. self.requiredProperties = requiredProperties
  65. }
  66. }
  67. /// A data type.
  68. ///
  69. /// Contains the set of OpenAPI [data types](https://spec.openapis.org/oas/v3.0.3#data-types).
  70. public enum DataType: String {
  71. /// A `String` type.
  72. case string = "STRING"
  73. /// A floating-point number type.
  74. case number = "NUMBER"
  75. /// An integer type.
  76. case integer = "INTEGER"
  77. /// A boolean type.
  78. case boolean = "BOOLEAN"
  79. /// An array type.
  80. case array = "ARRAY"
  81. /// An object type.
  82. case object = "OBJECT"
  83. }
  84. // MARK: - Codable Conformance
  85. extension Schema: Encodable {
  86. enum CodingKeys: String, CodingKey {
  87. case type
  88. case format
  89. case description
  90. case nullable
  91. case enumValues = "enum"
  92. case items
  93. case properties
  94. case requiredProperties = "required"
  95. }
  96. }
  97. extension DataType: Encodable {}