Skip to content

JSONSchema

mattpolzin edited this page Jul 3, 2020 · 12 revisions

JSONSchema

OpenAPI "Schema Object"

public enum JSONSchema

See OpenAPI Schema Object.

Inheritance

ComponentDictionaryLocatable, Decodable, Encodable, Equatable, JSONSchemaContext

Initializers

init(from:)

public init(from decoder: Decoder) throws

Enumeration Cases

boolean

case boolean(: Context<JSONTypeFormat.BooleanFormat>)

object

case object(: Context<JSONTypeFormat.ObjectFormat>, : ObjectContext)

array

case array(: Context<JSONTypeFormat.ArrayFormat>, : ArrayContext)

number

case number(: Context<JSONTypeFormat.NumberFormat>, : NumericContext)

integer

case integer(: Context<JSONTypeFormat.IntegerFormat>, : IntegerContext)

string

case string(: Context<JSONTypeFormat.StringFormat>, : StringContext)

all

case all(of: [JSONSchemaFragment], discriminator: OpenAPI.Discriminator?)

one

case one(of: [JSONSchema], discriminator: OpenAPI.Discriminator?)

any

case any(of: [JSONSchema], discriminator: OpenAPI.Discriminator?)

not

case not(: JSONSchema)

reference

case reference(: JSONReference<JSONSchema>)

undefined

This schema does not have a type specified. This is allowed but does not offer much in the way of documenting the schema so it is represented here as "undefined" with an optional description.

case undefined(description: String?)

Properties

openAPIComponentsKey

var openAPIComponentsKey: String

openAPIComponentsKeyPath

var openAPIComponentsKeyPath: KeyPath<OpenAPI.Components, OpenAPI.ComponentDictionary<Self>>

jsonTypeFormat

The type and format of the schema.

var jsonTypeFormat: JSONTypeFormat?

jsonType

The fundamental type of the schema.

var jsonType: JSONType?

Important: "object," "array," "allOf,", "oneOf," "anyOf," "not," "reference," and "undefined" are not considered types and such schemas will return `nil` for this property.

required

var required: Bool

description

var description: String?

discriminator

var discriminator: OpenAPI.Discriminator?

nullable

var nullable: Bool

readOnly

var readOnly: Bool

writeOnly

var writeOnly: Bool

deprecated

var deprecated: Bool

title

var title: String?

externalDocs

var externalDocs: OpenAPI.ExternalDocumentation?

allowedValues

var allowedValues: [AnyCodable]?

example

var example: AnyCodable?

generalContext

Get the general context most JSONSchemas have.

var generalContext: JSONSchemaContext?

This is the information shared by most schemas.

Notably missing this general context are:

objectContext

Get the context specific to an object schema. If not an object schema, returns nil.

var objectContext: ObjectContext?

arrayContext

Get the context specific to an array schema. If not an array schema, returns nil.

var arrayContext: ArrayContext?

numberContext

Get the context specific to a number schema. If not a number schema, returns nil.

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.

integerContext

Get the context specific to an integer schema. If not an integer schema, returns nil.

var integerContext: IntegerContext?

stringContext

Get the context specific to a string schema. If not a string schema, returns nil.

var stringContext: StringContext?

boolean

A required, non-nullable boolean schema.

var boolean: JSONSchema

string

A required, non-nullable string schema.

var string: JSONSchema

number

A required, non-nullable number schema.

var number: JSONSchema

integer

A required, non-nullable integer schema.

var integer: JSONSchema

object

A required, non-nullable object schema.

var object: JSONSchema

array

A required, non-nullable array schema.

var array: JSONSchema

Methods

optionalSchemaObject()

Return the optional version of this JSONSchema

public func optionalSchemaObject() -> JSONSchema

requiredSchemaObject()

Return the required version of this JSONSchema

public func requiredSchemaObject() -> JSONSchema

nullableSchemaObject()

Return the nullable version of this JSONSchema

public func nullableSchemaObject() -> JSONSchema

with(allowedValues:)

Return a version of this JSONSchema that only allows the given values.

public func with(allowedValues: [AnyCodable]) -> JSONSchema

with(example:)

Returns a version of this JSONSchema that has the given example attached.

public func with(example: AnyCodable) throws -> JSONSchema

dereferencedSchemaObject()

Returns a dereferenced schema object if this schema object already does not contain any references.

public func dereferencedSchemaObject() -> DereferencedJSONSchema?

To create a dereferenced schema object from a schema object that does have references, use dereferencedSchemaObject(resolvingIn:).

dereferencedSchemaObject(resolvingIn:)

Returns a dereferenced schema object if all references in this schema object can be found in the Components Object.

public func dereferencedSchemaObject(resolvingIn components: OpenAPI.Components) throws -> DereferencedJSONSchema

Important: Local dereferencing will `throw` if any `JSONReferences` point to other files or to locations within the same file other than the Components Object. It will also fail if any components are missing from the Components Object.

Throws

ReferenceError.cannotLookupRemoteReference or MissingReferenceError.referenceMissingOnLookup(name:) 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.

fragmentString(from:)

Attempts to create a String from a JSON fragment (e.g. Bool, Double, String, etc.)

internal static func fragmentString(from value: Any) -> String?

boolean(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:allowedValues:example:)

Construct a boolean schema.

public static func boolean(format: JSONTypeFormat.BooleanFormat = .unspecified, required: Bool = true, nullable: Bool = false, permissions: JSONSchema.Context<JSONTypeFormat.BooleanFormat>.Permissions = .readWrite, deprecated: Bool = false, title: String? = nil, description: String? = nil, discriminator: OpenAPI.Discriminator? = nil, externalDocs: OpenAPI.ExternalDocumentation? = nil, allowedValues: [AnyCodable]? = nil, example: AnyCodable? = nil) -> JSONSchema

boolean(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:allowedValues: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 = false, permissions: JSONSchema.Context<JSONTypeFormat.BooleanFormat>.Permissions = .readWrite, deprecated: Bool = false, title: String? = nil, description: String? = nil, discriminator: OpenAPI.Discriminator? = nil, externalDocs: OpenAPI.ExternalDocumentation? = nil, allowedValues: AnyCodable, example: AnyCodable? = nil) -> JSONSchema

string(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minLength:maxLength:pattern:allowedValues:example:)

Construct a string schema.

public static func string(format: JSONTypeFormat.StringFormat = .unspecified, required: Bool = true, nullable: Bool = false, permissions: JSONSchema.Context<JSONTypeFormat.StringFormat>.Permissions = .readWrite, deprecated: Bool = false, title: String? = nil, description: String? = nil, discriminator: OpenAPI.Discriminator? = nil, externalDocs: OpenAPI.ExternalDocumentation? = nil, minLength: Int = 0, maxLength: Int? = nil, pattern: String? = nil, allowedValues: [AnyCodable]? = nil, example: AnyCodable? = nil) -> JSONSchema

string(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minLength:maxLength:pattern:allowedValues: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 = false, permissions: JSONSchema.Context<JSONTypeFormat.StringFormat>.Permissions = .readWrite, deprecated: Bool = false, title: String? = nil, description: String? = nil, discriminator: OpenAPI.Discriminator? = nil, externalDocs: OpenAPI.ExternalDocumentation? = nil, minLength: Int = 0, maxLength: Int? = nil, pattern: String? = nil, allowedValues: AnyCodable, example: AnyCodable? = nil) -> JSONSchema

number(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues:example:)

Construct a number schema.

public static func number(format: JSONTypeFormat.NumberFormat = .unspecified, required: Bool = true, nullable: Bool = false, permissions: JSONSchema.Context<JSONTypeFormat.NumberFormat>.Permissions = .readWrite, deprecated: Bool = false, 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, example: AnyCodable? = nil) -> JSONSchema

number(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues: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 = false, permissions: JSONSchema.Context<JSONTypeFormat.NumberFormat>.Permissions = .readWrite, deprecated: Bool = false, 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, example: AnyCodable? = nil) -> JSONSchema

integer(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues:example:)

Construct an integer schema.

public static func integer(format: JSONTypeFormat.IntegerFormat = .unspecified, required: Bool = true, nullable: Bool = false, permissions: JSONSchema.Context<JSONTypeFormat.IntegerFormat>.Permissions = .readWrite, deprecated: Bool = false, 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, example: AnyCodable? = nil) -> JSONSchema

integer(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:multipleOf:maximum:minimum:allowedValues: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 = false, permissions: JSONSchema.Context<JSONTypeFormat.IntegerFormat>.Permissions = .readWrite, deprecated: Bool = false, 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, example: AnyCodable? = nil) -> JSONSchema

object(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minProperties:maxProperties:properties:additionalProperties:allowedValues:example:)

Construct an objecy schema.

public static func object(format: JSONTypeFormat.ObjectFormat = .unspecified, required: Bool = true, nullable: Bool = false, permissions: JSONSchema.Context<JSONTypeFormat.ObjectFormat>.Permissions = .readWrite, deprecated: Bool = false, title: String? = nil, description: String? = nil, discriminator: OpenAPI.Discriminator? = nil, externalDocs: OpenAPI.ExternalDocumentation? = nil, minProperties: Int = 0, maxProperties: Int? = nil, properties: [String: JSONSchema] = [:], additionalProperties: Either<Bool, JSONSchema>? = nil, allowedValues: [AnyCodable]? = nil, example: AnyCodable? = nil) -> JSONSchema

array(format:required:nullable:permissions:deprecated:title:description:discriminator:externalDocs:minItems:maxItems:uniqueItems:items:allowedValues:example:)

Construct an array schema.

public static func array(format: JSONTypeFormat.ArrayFormat = .unspecified, required: Bool = true, nullable: Bool = false, permissions: JSONSchema.Context<JSONTypeFormat.ArrayFormat>.Permissions = .readWrite, deprecated: Bool = false, title: String? = nil, description: String? = nil, discriminator: OpenAPI.Discriminator? = nil, externalDocs: OpenAPI.ExternalDocumentation? = nil, minItems: Int = 0, maxItems: Int? = nil, uniqueItems: Bool = false, items: JSONSchema? = nil, allowedValues: [AnyCodable]? = nil, example: AnyCodable? = nil) -> JSONSchema

all(of:)

Construct a schema stating all of the given fragment requirements are met.

public static func all(of schemas: [JSONSchemaFragment]) -> JSONSchema

all(of:discriminator:)

Construct a schema stating all of the given fragment requirements are met given a discriminator.

public static func all(of schemas: JSONSchemaFragment, discriminator: OpenAPI.Discriminator? = nil) -> JSONSchema

one(of:)

Construct a schema stating one of the given schema's requirements are met.

public static func one(of schemas: [JSONSchema]) -> JSONSchema

one(of:discriminator:)

Construct a schema stating one of the given schema's requirements are met given a discriminator.

public static func one(of schemas: JSONSchema, discriminator: OpenAPI.Discriminator? = nil) -> JSONSchema

any(of:)

Construct a schema stating any of the given schema's requirements are met.

public static func any(of schemas: [JSONSchema]) -> JSONSchema

any(of:discriminator:)

Construct a schema stating any of the given schema's requirements are met given a discriminator.

public static func any(of schemas: JSONSchema, discriminator: OpenAPI.Discriminator? = nil) -> JSONSchema

encode(to:)

public func encode(to encoder: Encoder) throws
Types
Protocols
Global Functions
Extensions
Clone this wiki locally