validation

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package validation provides a unified engine for validating generated LiveTemplate applications. It aggregates multiple checks (go.mod structure, template syntax, SQL migrations, compilation) into a single pass and reports issues using the shared validator.ValidationResult type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(ctx context.Context, appPath string) *validator.ValidationResult

Validate runs all default checks including compilation (go build ./...). This may be slow if dependencies are not cached. Use NewEngine with selective checks for latency-sensitive call sites (e.g. file watchers).

func ValidateFull

func ValidateFull(ctx context.Context, appPath string) *validator.ValidationResult

ValidateFull runs all checks including the runtime startup check. This is the most thorough validation and is the most expensive.

func ValidatePostGen

func ValidatePostGen(ctx context.Context, appPath string) *validator.ValidationResult

ValidatePostGen runs structural checks (no compilation) after code generation. Use this right after lvt gen commands where the app may not yet compile.

Types

type Check

type Check interface {
	// Name returns a human-readable name for the check (e.g. "go.mod").
	Name() string
	// Run executes the check and returns issues found.
	Run(ctx context.Context, appPath string) *validator.ValidationResult
}

Check is a single validation check that can be run against an app directory.

type CompilationCheck

type CompilationCheck struct {
	// RunGoModTidy runs go mod tidy before building. Off by default because
	// it mutates go.mod/go.sum in the target directory.
	RunGoModTidy bool
	// RunSqlc runs sqlc generate before building (if sqlc.yaml exists and
	// queries.sql has content). Off by default because it requires a locally
	// installed sqlc binary.
	RunSqlc bool
}

CompilationCheck runs sqlc generate (optional), go mod tidy (opt-in), and go build on an app directory. By default only go build runs; set RunGoModTidy or RunSqlc to true to enable those steps.

func (*CompilationCheck) Name

func (c *CompilationCheck) Name() string

func (*CompilationCheck) Run

type Engine

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

Engine orchestrates multiple validation checks.

func DefaultEngine

func DefaultEngine() *Engine

DefaultEngine returns an engine pre-loaded with all built-in checks.

func FullEngine

func FullEngine() *Engine

FullEngine returns an engine with all default checks plus RuntimeCheck. RuntimeCheck is expensive (builds binary, starts subprocess, probes HTTP) so it is not included in DefaultEngine.

func NewEngine

func NewEngine(opts ...Option) *Engine

NewEngine creates an engine with the given options.

func PostGenEngine

func PostGenEngine() *Engine

PostGenEngine returns an engine suited for post-generation validation. It checks structural aspects (go.mod, templates, migrations) but skips compilation because the app may not compile until sqlc generate is run.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context, appPath string) *validator.ValidationResult

Run executes every registered check sequentially and merges their results. Checks run independently even if earlier ones report errors, so the caller gets a complete picture of all issues. It respects context cancellation between checks so a timeout or cancellation stops early.

type GoModCheck

type GoModCheck struct{}

GoModCheck validates the go.mod file in an app directory.

func (*GoModCheck) Name

func (c *GoModCheck) Name() string

func (*GoModCheck) Run

type MigrationCheck

type MigrationCheck struct{}

MigrationCheck validates SQL migration files against an in-memory SQLite DB. Because apps may target PostgreSQL, SQL execution errors are reported as warnings (they may be false positives for non-SQLite dialects). Structural issues (missing files, missing goose directives) are always reported.

func (*MigrationCheck) Name

func (c *MigrationCheck) Name() string

func (*MigrationCheck) Run

type Option

type Option func(*Engine)

Option configures an Engine.

func WithCheck

func WithCheck(c Check) Option

WithCheck appends a check to the engine.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the maximum duration for all checks combined.

type RuntimeCheck

type RuntimeCheck struct {
	// StartupTimeout is how long to wait for the app to start listening.
	// Defaults to 15s if zero.
	StartupTimeout time.Duration
	// Port to run the app on. 0 means auto-allocate a free port.
	Port int
	// Routes to probe after the app starts. Defaults to ["/"] if empty.
	Routes []string
}

RuntimeCheck builds the app binary, starts it, and probes HTTP routes. It implements the Check interface and is opt-in (not in DefaultEngine) because it is expensive: it compiles a binary, starts a subprocess, and makes HTTP requests.

func (*RuntimeCheck) Name

func (c *RuntimeCheck) Name() string

func (*RuntimeCheck) Run

type TemplateCheck

type TemplateCheck struct{}

TemplateCheck validates all .tmpl files in an app directory using html/template (matching the existing generator/validate.go convention).

func (*TemplateCheck) Name

func (c *TemplateCheck) Name() string

func (*TemplateCheck) Run

Jump to

Keyboard shortcuts

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