Categorygithub.com/go-openapi/validate
modulepackage
0.24.0
Repository: https://github.com/go-openapi/validate.git
Documentation: pkg.go.dev

# README

Validation helpers Build Status codecov

Slack Status license Go Reference Go Report Card

This package provides helpers to validate Swagger 2.0. specification (aka OpenAPI 2.0).

Reference can be found here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.

What's inside?

  • A validator for Swagger specifications
  • A validator for JSON schemas draft4
  • Helper functions to validate individual values (used by code generated by go-swagger).
    • Required, RequiredNumber, RequiredString
    • ReadOnly
    • UniqueItems, MaxItems, MinItems
    • Enum, EnumCase
    • Pattern, MinLength, MaxLength
    • Minimum, Maximum, MultipleOf
    • FormatOf

Documentation

FAQ

  • Does this library support OpenAPI 3?

No. This package currently only supports OpenAPI 2.0 (aka Swagger 2.0). There is no plan to make it evolve toward supporting OpenAPI 3.x. This discussion thread relates the full story.

An early attempt to support Swagger 3 may be found at: https://github.com/go-openapi/spec3

# Packages

# Functions

AgainstSchema validates the specified data against the provided schema, using a registry of supported formats.
EnableArrayMustHaveItemsCheck activates the swagger rule: an array must have items defined.
EnableObjectArrayTypeCheck activates the swagger rule: an items must be in type: array.
Enum validates if the data is a member of the enum.
EnumCase validates if the data is a member of the enum and may respect case-sensitivity for strings.
FormatOf validates if a string matches a format in the format registry.
IsValueValidAgainstRange checks that a numeric value is compatible with the range defined by Type and Format, that is, may be converted without loss.
Maximum validates if a number is smaller than a given maximum.
MaximumInt validates if a number is smaller than a given maximum.
MaximumNativeType provides native type constraint validation as a facade to various numeric types versions of Maximum constraint check.
MaximumUint validates if a number is smaller than a given maximum.
MaxItems validates that there are at most n items in a slice.
MaxLength validates a string for maximum length.
Minimum validates if a number is smaller than a given minimum.
MinimumInt validates if a number is smaller than a given minimum.
MinimumNativeType provides native type constraint validation as a facade to various numeric types versions of Minimum constraint check.
MinimumUint validates if a number is smaller than a given minimum.
MinItems validates that there are at least n items in a slice.
MinLength validates a string for minimum length.
MultipleOf validates if the provided number is a multiple of the factor.
MultipleOfInt validates if the provided integer is a multiple of the factor.
MultipleOfNativeType provides native type constraint validation as a facade to various numeric types version of MultipleOf constraint check.
MultipleOfUint validates if the provided unsigned integer is a multiple of the factor.
NewFieldKey returns a pair of an object and field usable as a key of a map.
NewHeaderValidator creates a new header validator object.
NewItemKey returns a pair of a slice and index usable as a key of a map.
NewParamValidator creates a new param validator object.
NewSchemaValidator creates a new schema validator.
NewSpecValidator creates a new swagger spec validator instance.
Pattern validates a string against a regular expression.
ReadOnly validates an interface for readonly.
Required validates an interface for requiredness.
RequiredNumber validates a number for requiredness.
RequiredString validates a string for requiredness.
SetContinueOnErrors sets global default behavior regarding spec validation errors reporting.
Spec validates an OpenAPI 2.0 specification document.
SwaggerSchema activates swagger schema validation rules.
UniqueItems validates that the provided slice has unique elements.
WithOperationRequest returns a new context with operationType request in context value.
WithOperationRequest returns a new context with operationType response in context value.
WithRecycleValidators saves memory allocations and makes validators available for a single use of Validate() only.
WithSkipSchemataResult skips the deep audit payload stored in validation Result.

# Constants

ArrayDoesNotAllowAdditionalItemsError when an additionalItems construct is not verified by the array values provided.
ArrayInHeaderRequiresItemsError ...
ArrayInParamRequiresItemsError ...
ArrayRequiresItemsError ...
BothFormDataAndBodyError indicates that an operation specifies both a body and a formData parameter, which is forbidden.
CannotResolveRefError when a $ref could not be resolved.
CircularAncestryDefinitionError ...
DefaultValueDoesNotValidateError results from an invalid default value provided.
DefaultValueHeaderDoesNotValidateError results from an invalid default value provided in header.
DefaultValueHeaderItemsDoesNotValidateError results from an invalid default value provided in header.items.
DefaultValueInDoesNotValidateError ...
DefaultValueItemsDoesNotValidateError results from an invalid default value provided for Items.
DuplicateParamNameError ...
DuplicatePropertiesError ...
EmptyPathParameterError means that a path parameter was found empty (e.g.
ExamplesMimeNotSupportedWarning indicates that examples are provided with a mime type different than application/json, which the validator dos not support yetl.
ExamplesWithoutSchemaWarning indicates that examples are provided for a response,but not schema to validate the example against.
ExampleValueDoesNotValidateError results from an invalid example value provided.
ExampleValueHeaderDoesNotValidateError results from an invalid example value provided in header.
ExampleValueHeaderItemsDoesNotValidateError results from an invalid example value provided in header.items.
ExampleValueInDoesNotValidateError ...
ExampleValueItemsDoesNotValidateError results from an invalid example value provided for Items.
HasDependencyError indicates that a dependencies construct was not verified.
InternalErrorCode reports an internal technical error.
InvalidDocumentError states that spec validation only processes spec.Document objects.
InvalidItemsPatternError indicates an Items definition with invalid pattern.
Warning messages related to spec validation and returned as results.
InvalidParameterDefinitionAsSchemaError indicates an error detected on a parameter definition, which was mistaken with a schema definition.
InvalidParameterDefinitionError indicates an error detected on a parameter definition.
InvalidPatternError ...
InvalidPatternInError indicates an invalid pattern in a schema or items definition.
InvalidPatternInHeaderError indicates a header definition with an invalid pattern.
InvalidPatternInParamError ...
InvalidReferenceError indicates that a $ref property could not be resolved.
InvalidResponseDefinitionAsSchemaError indicates an error detected on a response definition, which was mistaken with a schema definition.
InvalidSchemaProvidedError indicates that the schema provided to validate a value cannot be properly compiled.
InvalidTypeConversionError indicates that a numerical conversion for the given type could not be carried on.
MultipleBodyParamError indicates that an operation specifies multiple parameter with in: body.
MustNotValidateSchemaError indicates that in a Not construct, the schema constraint specified was verified.
MustValidateAllSchemasError indicates that in a AllOf construct, at least one of the schema constraints specified were not verified TODO: punctuation in message.
MustValidateAtLeastOneSchemaError indicates that in a AnyOf construct, none of the schema constraints specified were verified.
MustValidateOnlyOneSchemaError indicates that in a OneOf construct, either none of the schema constraints specified were verified, or several were.
NonUniqueOperationIDError indicates that the same operationId has been specified several times.
NoParameterInPathError indicates that a path was found without any parameter.
NotFoundErrorCode indicates that a resource (e.g.
NoValidPathErrorOrWarning indicates that no single path could be validated.
NoValidResponseError indicates that no valid response description could be found for an operation.
ParamValidationTypeMismatch indicates that parameter has validation which does not match its type.
PathOverlapError ...
PathParamGarbledWarning ...
PathParamNotInPathError indicates that a parameter specified with in: path was not found in the path specification.
PathParamNotUniqueError ...
PathParamNotRequiredError ...
PathStrippedParamGarbledWarning ...
ReadOnlyAndRequiredWarning ...
RefNotAllowedInHeaderError indicates a $ref was found in a header definition, which is not allowed by Swagger.
RefShouldNotHaveSiblingsWarning indicates that a $ref was found with a sibling definition.
RequiredButNotDefinedError ...
RequiredHasDefaultWarning indicates that a required parameter property should not have a default.
SomeParametersBrokenError indicates that some parameters could not be resolved, which might result in partial checks to be carried on.
UnresolvedReferencesError indicates that at least one $ref could not be resolved.
UnusedDefinitionWarning ...
UnusedParamWarning ...
UnusedResponseWarning ...

# Variables

Debug is true when the SWAGGER_DEBUG env var is not empty.

# Structs

FieldKey is a pair of an object and a field, usable as a key for a map.
A HeaderValidator has very limited subset of validations to apply.
ItemKey is a pair of a slice and an index, usable as a key for a map.
Opts specifies validation options for a SpecValidator.
A ParamValidator has very limited subset of validations to apply.
Result represents a validation result set, composed of errors and warnings.
SchemaValidator validates data against a JSON schema.
SchemaValidatorOptions defines optional rules for schema validation.
SpecValidator validates a swagger 2.0 spec.

# Interfaces

An EntityValidator is an interface for things that can validate entities.

# Type aliases

Option sets optional rules for schema validation.