oasvalidator

package
v0.0.0-...-3a583f3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package oasvalidator defines validation functions for OpenAPI spec.

Index

Constants

View Source
const (
	// ErrCodeRequired represents a code for required field errors.
	ErrCodeRequired = "required"
	// ErrCodeRequestBodyError represents a code for invalid request body error.
	ErrCodeRequestBodyError = "request_body_error"
	// ErrCodeResponseDecodeBodyError represents a code for an decoding error from response body.
	ErrCodeResponseDecodeBodyError = "response_decode_body_error"
	// ErrCodeRequestEncodeBodyError represents a code for an encoding error from request body.
	ErrCodeRequestEncodeBodyError = "request_encode_body_error"
	// ErrCodeResponseEncodeBodyError represents a code for an encoding error from response body.
	ErrCodeResponseEncodeBodyError = "response_encode_body_error"
	// ErrCodeMalformedJSON represents a code for a malformed JSON error.
	ErrCodeMalformedJSON = "malformed_json"
	// ErrCodeMalformedXML represents a code for a malformed XML error.
	ErrCodeMalformedXML = "malformed_xml"
	// ErrCodeXMLEncodeError represents a code for a XML encoding error.
	ErrCodeXMLEncodeError = "xml_encode_error"
	// ErrCodeMultipartFormEncodeError represents a code for a multipart form encoding error.
	ErrCodeMultipartFormEncodeError = "multipart_encode_error"
	// ErrCodeRequestTransformError represents a code for a request transformation error.
	ErrCodeRequestTransformError = "request_transform_error"
	// ErrCodeResponseTransformError represents a code for a response transformation error.
	ErrCodeResponseTransformError = "response_transform_error"
	// ErrCodeWriteResponseError represents a code for a response write error.
	ErrCodeWriteResponseError = "write_response_error"
	// ErrCodeInvalidRESTfulRequestConfig represents a code for invalid errors of ProxyRESTfulRequestConfig.
	ErrCodeInvalidRESTfulRequestConfig = "invalid_restful_request_config"
	// ErrCodeProxyRESTfulResponseConfig represents a code for invalid errors of ProxyRESTfulResponseConfig.
	ErrCodeProxyRESTfulResponseConfig = "invalid_restful_response_config"
	// ErrCodeInvalidServerURL represents a code for invalid server URL errors.
	ErrCodeInvalidServerURL = "invalid_server_url"
	// ErrCodeInvalidRequestURL represents a code for invalid request URL errors.
	ErrCodeInvalidRequestURL = "invalid_request_url"
	// ErrCodeInvalidURLParam represents a code for invalid request URL parameter errors.
	ErrCodeInvalidURLParam = "invalid_url_param"
	// ErrCodeInvalidQueryParam represents a code for invalid query parameter errors.
	ErrCodeInvalidQueryParam = "invalid_query_param"
	// ErrCodeInvalidHeader represents a code for invalid header errors.
	ErrCodeInvalidHeader = "invalid_header"
	// ErrCodeInvalidCookie represents a code for invalid cookie errors.
	ErrCodeInvalidCookie = "invalid_cookie"
	// ErrCodeValidationError represents a code for validation errors.
	ErrCodeValidationError = "validation_error"
	// ErrCodeGraphQLResponseEmpty represents a code for empty graphql response.
	ErrCodeGraphQLResponseEmpty = "graphql_response_empty"
	// ErrCodeRemoteServerError represents a code for remote server errors.
	ErrCodeRemoteServerError = "remote_server_error"
	// ErrCodeOpenAPISchemaError represents a code for OpenAPI schema errors.
	ErrCodeOpenAPISchemaError = "openapi_schema_error"
)

Variables

View Source
var (
	// ErrInvalidContentType occurs when the content type string is invalid.
	ErrInvalidContentType = errors.New("invalid content type")
)

Functions

func ArrayMaxItemsValidationError

func ArrayMaxItemsValidationError(expected, actual int64) *httperror.ValidationError

ArrayMaxItemsValidationError returns a validation error for maximum items in array.

func ArrayMinItemsValidationError

func ArrayMinItemsValidationError(expected, actual int64) *httperror.ValidationError

ArrayMinItemsValidationError returns a validation error for minimum items in array.

func ArrayUniqueItemsValidationError

func ArrayUniqueItemsValidationError[T any](duplicates []T) *httperror.ValidationError

ArrayUniqueItemsValidationError returns a validation error for unique items array.

func CanNull

func CanNull(typeSchema *base.Schema, isNull bool) bool

CanNull checks if a nullable value is allowed by an OpenAPI schema.

func CompareBoolean

func CompareBoolean(a bool, b bool) int

CompareBoolean compares two boolean values, treating true as greater than false.

func CompareNullable

func CompareNullable[E cmp.Ordered](a *E, b *E) int

CompareNullable compares two optional ordered values. nil sorts before any non-nil value.

func CompareNullableBoolean

func CompareNullableBoolean(a *bool, b *bool) int

CompareNullableBoolean compares two optional booleans. nil sorts before false, false before true.

func DecodePrimitiveValueFromType

func DecodePrimitiveValueFromType(value any, typeName string) (any, string, error)

DecodePrimitiveValueFromType decodes a value from a primitive type.

func EnumValidationError

func EnumValidationError(
	typeSchema *base.Schema,
	actual string,
) *httperror.ValidationError

EnumValidationError returns a validation error when a value does not match any allowed enum value.

func EqualContentType

func EqualContentType(left, right string) bool

EqualContentType checks if both content type are equal with parameters excluded.

func FindDuplicatedItems

func FindDuplicatedItems[T cmp.Ordered](values []T) []T

FindDuplicatedItems find duplicated items in the array.

func FindDuplicatedItemsFunc

func FindDuplicatedItemsFunc[S ~[]E, E any](values S, compare func(a E, b E) int) []E

FindDuplicatedItemsFunc find duplicated items in the array with a comparison function.

func InvalidTypeError

func InvalidTypeError(
	expected []string,
	actual string,
) *httperror.ValidationError

InvalidTypeError returns a validation error when the value type does not match any expected schema type.

func MaxContainsError

func MaxContainsError(expected int64, actual int64) *httperror.ValidationError

MaxContainsError returns a validation error when more array items match the contains schema than allowed.

func MaxLengthValidationError

func MaxLengthValidationError(expected int64, actual int64) *httperror.ValidationError

MaxLengthValidationError returns a validation error when a string exceeds the allowed maximum length.

func MaximumValidationError

func MaximumValidationError(
	expected float64,
	actual float64,
	exclusive bool,
) *httperror.ValidationError

MaximumValidationError returns a validation error when a number exceeds the allowed maximum.

func MinContainsError

func MinContainsError(expected int64, actual int64) *httperror.ValidationError

MinContainsError returns a validation error when fewer array items match the contains schema than required.

func MinLengthValidationError

func MinLengthValidationError(expected int64, actual int64) *httperror.ValidationError

MinLengthValidationError returns a validation error when a string is shorter than the allowed minimum length.

func MinimumValidationError

func MinimumValidationError(
	expected float64,
	actual float64,
	exclusive bool,
) *httperror.ValidationError

MinimumValidationError returns a validation error when a number is below the allowed minimum.

func MultipleOfValidationError

func MultipleOfValidationError(
	multipleOf float64,
	actual float64,
) *httperror.ValidationError

MultipleOfValidationError returns a validation error when a number is not a multiple of the required divisor.

func NotNullError

func NotNullError() *httperror.ValidationError

NotNullError returns a validation error for not-null value.

func ObjectDependentRequiredError

func ObjectDependentRequiredError(name string, dependent string) *httperror.ValidationError

ObjectDependentRequiredError returns a validation error for a missing dependent required property in object.

func ObjectMaxPropertiesValidationError

func ObjectMaxPropertiesValidationError(expected, actual int64) *httperror.ValidationError

ObjectMaxPropertiesValidationError returns a validation error for maximum properties in object.

func ObjectMinPropertiesValidationError

func ObjectMinPropertiesValidationError(expected, actual int64) *httperror.ValidationError

ObjectMinPropertiesValidationError returns a validation error for minimum properties in object.

func ObjectPropertyKeyTypeError

func ObjectPropertyKeyTypeError(actualType string) *httperror.ValidationError

ObjectPropertyKeyTypeError returns a validation error for invalid object key error.

func ObjectRequiredPropertyError

func ObjectRequiredPropertyError(name string) *httperror.ValidationError

ObjectRequiredPropertyError returns a validation error for a missing required property in object.

func ParameterRequiredError

func ParameterRequiredError(name string) *httperror.ValidationError

ParameterRequiredError returns a validation error for a missing required parameter.

func PatternValidationError

func PatternValidationError(expected string) *httperror.ValidationError

PatternValidationError returns a validation error when a string does not match the required regex pattern.

func ReplaceURLTemplate

func ReplaceURLTemplate(input string, get goenvconf.GetEnvFunc) (string, error)

ReplaceURLTemplate finds and replace variables in the template string.

func TypeMismatchedError

func TypeMismatchedError(expected []string, actual string) *httperror.ValidationError

TypeMismatchedError returns a validation error when the decoded type does not match an expected type list.

func UnionTypeMismatchedError

func UnionTypeMismatchedError(
	unionType string,
	expected []string,
	actual []string,
) *httperror.ValidationError

UnionTypeMismatchedError returns a validation error when the types declared in an allOf/anyOf/oneOf union are inconsistent with the parent schema types.

func ValidateArray

func ValidateArray[T any](
	typeSchema *base.Schema,
	value []T,
	compare func(a T, b T) int,
) []httperror.ValidationError

ValidateArray validates an array value against an OpenAPI schema.

func ValidateArrayAndItems

func ValidateArrayAndItems[T any](
	typeSchema *base.Schema,
	value []T,
	compare func(a T, b T) int,
) []httperror.ValidationError

ValidateArrayAndItems validates an array value and its items against an OpenAPI schema.

func ValidateBoolean

func ValidateBoolean(typeSchema *base.Schema, value bool) []httperror.ValidationError

ValidateBoolean validates a boolean value against an OpenAPI schema.

func ValidateContains

func ValidateContains(typeSchema *base.Schema, value []any) *httperror.ValidationError

ValidateContains validates the contains rule against an array value.

func ValidateContentType

func ValidateContentType(contentType string) (string, error)

ValidateContentType validates the content type and prefer the application/json content type if the content type string has many content types.

func ValidateEnum

func ValidateEnum[T comparable](typeSchema *base.Schema, value T) bool

ValidateEnum validates a value against a list of enum.

func ValidateInteger

func ValidateInteger[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](
	typeSchema *base.Schema,
	value T,
) []httperror.ValidationError

ValidateInteger validates a number value against an OpenAPI schema.

func ValidateNullableBoolean

func ValidateNullableBoolean(typeSchema *base.Schema, value *bool) []httperror.ValidationError

ValidateNullableBoolean validates a nullable boolean value against an OpenAPI schema.

func ValidateNullableInteger

func ValidateNullableInteger[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](
	typeSchema *base.Schema,
	value *T,
) []httperror.ValidationError

ValidateNullableInteger validates a nullable integer value against an OpenAPI schema.

func ValidateNullableNumber

func ValidateNullableNumber[T float32 | float64](
	typeSchema *base.Schema,
	value *T,
) []httperror.ValidationError

ValidateNullableNumber validates a nullable number value against an OpenAPI schema.

func ValidateNullableString

func ValidateNullableString(typeSchema *base.Schema, value *string) []httperror.ValidationError

ValidateNullableString validates a nullable string value against an OpenAPI schema.

func ValidateNumber

func ValidateNumber[T float32 | float64](
	typeSchema *base.Schema,
	value T,
) []httperror.ValidationError

ValidateNumber validates a number value against an OpenAPI schema.

func ValidateObject

func ValidateObject[T any](
	typeSchema *base.Schema,
	value map[string]T,
) []httperror.ValidationError

ValidateObject validates an object value against an OpenAPI schema.

func ValidateObjectWithoutProperties

func ValidateObjectWithoutProperties[T any](
	typeSchema *base.Schema,
	value map[string]T,
) []httperror.ValidationError

ValidateObjectWithoutProperties validates an object value against an OpenAPI schema. This function only validates type, properties length and required properties.

func ValidateOperation

func ValidateOperation(
	document *highv3.Document,
	operation *highv3.Operation,
	additionalParams []*highv3.Parameter,
) (*oaschema.Operation, []httperror.ValidationError)

ValidateOperation validates an OpenAPI operation, resolves component references, and returns a normalized oaschema.Operation with validated parameters and request body schema.

func ValidateParameterDefinition

func ValidateParameterDefinition(
	param *highv3.Parameter,
) (*oaschema.Parameter, []httperror.ValidationError)

ValidateParameterDefinition validates a single parameter definition and returns a normalized oaschema.Parameter, resolving location, style, and schema.

func ValidateParameterDefinitions

func ValidateParameterDefinitions(
	params []*highv3.Parameter,
) ([]*oaschema.Parameter, []httperror.ValidationError)

ValidateParameterDefinitions validate a list of parameter definition and merge it with unique name and location.

func ValidateSchema

func ValidateSchema(schema *base.Schema) []httperror.ValidationError

ValidateSchema normalizes types and nullability, flattens allOf into the schema, and validates type consistency across anyOf/oneOf union branches.

func ValidateSchemaProxy

func ValidateSchemaProxy(proxy *base.SchemaProxy) (*base.Schema, []httperror.ValidationError)

ValidateSchemaProxy dereferences a SchemaProxy and validates the resulting schema.

func ValidateString

func ValidateString(typeSchema *base.Schema, value string) []httperror.ValidationError

ValidateString validates a string value against an OpenAPI schema.

func ValidateValue

func ValidateValue(typeSchema *base.Schema, value any) []httperror.ValidationError

ValidateValue validates a value against an OpenAPI schema.

func ValidateValueWithSchemaProxy

func ValidateValueWithSchemaProxy(
	schemaProxy *base.SchemaProxy,
	value any,
) []httperror.ValidationError

ValidateValueWithSchemaProxy validates a value against an OpenAPI schema proxy.

Types

This section is empty.

Directories

Path Synopsis
Package contentdecoder implement encoders and decoders for data by content types.
Package contentdecoder implement encoders and decoders for data by content types.
Package contentencoder implements encoder functions for content types.
Package contentencoder implements encoder functions for content types.
Package parameter defines serialization functions for HTTP parameters.
Package parameter defines serialization functions for HTTP parameters.
Package regexps defines a global cache for regular expressions.
Package regexps defines a global cache for regular expressions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL