-
Notifications
You must be signed in to change notification settings - Fork 37
Validation
Holds a function to determine if a validation
applies (predicate
) and a function that applies
a validation (validate
).
public struct Validation<Subject: Validatable>
Create a Validation that appllies to values of type Subject
.
public init(
check validate: @escaping (ValidationContext<Subject>) -> [ValidationError],
when predicate: @escaping (ValidationContext<Subject>) -> Bool = { _ in true }
)
You can return any number of errors from your validate
function, each with its own description of a problem. Add an
optional predicate
to apply your validation to a subset of
all values of the type your validate
method operates on.
- validate: A function taking validation contexts containing subjects of type
Subject
and validating them. This function must return an array of errors. If validation succeeds, return an empty array. - predicate: A function returning
true
if this validator should run against the given value.
Create a Validation with a single error that applies to values of type Subject
.
public init(
description: String,
check validate: @escaping (ValidationContext<Subject>) -> Bool,
when predicate: @escaping (ValidationContext<Subject>) -> Bool = { _ in true }
)
This version of the initializer assumes only one error can occur for this
validation and in exchange you can frontload the description of the validation
and simplify the body of the validate
method to just return false
if the value is invalid.
- description: A description of the correct state described by the
validate
function. Upon failure, the error will read "Failed to satisfy: ". - validate: A function taking validation contexts containing subjects of type
Subject
and validating them. This function returnstrue
if validation succeeds andfalse
if it fails. - predicate: A function returning
true
if this validator should run against the given value.
Validate the OpenAPI Document has at least one path in its
PathItem.Map
.
public static var documentContainsPaths: Validation<OpenAPI.PathItem.Map>
The OpenAPI Specifcation does not require that the document
contain any paths for security reasons
but documentation that is public in nature might only ever have
an empty PathItem.Map
in error.
Validate the OpenAPI Document's PathItems
all have at least
one operation.
public static var pathsContainOperations: Validation<OpenAPI.PathItem>
The OpenAPI Specifcation does not require that path items
contain any operations for security reasons
but documentation that is public in nature might only ever have
a PathItem
with no operations in error.
Validate the OpenAPI Document's JSONSchemas
all have at least
one defining characteristic.
public static var schemaComponentsAreDefined: Validation<JSONSchema>
The JSON Schema Specifcation does not require that components have any defining characteristics. An "empty" schema component can be written as follows:
{
}
It is reasonable, however, to want to validate that all schema components are non-empty and therefore offer some value to the consumer/reader of the OpenAPI documentation beyond just "this property exists."
Validate that any Parameters
in the path of any endpoint are documented.
In other words, if a path contains variables (i.e. "{variable}"
) then there are
corresponding parameters
entries in the PathItem
or Operation
for
each endpoint.
public static var pathParametersAreDefined: Validation<OpenAPI.PathItem.Map>
In order to gain easy access to both the path (where the variable placeholders live) and the parameter definitions, this validation runs once per document and performs a loop over each endpoint in the document.
Validate that all Server Objects define all of the variables found in their URL Templates.
public static var serverVariablesAreDefined: Validation<OpenAPI.Server>
For example, a server URL Template of {scheme}://website.com/{path}
would
fail this validation if either "scheme" or "path" were not found in the Server Object's
variables
dictionary.
Validate the OpenAPI Document's Operations
all have at least
one response.
public static var operationsContainResponses: Validation<OpenAPI.Response.Map>
The OpenAPI Specifcation requires that Responses Objects contain at least one response. The specification recommends that if there is only one response then it be a successful response.
Validate that the OpenAPI Document's Tags
all have unique names.
public static var documentTagNamesAreUnique: Validation<OpenAPI.Document>
The OpenAPI Specifcation requires that tag names on the Document are unique.
Validate that all OpenAPI Path Items have no duplicate parameters defined within them.
public static var pathItemParametersAreUnique: Validation<OpenAPI.PathItem>
A Path Item Parameter's identity is defined as the pairing of its name
and
location
.
The OpenAPI Specification requires that these parameters are unique.
Validate that all OpenAPI Operations have no duplicate parameters defined within them.
public static var operationParametersAreUnique: Validation<OpenAPI.Operation>
An Operation's Parameter's identity is defined as the pairing of its name
and
location
.
The OpenAPI Specification requires that these parameters are unique.
Validate that all OpenAPI Operation Ids are unique across the whole Document.
public static var operationIdsAreUnique: Validation<OpenAPI.Document>
The OpenAPI Specification requires that Operation Ids are unique.
Validate that all JSONSchema references are found in the document's components dictionary.
public static var schemaReferencesAreValid: Validation<JSONReference<JSONSchema>>
Validate that all Response references are found in the document's components dictionary.
public static var responseReferencesAreValid: Validation<JSONReference<OpenAPI.Response>>
Validate that all Parameter references are found in the document's components dictionary.
public static var parameterReferencesAreValid: Validation<JSONReference<OpenAPI.Parameter>>
Validate that all Example references are found in the document's components dictionary.
public static var exampleReferencesAreValid: Validation<JSONReference<OpenAPI.Example>>
Validate that all Request references are found in the document's components dictionary.
public static var requestReferencesAreValid: Validation<JSONReference<OpenAPI.Request>>
Validate that all Header references are found in the document's components dictionary.
public static var headerReferencesAreValid: Validation<JSONReference<OpenAPI.Header>>
Applies validation on type Subject
. Throws if validation fails.
public let validate: (ValidationContext<Subject>) -> [ValidationError]
The context includes
-
The entire
OpenAPI.Document
-
A value of the type in which this validator specializes.
-
The coding path where the validation is occurring.
Returns true
if this validator should apply to
the given value of type Subject
.
public let predicate: (ValidationContext<Subject>) -> Bool
The context includes
-
The entire
OpenAPI.Document
-
A value of the type in which this validator specializes.
-
The coding path where the validation is occurring.
Apply the validation to the given value if the predicate
returns true
.
public func apply(to subject: Subject, at codingPath: [CodingKey], in document: OpenAPI.Document) -> [ValidationError]
.
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