validation

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Spec Validation Rules
	RuleValidationRequiredField           = "validation-required-field"
	RuleValidationTypeMismatch            = "validation-type-mismatch"
	RuleValidationDuplicateKey            = "validation-duplicate-key"
	RuleValidationInvalidFormat           = "validation-invalid-format"
	RuleValidationEmptyValue              = "validation-empty-value"
	RuleValidationInvalidReference        = "validation-invalid-reference"
	RuleValidationInvalidSyntax           = "validation-invalid-syntax"
	RuleValidationInvalidSchema           = "validation-invalid-schema"
	RuleValidationInvalidTarget           = "validation-invalid-target"
	RuleValidationAllowedValues           = "validation-allowed-values"
	RuleValidationMutuallyExclusiveFields = "validation-mutually-exclusive-fields"
	RuleValidationOperationNotFound       = "validation-operation-not-found"
	RuleValidationOperationIdUnique       = "validation-operation-id-unique"
	RuleValidationOperationParameters     = "validation-operation-parameters"
	RuleValidationSchemeNotFound          = "validation-scheme-not-found"
	RuleValidationTagNotFound             = "validation-tag-not-found"
	RuleValidationSupportedVersion        = "validation-supported-version"
	RuleValidationCircularReference       = "validation-circular-reference"
)

Variables

View Source
var ErrSkipFix = errors.New("fix skipped by user")

ErrSkipFix is a sentinel error returned by Prompter when the user chooses to skip a fix. Use errors.Is(err, ErrSkipFix) to check.

Functions

func GetContextObject

func GetContextObject[T any](o *Options) *T

func NewMapKeyError added in v0.2.1

func NewMapKeyError(severity Severity, rule string, err error, core CoreModeler, node MapKeyNodeGetter, key string) error

func NewMapValueError added in v0.2.1

func NewMapValueError(severity Severity, rule string, err error, core CoreModeler, node MapValueNodeGetter, key string) error

func NewSliceError added in v0.2.1

func NewSliceError(severity Severity, rule string, err error, core CoreModeler, node SliceNodeGetter, index int) error

func NewValidationError added in v1.0.0

func NewValidationError(severity Severity, rule string, err error, node *yaml.Node) error

func NewValidationErrorWithDocumentLocation added in v1.16.0

func NewValidationErrorWithDocumentLocation(severity Severity, rule string, err error, node *yaml.Node, documentLocation string) error

NewValidationErrorWithDocumentLocation creates a validation error with document location metadata.

func NewValueError added in v0.2.1

func NewValueError(severity Severity, rule string, err error, core CoreModeler, node ValueNodeGetter) error

func RuleDescription added in v1.16.0

func RuleDescription(ruleID string) string

func RuleHowToFix added in v1.16.0

func RuleHowToFix(ruleID string) string

func RuleSummary added in v1.16.0

func RuleSummary(ruleID string) string

func SortValidationErrors added in v1.0.0

func SortValidationErrors(allErrors []error)

SortValidationErrors sorts the provided validation errors by line and column number lowest to highest.

Types

type ChangeDescriber added in v1.17.0

type ChangeDescriber interface {
	DescribeChange() (before, after string)
}

ChangeDescriber is an optional interface that fixes can implement to provide human-readable before/after descriptions of what the fix changes. This enables richer dry-run and reporting output.

type CoreModeler added in v0.2.1

type CoreModeler interface {
	GetRootNode() *yaml.Node
}

type Error

type Error struct {
	UnderlyingError error
	Node            *yaml.Node
	Severity        Severity
	Rule            string
	Fix             Fix
	// DocumentLocation is the absolute location (URL or file path) of the document
	// where the error originated. Empty means the main document.
	DocumentLocation string
}

Error represents a validation error and the line and column where it occurred TODO allow getting the JSON path for line/column for validation errors

func (Error) Error

func (e Error) Error() string

func (Error) GetColumnNumber added in v1.0.0

func (e Error) GetColumnNumber() int

func (Error) GetDocumentLocation added in v1.16.0

func (e Error) GetDocumentLocation() string

GetDocumentLocation returns the document location where the error originated.

func (Error) GetLineNumber added in v1.0.0

func (e Error) GetLineNumber() int

func (Error) GetNode added in v1.16.0

func (e Error) GetNode() *yaml.Node

func (Error) GetSeverity added in v1.16.0

func (e Error) GetSeverity() Severity

func (Error) Unwrap added in v0.2.2

func (e Error) Unwrap() error

type Fix added in v1.16.0

type Fix interface {
	// Description returns a human-readable description of what the fix does.
	Description() string

	// Interactive returns true if the fix requires user input before being applied.
	// Non-interactive fixes can be applied directly with Apply().
	// Interactive fixes must have SetInput() called with user responses before Apply().
	Interactive() bool

	// Prompts returns the input prompts needed for this fix.
	// Returns nil for non-interactive fixes.
	Prompts() []Prompt

	// SetInput provides user responses for interactive fixes.
	// The responses slice must correspond 1:1 with the Prompts() slice.
	// Returns an error if the input is invalid.
	// Calling this on a non-interactive fix is a no-op.
	SetInput(responses []string) error

	// Apply applies the fix to the document.
	// For interactive fixes, SetInput() must be called first.
	// The doc parameter is typically *openapi.OpenAPI.
	// Returns an error if the fix cannot be applied.
	Apply(doc any) error
}

Fix represents a suggested fix for a validation finding. Fixes can be non-interactive (applied automatically) or interactive (requiring user input before application).

type MapKeyNodeGetter added in v1.7.12

type MapKeyNodeGetter interface {
	GetMapKeyNodeOrRoot(key string, root *yaml.Node) *yaml.Node
}

MapKeyNodeGetter provides access to map key nodes for error reporting.

type MapValueNodeGetter added in v1.7.12

type MapValueNodeGetter interface {
	GetMapValueNodeOrRoot(key string, root *yaml.Node) *yaml.Node
}

MapValueNodeGetter provides access to map value nodes for error reporting.

type NodeFix added in v1.17.0

type NodeFix interface {
	Fix

	// ApplyNode applies the fix directly to the YAML node tree.
	// rootNode is the document root node.
	ApplyNode(rootNode *yaml.Node) error
}

NodeFix is an optional interface for fixes that operate directly on yaml.Node trees rather than the high-level document model. This is useful for simple textual changes (renaming a key, changing a value) where going through the model is unnecessary.

The fix engine checks for this interface first; if implemented, ApplyNode is called instead of Apply.

type Option

type Option func(o *Options)

func WithContextObject

func WithContextObject[T any](obj *T) Option

type Options

type Options struct {
	ContextObjects map[reflect.Type]any
}

func NewOptions

func NewOptions(opts ...Option) *Options

type Prompt added in v1.17.0

type Prompt struct {
	// Type is the kind of input needed.
	Type PromptType
	// Message is a human-readable description of what input is needed and why.
	Message string
	// Choices is the list of valid choices when Type is PromptChoice.
	// Ignored for other prompt types.
	Choices []string
	// Default is an optional default value.
	Default string
}

Prompt describes a single piece of input a fix needs from the user.

type PromptType added in v1.17.0

type PromptType int

PromptType describes what kind of user input a fix needs.

const (
	// PromptChoice indicates the fix requires selecting from a list of options.
	PromptChoice PromptType = iota
	// PromptFreeText indicates the fix requires free-form text input.
	PromptFreeText
)

type Prompter added in v1.17.0

type Prompter interface {
	// PromptFix presents a fix to the user and collects input for its prompts.
	// finding provides the error being fixed so the prompter can display context.
	// fix is the fix that needs input.
	// Returns the user's responses corresponding to fix.Prompts(), or an error.
	// Return ErrSkipFix (or wrap it) to indicate the user chose to skip this fix.
	PromptFix(finding *Error, fix Fix) ([]string, error)

	// Confirm asks the user a yes/no question.
	Confirm(message string) (bool, error)
}

Prompter collects user input for interactive fixes. Implementations can be terminal-based (stdin/stdout), GUI-based, or test stubs.

type RuleInfo added in v1.16.0

type RuleInfo struct {
	Summary     string
	Description string
	HowToFix    string
}

func RuleInfoForID added in v1.16.0

func RuleInfoForID(ruleID string) (RuleInfo, bool)

type Severity added in v1.16.0

type Severity string
const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
	SeverityHint    Severity = "hint"
)

func (Severity) Rank added in v1.16.0

func (s Severity) Rank() int

Rank returns a numeric rank for severity comparison. Higher rank means worse severity. SeverityError = 2, SeverityWarning = 1, SeverityHint = 0. Unknown severities are treated as SeverityError.

func (Severity) String added in v1.16.0

func (s Severity) String() string

type SliceNodeGetter added in v1.7.12

type SliceNodeGetter interface {
	GetSliceValueNodeOrRoot(index int, root *yaml.Node) *yaml.Node
}

SliceNodeGetter provides access to slice element nodes for error reporting.

type TypeMismatchError added in v0.2.2

type TypeMismatchError struct {
	Msg        string
	ParentName string
}

func NewTypeMismatchError added in v0.2.2

func NewTypeMismatchError(parentName, msg string, args ...any) *TypeMismatchError

func (TypeMismatchError) Error added in v0.2.2

func (e TypeMismatchError) Error() string

type ValueNodeGetter added in v1.7.12

type ValueNodeGetter interface {
	GetValueNodeOrRoot(root *yaml.Node) *yaml.Node
}

ValueNodeGetter provides access to value nodes for error reporting.

Jump to

Keyboard shortcuts

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