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
Copyright 2023 Princess B33f Heavy Industries / Dave Shanley SPDX-License-Identifier: MIT
Index ¶
- func DiagnosticLocationNodes(renderedSchema []byte, renderedNode *yaml.Node, ...) (*yaml.Node, map[string]*yaml.Node)
- func IsURLEncodedContentType(mediaType string) bool
- func IsXMLContentType(mediaType string) bool
- func LocateSchemaPropertyNodeByJSONPath(doc *yaml.Node, JSONPath string) *yaml.Node
- func LocateSchemaPropertyNodeByJSONPathFallback(doc *yaml.Node, primaryLocation, fallbackLocation string) *yaml.Node
- func LocateSchemaPropertyNodeByJSONPathWithResources(doc *yaml.Node, resourceNodes map[string]*yaml.Node, ...) *yaml.Node
- func SchemaCacheKey(schemaHash uint64, version float32, purpose SchemaValidationPurpose) uint64
- func TransformURLEncodedToSchemaJSON(bodyString string, schema *base.Schema, ...) (map[string]any, []*errors.ValidationError)
- func TransformXMLToSchemaJSON(xmlString string, schema *base.Schema) (any, []*liberrors.ValidationError)
- func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bool, []*liberrors.ValidationError)
- func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSchema *jsonschema.Schema, ...) (bool, []*liberrors.ValidationError)
- type CompiledValidationSchema
- type PropertyNameInfo
- type RenderedValidationSchema
- type SchemaValidationPurpose
- type SchemaValidator
- type URLEncodedValidator
- type XMLValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DiagnosticLocationNodes ¶ added in v0.13.13
func DiagnosticLocationNodes( renderedSchema []byte, renderedNode *yaml.Node, resourceNodes map[string]*yaml.Node, ) (*yaml.Node, map[string]*yaml.Node)
DiagnosticLocationNodes returns rendered-schema nodes for validation error locations.
func IsURLEncodedContentType ¶ added in v0.12.0
func IsXMLContentType ¶ added in v0.9.2
IsXMLContentType checks if a media type string represents xml content.
func LocateSchemaPropertyNodeByJSONPath ¶
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 LocateSchemaPropertyNodeByJSONPathFallback ¶ added in v0.13.11
func LocateSchemaPropertyNodeByJSONPathFallback(doc *yaml.Node, primaryLocation, fallbackLocation string) *yaml.Node
LocateSchemaPropertyNodeByJSONPathFallback locates a schema node using a primary and fallback keyword location.
func LocateSchemaPropertyNodeByJSONPathWithResources ¶ added in v0.13.11
func LocateSchemaPropertyNodeByJSONPathWithResources( doc *yaml.Node, resourceNodes map[string]*yaml.Node, primaryLocation, fallbackLocation string, ) *yaml.Node
LocateSchemaPropertyNodeByJSONPathWithResources locates a schema node, selecting external resource nodes when available.
func SchemaCacheKey ¶ added in v0.13.10
func SchemaCacheKey(schemaHash uint64, version float32, purpose SchemaValidationPurpose) uint64
SchemaCacheKey returns a cache key for a schema compiled in a validation context.
func TransformURLEncodedToSchemaJSON ¶ added in v0.12.0
func TransformXMLToSchemaJSON ¶ added in v0.12.0
func TransformXMLToSchemaJSON(xmlString string, schema *base.Schema) (any, []*liberrors.ValidationError)
TransformXMLToSchemaJSON converts xml to json structure matching openapi schema. applies xml object transformations: name, attribute, wrapped.
func ValidateOpenAPIDocument ¶
func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bool, []*liberrors.ValidationError)
ValidateOpenAPIDocument validates an OpenAPI document against the embedded OpenAPI 2, 3.0, 3.1, or 3.2 schema selected by libopenapi. It will return true if the document is valid, false if it is not and a slice of ValidationError pointers.
func ValidateOpenAPIDocumentWithPrecompiled ¶ added in v0.13.1
func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSchema *jsonschema.Schema, opts ...config.Option) (bool, []*liberrors.ValidationError)
ValidateOpenAPIDocumentWithPrecompiled validates an OpenAPI document against the OAS JSON Schema. When compiledSchema is non-nil it is used directly, skipping schema compilation. When SpecJSONBytes is available on the document's SpecInfo, the normalizeJSON round-trip is bypassed in favour of a single jsonschema.UnmarshalJSON call.
Types ¶
type CompiledValidationSchema ¶ added in v0.13.11
type CompiledValidationSchema struct {
RenderedInline []byte
ReferenceSchema string
RenderedJSON []byte
RenderedNode *yaml.Node
ResourceNodes map[string]*yaml.Node
CompiledSchema *jsonschema.Schema
}
CompiledValidationSchema contains a schema compiled for validation plus the rendered schema context.
func CompileSchemaForValidation ¶ added in v0.13.11
func CompileSchemaForValidation( schema *base.Schema, purpose SchemaValidationPurpose, options *config.ValidationOptions, version float32, ) (*CompiledValidationSchema, error)
CompileSchemaForValidation compiles a schema for validation while preserving reachable references.
func (*CompiledValidationSchema) ToCacheEntry ¶ added in v0.13.11
func (c *CompiledValidationSchema) ToCacheEntry(schema *base.Schema) *cache.SchemaCacheEntry
ToCacheEntry converts a compiled validation schema into the shared schema cache shape.
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 RenderedValidationSchema ¶ added in v0.13.10
type RenderedValidationSchema struct {
RenderedInline []byte
ReferenceSchema string
RenderedJSON []byte
RenderedNode *yaml.Node
}
RenderedValidationSchema contains a rendered schema and its JSON equivalent.
func RenderSchemaForValidation ¶ added in v0.13.10
func RenderSchemaForValidation(schema *base.Schema, purpose SchemaValidationPurpose) (*RenderedValidationSchema, error)
RenderSchemaForValidation renders schema for the supplied validation purpose. For request bodies it removes readOnly properties from required lists, and for response bodies it removes writeOnly properties from required lists.
type SchemaValidationPurpose ¶ added in v0.13.10
type SchemaValidationPurpose uint64
SchemaValidationPurpose identifies the context in which a schema is compiled. Request and response bodies need distinct cache entries because readOnly and writeOnly annotations change required-property semantics by direction.
const ( SchemaValidationPurposeGeneric SchemaValidationPurpose = iota SchemaValidationPurposeRequestBody SchemaValidationPurposeResponseBody )
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)
// Release clears validator-owned options and logger references.
Release()
}
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 URLEncodedValidator ¶ added in v0.12.0
type URLEncodedValidator interface {
// ValidateURLEncodedString validates an URL encoded string against a schema, applying OpenAPI object transformations.
// Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance).
ValidateURLEncodedString(schema *base.Schema, encoding *orderedmap.Map[string, *v3.Encoding], urlEncodedString string) (bool, []*liberrors.ValidationError)
// ValidateURLEncodedStringWithVersion validates an URL encoded 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.
ValidateURLEncodedStringWithVersion(schema *base.Schema, encoding *orderedmap.Map[string, *v3.Encoding], urlEncodedString string, version float32) (bool, []*liberrors.ValidationError)
}
URLEncodedValidator is an interface that defines methods for validating URL encoded strings against OpenAPI schemas. There are 2 methods for validating URL encoded:
ValidateURLEncodedString validates an URL encoded string against a schema, applying OpenAPI object transformations. ValidateURLEncodedStringWithVersion - version-aware URL encoded validation that allows OpenAPI 3.0 keywords when version is specified.
func NewURLEncodedValidator ¶ added in v0.12.0
func NewURLEncodedValidator(opts ...config.Option) URLEncodedValidator
NewURLEncodedValidator creates a new URLEncodedValidator instance with default logging configuration.
func NewURLEncodedValidatorWithLogger ¶ added in v0.12.0
func NewURLEncodedValidatorWithLogger(logger *slog.Logger, opts ...config.Option) URLEncodedValidator
NewURLEncodedValidatorWithLogger creates a new URLEncodedValidator instance with a custom logger.
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.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package openapi_schemas contains legacy OpenAPI 3.0 and 3.1 schema loaders.
|
Package openapi_schemas contains legacy OpenAPI 3.0 and 3.1 schema loaders. |