validation

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRemediable

func IsRemediable(err error) bool

IsRemediable checks if an error has a remediation.

func IsWarning added in v1.0.10

func IsWarning(err error) bool

IsWarning checks if an error is a warning.

func NewRemediableErr

func NewRemediableErr(err, remediation string) error

NewRemediableErr returns a new Remediable error.

func NewWarning added in v1.0.10

func NewWarning(err, remediation string) error

NewWarning returns a new Warning error.

func Remediation

func Remediation(err error) string

Remediation returns the Remediation message for an error if it has it. Otherwise it returns an empty string.

func Unwrap

func Unwrap(err error) []error

Unwrap unfolds and flattens errors if err implements Unwrap []error. If it doesn't implement it, it just returns a slice with one single error.

func WithRemediation

func WithRemediation(err error, remediation string) error

WithRemediation makes an error Remediable.

func WithWarning added in v1.0.10

func WithWarning(err error, remediation string) error

WithWarning makes an error a Warning.

Types

type ChannelReader

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

ChannelReader implements LineReader for a channel of strings source.

func NewChannelReader

func NewChannelReader(in <-chan string, name string) ChannelReader

NewChannelReader returns a new ChannelReader.

func (ChannelReader) Line

func (c ChannelReader) Line() (string, bool)

Line reads a line from the channel. Returns false if there is no more lines to read (in buffered channels) or the channel is closed.

func (ChannelReader) Name

func (c ChannelReader) Name() string

type Colorer

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

func (Colorer) Black

func (c Colorer) Black(m string) string

func (Colorer) Blue

func (c Colorer) Blue(m string) string

func (Colorer) Bold

func (c Colorer) Bold(m string) string

func (Colorer) Cyan

func (c Colorer) Cyan(m string) string

func (Colorer) Green

func (c Colorer) Green(m string) string

func (Colorer) Grey

func (c Colorer) Grey(m string) string

func (Colorer) Magenta

func (c Colorer) Magenta(m string) string

func (Colorer) Red

func (c Colorer) Red(m string) string

func (Colorer) Underline

func (c Colorer) Underline(m string) string

func (Colorer) Yellow

func (c Colorer) Yellow(m string) string

type FileCapture

type FileCapture struct {
	*os.File
	// contains filtered or unexported fields
}

FileCapture forwards the content of a file to a channel, reading line by line.

func NewFileCapture

func NewFileCapture(out chan<- string) *FileCapture

NewFileCapture returns a new FileCapture.

func (*FileCapture) Close

func (r *FileCapture) Close() error

Close closes all internal resources and stops routines.

func (*FileCapture) Init

func (r *FileCapture) Init() error

Init initializes the FileCapture. The caller is responsible for calling Close after Init succeeds once they are done with the FileCapture. If not, FileCapture might leak resources.

type Informer

type Informer interface {
	Starting(ctx context.Context, name, message string)
	Done(ctx context.Context, name string, err error)
}

type LineReader

type LineReader interface {
	// Line reads a line from the source. It returns false for the second argument
	// if there is no more lines to read.
	Line() (string, bool)
	// Name is name of the lines source.
	Name() string
}

LineReader is an interface that allows to read lines from some source.

type LoggerPrinter added in v1.0.10

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

LoggerPrinter is an informer that uses zap logger for validation output while preserving remediation functionality.

func NewLoggerPrinter added in v1.0.10

func NewLoggerPrinter(ctx context.Context) *LoggerPrinter

NewLoggerPrinter creates a new LoggerPrinter that uses the zap logger from the provided context.

func NewLoggerPrinterWithLogger added in v1.0.10

func NewLoggerPrinterWithLogger(log *zap.Logger) *LoggerPrinter

NewLoggerPrinterWithLogger creates a new LoggerPrinter with the provided logger.

func (*LoggerPrinter) Done added in v1.0.10

func (p *LoggerPrinter) Done(ctx context.Context, name string, err error)

Done logs the result of a validation using the zap logger. For successful validations, it logs at Info level. For warnings, it logs at Warn level. For failed validations, it logs at Error level and includes remediation if available.

func (*LoggerPrinter) Starting added in v1.0.10

func (p *LoggerPrinter) Starting(ctx context.Context, name, message string)

Starting logs the start of a validation using the zap logger.

type NoOpInformer

type NoOpInformer struct{}

func (NoOpInformer) Done

func (NoOpInformer) Done(ctx context.Context, name string, err error)

func (NoOpInformer) Starting

func (NoOpInformer) Starting(ctx context.Context, name, message string)

type Printer

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

Informer is an informer that prints the validation steps to stdout.

func NewPrinter

func NewPrinter(opts ...PrinterOpt) Printer

func (Printer) Done

func (p Printer) Done(ctx context.Context, name string, err error)

Done prints the result of a validation. If a validation fails, it will print the error, external output if any and the error remediation if the error is Remediable.

func (Printer) Starting

func (p Printer) Starting(ctx context.Context, name, message string)

Starting prints the starting message of a validation.

type PrinterOpt

type PrinterOpt func(*Printer)

PrinterOpt allows to configure the Printer.

func WithExternalLogs

func WithExternalLogs(in LineReader) PrinterOpt

WithExternalLogs allows to configure an external source of logs that will get included in the output when a validation fails. This is useful for correctly displaying the output of external processes that are invoked during the validation. You can just capture their output and let the printer handle its display.

func WithNoColor

func WithNoColor() PrinterOpt

WithNoColor disables color output.

func WithOutWriter

func WithOutWriter(out io.Writer) PrinterOpt

WithOutWriter allows to configure the output destination.

type PrinterWithStdCapture

type PrinterWithStdCapture struct {
	Printer
	FileCapture
}

PrinterWithStdCapture is a convenience struct that combines a Printer and a FileCapture, allowing to capture a os.File. Useful for capturing stderr or stdout and displaying it through the Printer.

func NewPrinterWithStdCapture

func NewPrinterWithStdCapture(stdName string, noColor bool) *PrinterWithStdCapture

NewPrinterWithStdCapture returns a new PrinterWithStdCapture.

type Remediable

type Remediable interface {
	Remediation() string
}

Remediable is an error that provides a possible remediation.

type Runner

type Runner[O Validatable[O]] struct {
	// contains filtered or unexported fields
}

Runner allows to compose and run validations.

func NewRunner

func NewRunner[O Validatable[O]](informer Informer, opts ...RunnerOpt) *Runner[O]

NewRunner constructs a new Runner.

func (*Runner[O]) Register

func (r *Runner[O]) Register(validations ...Validation[O])

Register adds validations to the Runner.

func (*Runner[O]) Sequentially

func (r *Runner[O]) Sequentially(ctx context.Context, obj O) error

Sequentially runs all validations one after the other and waits until they all finish, aggregating the errors if present. Warnings are logged but don't cause failure. obj must not be modified. If it is, this indicates a programming error and the method will panic.

func (*Runner[O]) UntilError

func (r *Runner[O]) UntilError(validations ...Validation[O]) Validation[O]

type RunnerConfig

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

RunnerConfig holds the configuration for the Runner.

type RunnerOpt

type RunnerOpt func(*RunnerConfig)

RunnerOpt allows to configure the Runner.

func WithSkipValidations

func WithSkipValidations(namesToSkip ...string) RunnerOpt

WithSkipValidation configures the runner to skip the validations with the given names.

type Validatable

type Validatable[O any] interface {
	DeepCopy() O
}

Validatable is anything that can be validated.

type Validate

type Validate[O Validatable[O]] func(ctx context.Context, informer Informer, obj O) error

Validate is the logic for a validation of a type O.

func UntilError

func UntilError[O Validatable[O]](validates ...Validate[O]) Validate[O]

UntilError returns a composed validate that runs all validations until one fails.

type Validation

type Validation[O Validatable[O]] struct {
	Name     string
	Validate Validate[O]
}

Validation validates the system for a type O.

func New

func New[O Validatable[O]](name string, validate Validate[O]) Validation[O]

type Warning added in v1.0.10

type Warning interface {
	IsWarning() bool
	Error() string
}

Warning represents a validation warning that doesn't prevent execution

Jump to

Keyboard shortcuts

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