compare

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: 11 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 compare implements stage 2 (schema) and stage 3 (example) of the response checks (FR-CMP).

Index

Constants

View Source
const Missing = "(missing)"

Missing marks an absent value in a Diff.

Variables

This section is empty.

Functions

func Equal

func Equal(a, b any) bool

Equal compares two primitive values; numbers are compared numerically without loss of precision (FR-CMP-06).

func Render

func Render(v any) string

Render formats a value as compact JSON for diffs and the report.

func Response

func Response(v *spec.Validator, r *openapi3.Response, header http.Header, body []byte) (errs []spec.SchemaError, decoded any, isJSON bool)

Response checks headers and body of a response against the documented response (stage 2, FR-CMP-01). For JSON bodies it also returns the decoded body, so stage 3 can use it.

func Schema

func Schema(r *openapi3.Response, ct string) *openapi3.Schema

Schema returns the schema of the documented media type matching ct.

Types

type Diff

type Diff struct {
	Pointer  string
	Expected string
	Actual   string
	Note     string // optional explanation
}

Diff is one difference between expected and actual value (FR-CMP-08).

func Values

func Values(expected, actual any, opt Options) []Diff

Values compares expected with actual. Both must be in canonical form (numbers as json.Number). The result is sorted by pointer.

type Mode

type Mode string

Mode is the comparison mode (FR-CMP-02).

const (
	ModeSchema Mode = "schema" // only stage 2
	ModeSubset Mode = "subset" // every field of the example must match; extra fields are fine
	ModeExact  Mode = "exact"  // identical apart from ignored fields
)

Comparison modes.

func ParseMode

func ParseMode(s string) (Mode, error)

ParseMode validates a mode from Config or x-apitest-compare.

type Options

type Options struct {
	Mode Mode
	// Ignore contains field names (matched at every level) and JSON pointers
	// (starting with "/"; "*" matches any array index or key).
	Ignore []string
	// Schema of the actual value. Fields with format uuid, date-time, date or
	// readOnly are only checked for presence (FR-CMP-05).
	Schema *openapi3.Schema
	// Unordered compares all arrays independent of order (FR-CMP-07). Single
	// arrays can be marked with x-apitest-compare-unordered in the schema.
	Unordered bool
	// IgnoreCase compares strings and field names without regard to case:
	// "Nord" matches "nord", a field "Name" matches "name". An exact match of
	// a field name wins.
	IgnoreCase bool
}

Options control Values.

Jump to

Keyboard shortcuts

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