@@ -72,169 +72,172 @@ public enum JSONSchema: Equatable, JSONSchemaContext {
7272 }
7373 }
7474
75- /// `true` if values for this schema can be `null`.
76- ///
77- /// - Important: This is distinct from the concept of optionality.
78- ///
79- /// **Nullability:** Whether or not a value can be `null`.
80- ///
81- /// **Optionality:** Whether or not a key/value can be entirely
82- /// omitted from request/response data.
83- public var nullable : Bool {
75+ /// Get the description, if specified. If unspecified, returns `nil`.
76+ public var description : String ? {
8477 switch self {
8578 case . boolean( let context as JSONSchemaContext ) ,
8679 . object( let context as JSONSchemaContext , _) ,
8780 . array( let context as JSONSchemaContext , _) ,
8881 . number( let context as JSONSchemaContext , _) ,
8982 . integer( let context as JSONSchemaContext , _) ,
9083 . string( let context as JSONSchemaContext , _) :
91- return context. nullable
92- case . all, . one, . any, . not, . reference, . undefined:
93- return false
84+ return context. description
85+ case . undefined( description: let description) :
86+ return description
87+ case . all, . one, . any, . not, . reference:
88+ return nil
9489 }
9590 }
9691
97- /// `true` if this schema can only be read from and is therefore
98- /// unsupported for request data.
99- public var readOnly : Bool {
92+ /// Get the discriminator, if specified. If unspecified, returns `nil`.
93+ public var discriminator : OpenAPI . Discriminator ? {
10094 switch self {
10195 case . boolean( let context as JSONSchemaContext ) ,
10296 . object( let context as JSONSchemaContext , _) ,
10397 . array( let context as JSONSchemaContext , _) ,
10498 . number( let context as JSONSchemaContext , _) ,
10599 . integer( let context as JSONSchemaContext , _) ,
106100 . string( let context as JSONSchemaContext , _) :
107- return context. readOnly
108- case . all, . one, . any, . not, . reference, . undefined:
109- return false
101+ return context. discriminator
102+ case . all( _, let discriminator) ,
103+ . one( _, let discriminator) ,
104+ . any( _, let discriminator) :
105+ return discriminator
106+ case . undefined, . not, . reference:
107+ return nil
110108 }
111109 }
112110
111+ /// `true` if values for this schema can be `null`.
112+ ///
113+ /// - Important: This is distinct from the concept of optionality.
114+ ///
115+ /// **Nullability:** Whether or not a value can be `null`.
116+ ///
117+ /// **Optionality:** Whether or not a key/value can be entirely
118+ /// omitted from request/response data.
119+ public var nullable : Bool {
120+ return generalContext? . nullable ?? false
121+ }
122+
123+ /// `true` if this schema can only be read from and is therefore
124+ /// unsupported for request data.
125+ public var readOnly : Bool {
126+ return generalContext? . readOnly ?? false
127+ }
128+
113129 /// `true` if this schema can only be written to and is therefore
114130 /// unavailable in response data.
115131 public var writeOnly : Bool {
116- switch self {
117- case . boolean( let context as JSONSchemaContext ) ,
118- . object( let context as JSONSchemaContext , _) ,
119- . array( let context as JSONSchemaContext , _) ,
120- . number( let context as JSONSchemaContext , _) ,
121- . integer( let context as JSONSchemaContext , _) ,
122- . string( let context as JSONSchemaContext , _) :
123- return context. writeOnly
124- case . all, . one, . any, . not, . reference, . undefined:
125- return false
126- }
132+ return generalContext? . writeOnly ?? false
127133 }
128134
129135 /// `true` if this schema is deprecated, `false` otherwise.
130136 public var deprecated : Bool {
131- switch self {
132- case . boolean( let context as JSONSchemaContext ) ,
133- . object( let context as JSONSchemaContext , _) ,
134- . array( let context as JSONSchemaContext , _) ,
135- . number( let context as JSONSchemaContext , _) ,
136- . integer( let context as JSONSchemaContext , _) ,
137- . string( let context as JSONSchemaContext , _) :
138- return context. deprecated
139- case . all, . one, . any, . not, . reference, . undefined:
140- return false
141- }
137+ return generalContext? . deprecated ?? false
142138 }
143139
144140 /// Get the title, if specified. If unspecified, returns `nil`.
145141 public var title : String ? {
142+ return generalContext? . title
143+ }
144+
145+ /// Get the external docs, if specified. If unspecified, returns `nil`.
146+ public var externalDocs : OpenAPI . ExternalDocumentation ? {
147+ return generalContext? . externalDocs
148+ }
149+
150+ /// Get the allowed values, if specified. If unspecified, returns `nil`.
151+ public var allowedValues : [ AnyCodable ] ? {
152+ return generalContext? . allowedValues
153+ }
154+
155+ /// Get an example, if specified. If unspecified, returns `nil`.
156+ public var example : AnyCodable ? {
157+ return generalContext? . example
158+ }
159+ }
160+
161+ // MARK: - Context Accessors
162+ extension JSONSchema {
163+ /// Get the general context most JSONSchemas have.
164+ ///
165+ /// This is the information shared by most schemas.
166+ ///
167+ /// Notably missing this general context are:
168+ /// - `all`
169+ /// - `one`
170+ /// - `any`
171+ /// - `not`
172+ /// - `reference`
173+ /// - `undefined`
174+ ///
175+ public var generalContext : JSONSchemaContext ? {
146176 switch self {
147177 case . boolean( let context as JSONSchemaContext ) ,
148178 . object( let context as JSONSchemaContext , _) ,
149179 . array( let context as JSONSchemaContext , _) ,
150180 . number( let context as JSONSchemaContext , _) ,
151181 . integer( let context as JSONSchemaContext , _) ,
152182 . string( let context as JSONSchemaContext , _) :
153- return context. title
183+ return context
154184 case . all, . one, . any, . not, . reference, . undefined:
155185 return nil
156186 }
157187 }
158188
159- /// Get the description, if specified. If unspecified, returns `nil`.
160- public var description : String ? {
161- switch self {
162- case . boolean( let context as JSONSchemaContext ) ,
163- . object( let context as JSONSchemaContext , _) ,
164- . array( let context as JSONSchemaContext , _) ,
165- . number( let context as JSONSchemaContext , _) ,
166- . integer( let context as JSONSchemaContext , _) ,
167- . string( let context as JSONSchemaContext , _) :
168- return context. description
169- case . undefined( description: let description) :
170- return description
171- case . all, . one, . any, . not, . reference:
189+ /// Get the context specific to an `object` schema. If not an
190+ /// object schema, returns `nil`.
191+ public var objectContext : ObjectContext ? {
192+ guard case . object( _, let context) = self else {
172193 return nil
173194 }
195+ return context
174196 }
175197
176- /// Get the discriminator, if specified. If unspecified, returns `nil`.
177- public var discriminator : OpenAPI . Discriminator ? {
178- switch self {
179- case . boolean( let context as JSONSchemaContext ) ,
180- . object( let context as JSONSchemaContext , _) ,
181- . array( let context as JSONSchemaContext , _) ,
182- . number( let context as JSONSchemaContext , _) ,
183- . integer( let context as JSONSchemaContext , _) ,
184- . string( let context as JSONSchemaContext , _) :
185- return context. discriminator
186- case . all( _, let discriminator) ,
187- . one( _, let discriminator) ,
188- . any( _, let discriminator) :
189- return discriminator
190- case . undefined, . not, . reference:
198+ /// Get the context specific to an `array` schema. If not an
199+ /// array schema, returns `nil`.
200+ public var arrayContext : ArrayContext ? {
201+ guard case . array( _, let context) = self else {
191202 return nil
192203 }
204+ return context
193205 }
194206
195- /// Get the external docs, if specified. If unspecified, returns `nil`.
196- public var externalDocs : OpenAPI . ExternalDocumentation ? {
197- switch self {
198- case . boolean( let context as JSONSchemaContext ) ,
199- . object( let context as JSONSchemaContext , _) ,
200- . array( let context as JSONSchemaContext , _) ,
201- . number( let context as JSONSchemaContext , _) ,
202- . integer( let context as JSONSchemaContext , _) ,
203- . string( let context as JSONSchemaContext , _) :
204- return context. externalDocs
205- case . all, . one, . any, . not, . reference, . undefined:
207+ /// Get the context specific to a `number` schema. If not a
208+ /// number schema, returns `nil`.
209+ ///
210+ /// Although integers are numbers, an `integer` schema will
211+ /// still return `nil` when asked for a `numberContext`.
212+ ///
213+ /// If you wish to get a `NumericContext` from an `integer`
214+ /// schema, take an `IntegerContext` and explicitly request
215+ /// a `NumericContext` from it via its `numericContext`
216+ /// accessor.
217+ ///
218+ public var numberContext : NumericContext ? {
219+ guard case . number( _, let context) = self else {
206220 return nil
207221 }
222+ return context
208223 }
209224
210- /// Get the allowed values, if specified. If unspecified, returns `nil`.
211- public var allowedValues : [ AnyCodable ] ? {
212- switch self {
213- case . boolean( let context as JSONSchemaContext ) ,
214- . object( let context as JSONSchemaContext , _) ,
215- . array( let context as JSONSchemaContext , _) ,
216- . number( let context as JSONSchemaContext , _) ,
217- . integer( let context as JSONSchemaContext , _) ,
218- . string( let context as JSONSchemaContext , _) :
219- return context. allowedValues
220- case . all, . one, . any, . not, . reference, . undefined:
225+ /// Get the context specific to an `integer` schema. If not an
226+ /// integer schema, returns `nil`.
227+ public var integerContext : IntegerContext ? {
228+ guard case . integer( _, let context) = self else {
221229 return nil
222230 }
231+ return context
223232 }
224233
225- /// Get an example, if specified. If unspecified, returns `nil`.
226- public var example : AnyCodable ? {
227- switch self {
228- case . boolean( let context as JSONSchemaContext ) ,
229- . object( let context as JSONSchemaContext , _) ,
230- . array( let context as JSONSchemaContext , _) ,
231- . number( let context as JSONSchemaContext , _) ,
232- . integer( let context as JSONSchemaContext , _) ,
233- . string( let context as JSONSchemaContext , _) :
234- return context. example
235- case . all, . one, . any, . not, . reference, . undefined:
234+ /// Get the context specific to a `string` schema. If not a
235+ /// string schema, returns `nil`.
236+ public var stringContext : StringContext ? {
237+ guard case . string( _, let context) = self else {
236238 return nil
237239 }
240+ return context
238241 }
239242}
240243
0 commit comments