schema_validation

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 23 Imported by: 5

Documentation

Overview

Package schema_validation contains all the logic, models and interfaces for validating OpenAPI 3+ Schemas. Functionality for validating individual *base.Schema instances, but as well as validating a complete OpenAPI 3+ document

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsXMLContentType added in v0.9.2

func IsXMLContentType(mediaType string) bool

IsXMLContentType checks if a media type string represents xml content.

func LocateSchemaPropertyNodeByJSONPath

func LocateSchemaPropertyNodeByJSONPath(doc *yaml.Node, JSONPath string) *yaml.Node

LocateSchemaPropertyNodeByJSONPath will locate a schema property node by a JSONPath. It converts something like #/components/schemas/MySchema/properties/MyProperty to something like $.components.schemas.MySchema.properties.MyProperty

func ValidateOpenAPIDocument

func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bool, []*liberrors.ValidationError)

ValidateOpenAPIDocument will validate an OpenAPI document against the OpenAPI 2, 3.0 and 3.1 schemas (depending on version) It will return true if the document is valid, false if it is not and a slice of ValidationError pointers.

Types

type PropertyNameInfo added in v0.9.1

type PropertyNameInfo struct {
	PropertyName   string   // The property name that violated validation (e.g., "$defs-atmVolatility_type")
	ParentLocation []string // The path to the parent containing the property (e.g., ["components", "schemas"])
	EnhancedReason string   // A more detailed error message with context
	Pattern        string   // The pattern that was violated, if applicable
}

PropertyNameInfo contains extracted information about a property name validation error

type SchemaValidator

type SchemaValidator interface {
	// ValidateSchemaString accepts a schema object to validate against, and a JSON/YAML blob that is defined as a string.
	// Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance).
	ValidateSchemaString(schema *base.Schema, payload string) (bool, []*liberrors.ValidationError)

	// ValidateSchemaObject accepts a schema object to validate against, and an object, created from unmarshalled JSON/YAML.
	// This is a pre-decoded object that will skip the need to unmarshal a string of JSON/YAML.
	// Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance).
	ValidateSchemaObject(schema *base.Schema, payload interface{}) (bool, []*liberrors.ValidationError)

	// ValidateSchemaBytes accepts a schema object to validate against, and a byte slice containing a schema to
	// validate against. Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance).
	ValidateSchemaBytes(schema *base.Schema, payload []byte) (bool, []*liberrors.ValidationError)

	// ValidateSchemaStringWithVersion accepts a schema object to validate against, a JSON/YAML blob, and an OpenAPI version.
	// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed and processed.
	// When version is 3.1+, OpenAPI 3.0-specific keywords like 'nullable' will cause validation to fail.
	ValidateSchemaStringWithVersion(schema *base.Schema, payload string, version float32) (bool, []*liberrors.ValidationError)

	// ValidateSchemaObjectWithVersion accepts a schema object to validate against, an object, and an OpenAPI version.
	// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed and processed.
	// When version is 3.1+, OpenAPI 3.0-specific keywords like 'nullable' will cause validation to fail.
	ValidateSchemaObjectWithVersion(schema *base.Schema, payload interface{}, version float32) (bool, []*liberrors.ValidationError)

	// ValidateSchemaBytesWithVersion accepts a schema object to validate against, a byte slice, and an OpenAPI version.
	// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed and processed.
	// When version is 3.1+, OpenAPI 3.0-specific keywords like 'nullable' will cause validation to fail.
	ValidateSchemaBytesWithVersion(schema *base.Schema, payload []byte, version float32) (bool, []*liberrors.ValidationError)
}

SchemaValidator is an interface that defines the methods for validating a *base.Schema (V3+ Only) object. There are 6 methods for validating a schema:

ValidateSchemaString accepts a schema object to validate against, and a JSON/YAML blob that is defined as a string.
ValidateSchemaObject accepts a schema object to validate against, and an object, created from unmarshalled JSON/YAML.
ValidateSchemaBytes accepts a schema object to validate against, and a JSON/YAML blob that is defined as a byte array.
ValidateSchemaStringWithVersion - version-aware validation that allows OpenAPI 3.0 keywords when version is specified.
ValidateSchemaObjectWithVersion - version-aware validation that allows OpenAPI 3.0 keywords when version is specified.
ValidateSchemaBytesWithVersion - version-aware validation that allows OpenAPI 3.0 keywords when version is specified.

func NewSchemaValidator

func NewSchemaValidator(opts ...config.Option) SchemaValidator

NewSchemaValidator will create a new SchemaValidator instance, ready to accept schemas and payloads to validate.

func NewSchemaValidatorWithLogger added in v0.0.28

func NewSchemaValidatorWithLogger(logger *slog.Logger, opts ...config.Option) SchemaValidator

NewSchemaValidatorWithLogger will create a new SchemaValidator instance, ready to accept schemas and payloads to validate.

type XMLValidator added in v0.9.2

type XMLValidator interface {
	// ValidateXMLString validates an XML string against an OpenAPI schema, applying xml object transformations.
	// Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance).
	ValidateXMLString(schema *base.Schema, xmlString string) (bool, []*liberrors.ValidationError)

	// ValidateXMLStringWithVersion validates an XML string with version-specific rules.
	// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed and processed.
	// When version is 3.1+, OpenAPI 3.0-specific keywords like 'nullable' will cause validation to fail.
	ValidateXMLStringWithVersion(schema *base.Schema, xmlString string, version float32) (bool, []*liberrors.ValidationError)
}

XMLValidator is an interface that defines methods for validating XML against OpenAPI schemas. There are 2 methods for validating XML:

ValidateXMLString validates an XML string against a schema, applying OpenAPI xml object transformations.
ValidateXMLStringWithVersion - version-aware XML validation that allows OpenAPI 3.0 keywords when version is specified.

func NewXMLValidator added in v0.9.2

func NewXMLValidator(opts ...config.Option) XMLValidator

NewXMLValidator creates a new XMLValidator instance with default logging configuration.

func NewXMLValidatorWithLogger added in v0.9.2

func NewXMLValidatorWithLogger(logger *slog.Logger, opts ...config.Option) XMLValidator

NewXMLValidatorWithLogger creates a new XMLValidator instance with a custom logger.

Directories

Path Synopsis
Package openapi_schemas contains the OpenAPI 3.0 and 3.1 schemas that are loaded from libopenapi, or our own fork of the official OpenAPI repo specifications.
Package openapi_schemas contains the OpenAPI 3.0 and 3.1 schemas that are loaded from libopenapi, or our own fork of the official OpenAPI repo specifications.

Jump to

Keyboard shortcuts

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