spec

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spec loads an OpenAPI document and exposes its operations in a deterministic order. It encapsulates the OpenAPI library (kin-openapi) so the rest of apitest does not depend on its API directly.

Index

Constants

View Source
const (
	FindingExampleSchema = "example_schema"
	FindingHeuristic     = "heuristic"
	FindingBinding       = "binding"
	FindingValidation    = "validation"
	FindingAuth          = "auth"
)

Finding kinds.

View Source
const AssumedScheme = "apitestBearer"

AssumedScheme is the security scheme apitest adds when a spec declares no security at all (see AssumeBearer).

View Source
const Untagged = "untagged"

Untagged is the group of operations without tags.

Variables

This section is empty.

Functions

func CompilePattern

func CompilePattern(expr string) (openapi3.RegexMatcher, error)

CompilePattern compiles a schema pattern the way apitest validates it, for tools that must produce values apitest accepts.

func DecodeJSON

func DecodeJSON(b []byte, out any) error

DecodeJSON decodes JSON with numbers kept as json.Number.

func IsJSON

func IsJSON(mediaType string) bool

IsJSON reports whether a media type is JSON (application/json, application/*+json).

func Normalize

func Normalize(v any) any

Normalize converts a value from YAML/JSON decoding into the canonical form used everywhere in apitest: maps, slices, strings, bools, nil and json.Number for numbers.

Types

type Finding

type Finding struct {
	Kind    string // e.g. FindingExampleSchema
	Where   string // location in the spec
	Message string
}

Finding is a problem in the spec that does not prevent loading.

type Mode

type Mode int

Mode selects how readOnly and writeOnly are treated during validation.

const (
	ModePlain    Mode = iota // no readOnly/writeOnly rules
	ModeRequest              // readOnly properties must not be sent
	ModeResponse             // writeOnly properties must not be returned
)

Validation modes.

type Operation

type Operation struct {
	// ID is the operationId, or the substitute "<METHOD> <path>" (FR-SPEC-06).
	ID             string
	HasOperationID bool
	Method         string
	Path           string
	Tags           []string
	Op             *openapi3.Operation
	// Params are path-level and operation-level parameters merged; operation
	// parameters override path parameters with the same name and location.
	Params []*openapi3.Parameter
	// Security is the effective security: the operation's own requirements
	// or, if unset, the document's. An empty slice means "no authentication".
	Security openapi3.SecurityRequirements
	// Where is the location of the operation in the spec, e.g. "paths./x.post".
	Where string
	// SecurityDeclared is set if the operation or the document declares
	// security, including an explicit "security: []".
	SecurityDeclared bool
}

Operation is one path + method combination with its effective parameters and security requirements.

func (*Operation) Group

func (o *Operation) Group() string

Group is the resource group of the operation: its first tag.

func (*Operation) Is

func (o *Operation) Is(id string) bool

Is reports whether id names this operation: its operationId or "<METHOD> <path>", which is accepted for every operation.

type SchemaError

type SchemaError struct {
	Pointer string // e.g. "/price"; "" is the root
	Reason  string
}

SchemaError is one validation problem at a JSON pointer.

func (SchemaError) String

func (e SchemaError) String() string

type Spec

type Spec struct {
	Doc      *openapi3.T
	Path     string // absolute path of the spec file
	Version  string // value of the "openapi" field
	Ops      []*Operation
	Findings []Finding
}

Spec is a loaded, validated OpenAPI document.

func Load

func Load(ctx context.Context, path string) (*Spec, error)

Load reads, resolves and validates the spec at path. Relative paths are resolved against the current working directory, as is usual in Go tests.

func (*Spec) AssumeBearer

func (s *Spec) AssumeBearer() (string, bool)

AssumeBearer handles specs that declare no security anywhere although the API needs a token: every operation without declared security requires a bearer token. It changes nothing if any operation or the document declares security (also "security: []"). With exactly one defined security scheme, that scheme is used, otherwise an http bearer scheme is added. It returns the scheme name and whether the spec was changed.

func (*Spec) BasePath

func (s *Spec) BasePath() string

BasePath returns the path component of the first server, or "" for "/".

func (*Spec) Is31

func (s *Spec) Is31() bool

Is31 reports whether the document is OpenAPI 3.1.x or later.

func (*Spec) Op

func (s *Spec) Op(id string) *Operation

Op returns the operation with the given ID (operationId or substitute ID).

type Validator

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

Validator validates values against schemas. It holds only per-validation settings; nothing is registered globally (FR-GO-04).

func NewValidator

func NewValidator() *Validator

NewValidator returns a validator with format checks for the formats that FR-CMP-01 requires. Formats already known to kin-openapi (date, date-time, byte, int32, int64) are validated as well.

func (*Validator) Validate

func (v *Validator) Validate(schema *openapi3.Schema, value any, mode Mode) []SchemaError

Validate checks value against schema. value must be in the canonical form produced by Normalize or DecodeJSON.

Jump to

Keyboard shortcuts

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