validate

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package validate provides OpenAPI specification validation functionality. It validates OpenAPI specs against the official JSON Schema definitions.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPathEmpty              = errors.New("path cannot be empty")
	ErrPathNoLeadingSlash     = errors.New("path must start with '/'")
	ErrPathDuplicateParameter = errors.New("duplicate path parameter")
	ErrPathInvalidParameter   = errors.New("invalid path parameter format")
)

Path validation errors

Functions

func ValidateComponentName added in v0.4.0

func ValidateComponentName(name string) error

ValidateComponentName validates an OpenAPI component name. Valid names must match: ^[a-zA-Z0-9.\-_]+$ Returns an error if the name is invalid.

func ValidatePath added in v0.4.0

func ValidatePath(path string) error

ValidatePath validates a path pattern for use in OpenAPI operations.

It accepts both router-style (:param) and OpenAPI-style ({param}) syntax for developer convenience. The internal builder will normalize these to OpenAPI format.

Validation checks:

  • Non-empty path
  • Path starts with '/'
  • Valid path parameter syntax (:param or {param})
  • No duplicate path parameters
  • Parameter names match [a-zA-Z0-9._-]+
  • Properly paired braces in {param} syntax

Returns an error if validation fails.

func ValidateResponseCode added in v0.4.0

func ValidateResponseCode(code string) error

ValidateResponseCode validates an OpenAPI response code. Valid codes: "default", "200", "404", "4XX", "5XX", etc. Returns an error if the code is invalid.

Types

type Validator added in v0.4.0

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates OpenAPI specifications against their meta-schemas. Thread-safe. Compiles schemas once and caches them for reuse.

func New

func New() *Validator

New creates a new Validator with embedded OpenAPI meta-schemas.

The validator uses santhosh-tekuri/jsonschema which supports both JSON Schema draft-04 (for OpenAPI 3.0) and draft-2020-12 (for OpenAPI 3.1).

func (*Validator) Validate added in v0.4.0

func (v *Validator) Validate(ctx context.Context, specJSON []byte, version Version) error

Validate validates an OpenAPI specification JSON against the meta-schema for the given version.

The schema is compiled on first use and cached for subsequent validations. This method is thread-safe.

Example:

validator := validate.New()
if err := validator.Validate(ctx, specJSON, validate.V31); err != nil {
    log.Fatalf("Invalid OpenAPI spec: %v", err)
}

type Version added in v0.4.0

type Version string

Version represents OpenAPI specification version.

const (
	// V30 represents OpenAPI 3.0.x.
	V30 Version = "3.0"
	// V31 represents OpenAPI 3.1.x.
	V31 Version = "3.1"
)

Jump to

Keyboard shortcuts

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