-
Notifications
You must be signed in to change notification settings - Fork 37
JSONSchema
OpenAPI "Schema Object"
public enum JSONSchema: Equatable, JSONSchemaContext
ComponentDictionaryLocatable
, Decodable
, Encodable
, Equatable
, LocallyDereferenceable
, JSONSchemaContext
public init(from decoder: Decoder) throws
case boolean(CoreContext<JSONTypeFormat.BooleanFormat>)
case number(CoreContext<JSONTypeFormat.NumberFormat>, NumericContext)
case integer(CoreContext<JSONTypeFormat.IntegerFormat>, IntegerContext)
case string(CoreContext<JSONTypeFormat.StringFormat>, StringContext)
indirect case object(CoreContext<JSONTypeFormat.ObjectFormat>, ObjectContext)
indirect case array(CoreContext<JSONTypeFormat.ArrayFormat>, ArrayContext)
indirect case all(of: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>)
indirect case one(of: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>)
indirect case any(of: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>)
indirect case not(JSONSchema, core: CoreContext<JSONTypeFormat.AnyFormat>)
case reference(JSONReference<JSONSchema>)
Schemas without a type
.
case fragment(CoreContext<JSONTypeFormat.AnyFormat>)
public static var openAPIComponentsKey: String
public static var openAPIComponentsKeyPath: KeyPath<OpenAPI.Components, OpenAPI.ComponentDictionary<Self>>
The type and format of the schema.
public var jsonTypeFormat: JSONTypeFormat?
The fundamental type of the schema.
public var jsonType: JSONType?
The format of the schema as a string value.
public var formatString: String?
This can be set even when a schema type has
not be specified. If a type has been specified,
a type-safe format can be used and retrieved
via the jsonTypeFormat
property.
public var required: Bool
public var description: String?
public var discriminator: OpenAPI.Discriminator?
public var nullable: Bool
public var readOnly: Bool
public var writeOnly: Bool
public var deprecated: Bool
public var title: String?
public var externalDocs: OpenAPI.ExternalDocumentation?
public var allowedValues: [AnyCodable]?
public var defaultValue: AnyCodable?
public var example: AnyCodable?
Check if this schema is an empty .fragment
.
public var isEmpty: Bool
A special case of the .fragment
schema is the "empty"
schema where no information about the schema component
is available.
This is equivalent to the following JSON Schema:
{
}
Check if this schema is a .fragment
.
public var isFragment: Bool
Check if a schema is a .boolean
.
public var isBoolean: Bool
Check if a schema is a .number
.
public var isNumber: Bool
This returns false
if the schema is an
.integer
even though Integer schemas
can be easily transformed into Number schemas.
Check if a schema is an .integer
.
public var isInteger: Bool
Check if a schema is a .string
.
public var isString: Bool
Check if a schema is an .object
.
public var isObject: Bool
Check if a schema is an .array
.
public var isArray: Bool
Check if a schema is a .reference
.
public var isReference: Bool
Get the core context most JSONSchemas have.
public var coreContext: JSONSchemaContext?
This is the information shared by most schemas.
Notably, reference
schemas do not have this core context.
Get the context specific to an object
schema. If not an
object schema, returns nil
.
public var objectContext: ObjectContext?
Get the context specific to an array
schema. If not an
array schema, returns nil
.
public var arrayContext: ArrayContext?
Get the context specific to a number
schema. If not a
number schema, returns nil
.
public var numberContext: NumericContext?
Although integers are numbers, an integer
schema will
still return nil
when asked for a numberContext
.
If you wish to get a NumericContext
from an integer
schema, take an IntegerContext
and explicitly request
a NumericContext
from it via its numericContext
accessor.
Get the context specific to an integer
schema. If not an
integer schema, returns nil
.
public var integerContext: IntegerContext?
Get the context specific to a string
schema. If not a
string schema, returns nil
.
public var stringContext: StringContext?
A required, non-nullable boolean schema.
public static var boolean: JSONSchema
A required, non-nullable fragment of a schema.
public static var fragment: JSONSchema
This is also known as the "empty" schema because
its representation in JSON is just an empty object { }
.
A required, non-nullable string schema.
public static var string: JSONSchema
A required, non-nullable number schema.
public static var number: JSONSchema
A required, non-nullable integer schema.
public static var integer: JSONSchema
A required, non-nullable object schema.
public static var object: JSONSchema
A required, non-nullable array schema.
public static var array: JSONSchema
Returns a dereferenced schema object if all references in this schema object can be found in the Components Object.
public func _dereferenced(
in components: OpenAPI.Components,
following references: Set<AnyHashable>,
dereferencedFromComponentNamed name: String?
) throws -> DereferencedJSONSchema
_dereferenced(in:following:)
is meant for internal use
but this type gets a public-facing dereferenced(in:)
function
for free from the LocallyDereferenceable
protocol. For all
external uses, call dereferenced(in:)
.
ReferenceError.cannotLookupRemoteReference
or ReferenceError.missingOnLookup(name:key:)
depending on whether an unresolvable reference points to another file or just points to a component in the same file that cannot be found in the Components Object.
Returns a dereferenced schema object if this schema object already does not contain any references.
public func dereferenced() -> DereferencedJSONSchema?
To create a dereferenced schema object from a schema object
that does have references, use dereferenced(in:)
.
Return the optional version of this JSONSchema
public func optionalSchemaObject() -> JSONSchema
Return the required version of this JSONSchema
public func requiredSchemaObject() -> JSONSchema
Return the nullable version of this JSONSchema
public func nullableSchemaObject() -> JSONSchema
Return a version of this JSONSchema
that only allows the given
values.
public func with(allowedValues: [AnyCodable]) -> JSONSchema
Return a version of this JSONSchema
that has the given default value.
public func with(defaultValue: AnyCodable) -> JSONSchema
Returns a version of this JSONSchema
that has the given example
attached.
public func with(example: AnyCodable) throws -> JSONSchema
Returns a version of this JSONSchema
that has the given discriminator.
public func with(discriminator: OpenAPI.Discriminator) -> JSONSchema
boolean(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:allowedValues:defaultValue:example:)
Construct a boolean schema.
public static func boolean(
format: JSONTypeFormat.BooleanFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.BooleanFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
boolean(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:allowedValues:defaultValue:example:)
Construct a boolean schema passing a variadic list of allowed values.
public static func boolean(
format: JSONTypeFormat.BooleanFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.BooleanFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
fragment(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:allowedValues:defaultValue:example:)
Construct a fragment of a schema.
public static func fragment(
format: JSONTypeFormat.AnyFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.AnyFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
fragment(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:allowedValues:defaultValue:example:)
Construct a fragment of a schema passing a variadic list of allowed values.
public static func fragment(
format: JSONTypeFormat.AnyFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.AnyFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
string(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minLength:maxLength:pattern:allowedValues:defaultValue:example:)
Construct a string schema.
public static func string(
format: JSONTypeFormat.StringFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.StringFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
minLength: Int? = nil,
maxLength: Int? = nil,
pattern: String? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
string(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minLength:maxLength:pattern:allowedValues:defaultValue:example:)
Construct a string schema passing a variadic list of allowed values.
public static func string(
format: JSONTypeFormat.StringFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.StringFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
minLength: Int? = nil,
maxLength: Int? = nil,
pattern: String? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
number(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues:defaultValue:example:)
Construct a number schema.
public static func number(
format: JSONTypeFormat.NumberFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.NumberFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
multipleOf: Double? = nil,
maximum: (Double, exclusive: Bool)? = nil,
minimum: (Double, exclusive: Bool)? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
number(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues:defaultValue:example:)
Construct a number schema passing a variadic list of allowed values.
public static func number(
format: JSONTypeFormat.NumberFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.NumberFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
multipleOf: Double? = nil,
maximum: (Double, exclusive: Bool)? = nil,
minimum: (Double, exclusive: Bool)? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
integer(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues:defaultValue:example:)
Construct an integer schema.
public static func integer(
format: JSONTypeFormat.IntegerFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.IntegerFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
multipleOf: Int? = nil,
maximum: (Int, exclusive: Bool)? = nil,
minimum: (Int, exclusive: Bool)? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
integer(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues:defaultValue:example:)
Construct an integer schema passing a variadic list of allowed values.
public static func integer(
format: JSONTypeFormat.IntegerFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.IntegerFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
multipleOf: Int? = nil,
maximum: (Int, exclusive: Bool)? = nil,
minimum: (Int, exclusive: Bool)? = nil,
allowedValues: AnyCodable...,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
object(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minProperties:maxProperties:properties:additionalProperties:allowedValues:defaultValue:example:)
Construct an objecy schema.
public static func object(
format: JSONTypeFormat.ObjectFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.ObjectFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
minProperties: Int? = nil,
maxProperties: Int? = nil,
properties: [String: JSONSchema] = [:],
additionalProperties: Either<Bool, JSONSchema>? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
array(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minItems:maxItems:uniqueItems:items:allowedValues:defaultValue:example:)
Construct an array schema.
public static func array(
format: JSONTypeFormat.ArrayFormat = .unspecified,
required: Bool = true,
nullable: Bool? = nil,
permissions: JSONSchema.CoreContext<JSONTypeFormat.ArrayFormat>.Permissions? = nil,
deprecated: Bool? = nil,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil,
externalDocs: OpenAPI.ExternalDocumentation? = nil,
minItems: Int? = nil,
maxItems: Int? = nil,
uniqueItems: Bool? = nil,
items: JSONSchema? = nil,
allowedValues: [AnyCodable]? = nil,
defaultValue: AnyCodable? = nil,
example: AnyCodable? = nil
) -> JSONSchema
Construct a schema stating all of the given fragment requirements are met.
public static func all(
of schemas: [JSONSchema]
) -> JSONSchema
Construct a schema stating all of the given fragment requirements are met given a discriminator.
public static func all(
of schemas: JSONSchema...,
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
) -> JSONSchema
Construct a schema stating one of the given schema's requirements are met.
public static func one(
of schemas: [JSONSchema]
) -> JSONSchema
Construct a schema stating one of the given schema's requirements are met given a discriminator.
public static func one(
of schemas: JSONSchema...,
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
) -> JSONSchema
Construct a schema stating any of the given schema's requirements are met.
public static func any(
of schemas: [JSONSchema]
) -> JSONSchema
Construct a schema stating any of the given schema's requirements are met given a discriminator.
public static func any(
of schemas: JSONSchema...,
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
) -> JSONSchema
Construct a schema stating the given schema's requirements are not met.
public static func not(
_ schema: JSONSchema,
required: Bool = true,
title: String? = nil,
description: String? = nil,
discriminator: OpenAPI.Discriminator? = nil
) -> JSONSchema
Only the schema taken as the first argument is inverted by the not logic. Any other arguments are here to directly describe what this schema is as opposed to what it is not.
public func encode(to encoder: Encoder) throws
Get a simplified DereferencedJSONSchema
.
public func simplified(given components: OpenAPI.Components) throws -> DereferencedJSONSchema
A fully simplified JSON Schema is both dereferenced and also reduced to a more normal form where possible.
As an example, many compound schemas can be simplified.
{
"allOf": [
{ "type": "object", "description": "Hello World" },
{
"properties": [
"name": { "type": "string" }
]
}
]
}
simplifies to ->
{
"type": "object",
"description": "Hello World",
"properties": [
"name": { "type": "string" }
]
}
You can create simplified schemas from the DereferencedJSONSchema
type with the simplified()
method or you can create simplified schemas from
the JSONSchema
type with the simplified(given:)
method (which
combines dereferencing and resolving by taking the OpenAPI.Components
as
input).
.
Types
- AnyCodable
- DereferencedContent
- DereferencedContentEncoding
- DereferencedDocument
- DereferencedDocument.Route
- DereferencedHeader
- DereferencedJSONSchema
- DereferencedJSONSchema.ArrayContext
- DereferencedJSONSchema.ObjectContext
- DereferencedOperation
- DereferencedOperation.ResponseOutcome
- DereferencedParameter
- DereferencedPathItem
- DereferencedPathItem.Endpoint
- DereferencedRequest
- DereferencedResponse
- DereferencedSchemaContext
- DereferencedSecurityRequirement
- DereferencedSecurityRequirement.ScopedScheme
- Either
- EitherDecodeNoTypesMatchedError
- EitherDecodeNoTypesMatchedError.IndividualFailure
- ErrorCategory
- ErrorCategory.KeyValue
- InconsistencyError
- JSONReference
- JSONReference.InternalReference
- JSONReference.Path
- JSONReference.PathComponent
- JSONSchema
- JSONSchema.ArrayContext
- JSONSchema.CoreContext
- JSONSchema.CoreContext.Permissions
- JSONSchema.IntegerContext
- JSONSchema.IntegerContext.Bound
- JSONSchema.NumericContext
- JSONSchema.NumericContext.Bound
- JSONSchema.ObjectContext
- JSONSchema.StringContext
- JSONSchemaResolutionError
- JSONType
- JSONTypeFormat
- JSONTypeFormat.AnyFormat
- JSONTypeFormat.ArrayFormat
- JSONTypeFormat.BooleanFormat
- JSONTypeFormat.IntegerFormat
- JSONTypeFormat.IntegerFormat.Extended
- JSONTypeFormat.NumberFormat
- JSONTypeFormat.ObjectFormat
- JSONTypeFormat.StringFormat
- JSONTypeFormat.StringFormat.Extended
- OpenAPI
- OpenAPI.CallbackURL
- OpenAPI.ComponentKey
- OpenAPI.Components
- OpenAPI.Components.ReferenceCycleError
- OpenAPI.Components.ReferenceError
- OpenAPI.Content
- OpenAPI.Content.Encoding
- OpenAPI.ContentType
- OpenAPI.Discriminator
- OpenAPI.Document
- OpenAPI.Document.Info
- OpenAPI.Document.Info.Contact
- OpenAPI.Document.Info.License
- OpenAPI.Document.Route
- OpenAPI.Document.Version
- OpenAPI.Error
- OpenAPI.Error.Decoding
- OpenAPI.Error.Decoding.Document
- OpenAPI.Error.Decoding.Document.Context
- OpenAPI.Error.Decoding.Operation
- OpenAPI.Error.Decoding.Operation.Context
- OpenAPI.Error.Decoding.Path
- OpenAPI.Error.Decoding.Path.Context
- OpenAPI.Error.Decoding.Request
- OpenAPI.Error.Decoding.Request.Context
- OpenAPI.Error.Decoding.Response
- OpenAPI.Error.Decoding.Response.Context
- OpenAPI.Example
- OpenAPI.ExternalDocumentation
- OpenAPI.Header
- OpenAPI.HttpMethod
- OpenAPI.Link
- OpenAPI.OAuthFlows
- OpenAPI.OAuthFlows.AuthorizationCode
- OpenAPI.OAuthFlows.ClientCredentials
- OpenAPI.OAuthFlows.CommonFields
- OpenAPI.OAuthFlows.Implicit
- OpenAPI.OAuthFlows.Password
- OpenAPI.Operation
- OpenAPI.Operation.ResponseOutcome
- OpenAPI.Parameter
- OpenAPI.Parameter.Context
- OpenAPI.Parameter.Context.Location
- OpenAPI.Parameter.SchemaContext
- OpenAPI.Parameter.SchemaContext.Style
- OpenAPI.Path
- OpenAPI.PathItem
- OpenAPI.PathItem.Endpoint
- OpenAPI.Request
- OpenAPI.Response
- OpenAPI.Response.StatusCode
- OpenAPI.Response.StatusCode.Range
- OpenAPI.RuntimeExpression
- OpenAPI.RuntimeExpression.Source
- OpenAPI.SecurityScheme
- OpenAPI.SecurityScheme.Location
- OpenAPI.SecurityScheme.SecurityType
- OpenAPI.SecurityScheme.SecurityType.Name
- OpenAPI.Server
- OpenAPI.Server.Variable
- OpenAPI.Tag
- OpenAPI.XML
- OrderedDictionary
- OrderedDictionary.Iterator
- ResolvedDocument
- ResolvedEndpoint
- ResolvedRoute
- URLTemplate
- URLTemplate.Component
- Validation
- ValidationContext
- ValidationError
- ValidationErrorCollection
- Validator
- Validator.CodingKey
Protocols
Global Functions
Extensions
- Array
- Bool
- Dictionary
- Double
- Float
- Int
- Int32
- Int64
- OpenAPI.Callbacks
- OpenAPI.Content.Encoding
- OpenAPI.Document.Info
- OpenAPI.Document.Info.Contact
- OpenAPI.Document.Info.License
- OpenAPI.Error.Decoding
- OpenAPI.Error.Decoding.Document
- OpenAPI.Error.Decoding.Operation
- OpenAPI.Error.Decoding.Path
- OpenAPI.Error.Decoding.Request
- OpenAPI.Error.Decoding.Response
- OpenAPI.OAuthFlows.AuthorizationCode
- OpenAPI.OAuthFlows.ClientCredentials
- OpenAPI.OAuthFlows.CommonFields
- OpenAPI.OAuthFlows.Implicit
- OpenAPI.OAuthFlows.Password
- OpenAPI.Parameter.Context
- OpenAPI.Parameter.SchemaContext
- OpenAPI.Response.StatusCode
- OpenAPI.Server.Variable
- Optional
- String
- URL
- UUID