engine

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package engine runs rules over an OpenAPI document and scores the result.

It knows nothing about any particular ruleset. A rule is a function over a spec.Document that records, through a Context, each place it looked and each place it found a problem. Recording the places it looked is what makes a score possible: a rule that checked 40 parameters and found 2 without a description is doing much better than one that checked 3 and found 2, and a bare count of findings cannot tell those apart.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CategoryScore

type CategoryScore struct {
	Name  string
	Score float64
	// Applicable is false when no rule in the category found anything to
	// check. Such a category is left out of the overall score.
	Applicable bool
}

CategoryScore is the severity-weighted mean of a category's applicable rule scores, from 0 to 100.

type Context

type Context struct {
	Doc *spec.Document
	// contains filtered or unexported fields
}

Context is what a rule reports through.

func (*Context) Check

func (c *Context) Check(ok bool, op string, node *yaml.Node, ptr, msg string)

Check records a pass or a failure depending on ok.

func (*Context) CheckWeighted

func (c *Context) CheckWeighted(ok bool, weight float64, op string, node *yaml.Node, ptr, msg string)

CheckWeighted is Check for a place that should count for more or less than one in the score. The finding is reported the same either way; only its share of the rule's score changes.

func (*Context) Fail

func (c *Context) Fail(op string, node *yaml.Node, ptr, msg string)

Fail records a place the rule looked and found a problem. node is used for the file, line and column; pass the most specific node available.

func (*Context) Option

func (c *Context) Option(name string) int

Option returns the current value of one of the rule's declared options. Asking for one it did not declare is a bug in the rule, and panics.

func (*Context) Pass

func (c *Context) Pass()

Pass records a place the rule looked and found nothing wrong.

type Finding

type Finding struct {
	Rule     string   `json:"rule"`
	Severity Severity `json:"-"`
	Category string   `json:"category"`
	Message  string   `json:"message"`
	// Operation is the label of the operation the finding belongs to, or ""
	// for document-level findings.
	Operation string `json:"operation,omitempty"`
	// File is the file the finding is in, or "" for the root document.
	File    string `json:"file,omitempty"`
	Pointer string `json:"pointer"`
	Line    int    `json:"line"`
	Column  int    `json:"column"`
}

Finding is one problem at one place.

type Options

type Options struct {
	// Ignore reports whether a finding has been accepted. An ignored finding
	// is kept in RuleResult.Suppressed and counts as a pass.
	Ignore func(Finding) bool
}

Options adjusts a run.

type Report

type Report struct {
	Document   string
	Version    string
	Operations int
	Rules      []RuleResult
	Categories []CategoryScore
	// Score is the mean of the applicable category scores, from 0 to 100.
	Score float64
	// Skipped lists remote files that could not be fetched, so the schemas
	// behind them were not checked. A report with any is incomplete, and
	// its score describes only the part of the spec that was reached.
	Skipped []spec.Skip
	// Stale lists remote files checked from the cache because they could
	// not be fetched this run.
	Stale []string
}

Report is the outcome of running a ruleset over one document.

func Run

func Run(doc *spec.Document, rules []*Rule) *Report

Run applies rules to doc. Rules run in the order given, and categories are reported in the order they first appear in that list.

func RunWith

func RunWith(doc *spec.Document, rules []*Rule, opts Options) *Report

RunWith is Run with options.

func (*Report) Complete

func (r *Report) Complete() bool

Complete reports whether every file the spec refers to was checked.

func (*Report) Findings

func (r *Report) Findings() []Finding

Findings returns every reported finding, most severe first, then in document order.

func (*Report) Suppressed

func (r *Report) Suppressed() []Finding

Suppressed returns the findings configuration chose to ignore, in the same order as Findings.

type Rule

type Rule struct {
	ID       string // kebab-case, stable: it is how people configure a rule
	Title    string
	Category string
	Severity Severity
	// Options are the rule's tunable thresholds and their current values.
	// The keys a rule declares here are the only ones configuration may set.
	Options map[string]int
	Check   func(*Context)
}

Rule is one check.

func (*Rule) Clone

func (r *Rule) Clone() *Rule

Clone returns a copy that can be reconfigured without affecting r.

type RuleResult

type RuleResult struct {
	Rule    *Rule
	Checked int
	// Failed counts findings that were reported. Suppressed findings are
	// not in it: a finding someone has accepted counts as a pass.
	Failed     int
	Findings   []Finding
	Suppressed []Finding
	// Weight and FailedWeight are Checked and Failed with each place counted
	// at its weight, and are what the score is computed from. They equal the
	// counts unless the rule used CheckWeighted.
	Weight, FailedWeight float64
}

RuleResult is how one rule did.

func (RuleResult) Applicable

func (r RuleResult) Applicable() bool

Applicable reports whether the rule found anything to look at. A spec with no request bodies has nothing to say about request examples, and should neither gain nor lose for it.

func (RuleResult) Score

func (r RuleResult) Score() float64

Score is the weighted fraction of checked places that passed, from 0 to 1.

type Severity

type Severity int

Severity grades how badly a finding hurts.

const (
	// Info is worth fixing but rarely stops an agent on its own.
	Info Severity = iota + 1
	// Warning makes an agent more likely to pick the wrong operation or send
	// the wrong arguments.
	Warning
	// Error stops an agent outright, or makes the operation impossible to
	// expose as a tool without hand editing.
	Error
)

func ParseSeverity

func ParseSeverity(s string) Severity

ParseSeverity is the inverse of String. It returns 0 for anything else.

func (Severity) String

func (s Severity) String() string

Jump to

Keyboard shortcuts

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