validation

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package validation provides shared validation utilities for structured document projects. It defines common error types, severity levels, and result structures to ensure consistent validation behavior and reporting across all document types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// SemVerPattern matches semantic version strings (e.g., "1.2.3", "0.1.0-alpha").
	SemVerPattern = regexp.MustCompile(`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)

	// DatePattern matches YYYY-MM-DD date strings.
	DatePattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)

	// CVEPattern matches CVE identifiers (e.g., "CVE-2024-12345").
	CVEPattern = regexp.MustCompile(`^CVE-\d{4}-\d{4,}$`)

	// GHSAPattern matches GitHub Security Advisory identifiers.
	GHSAPattern = regexp.MustCompile(`^GHSA-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$`)

	// URLPattern matches URLs (basic pattern).
	URLPattern = regexp.MustCompile(`^https?://[^\s]+$`)

	// EmailPattern matches email addresses (basic pattern).
	EmailPattern = regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)

	// IDPattern matches valid identifier strings (alphanumeric with hyphens/underscores).
	IDPattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`)
)

Common validation patterns used across structured documents.

Functions

func FormatTOON

func FormatTOON(result *Result) string

FormatTOON returns the validation result in TOON format (token-efficient).

func InRange

func InRange(r *Result, path string, value, min, max float64, fieldName string) bool

InRange validates that a number is within a range.

func MaxLength

func MaxLength(r *Result, path, value string, max int, fieldName string) bool

MaxLength validates that a string does not exceed a maximum length.

func MinLength

func MinLength(r *Result, path, value string, min int, fieldName string) bool

MinLength validates that a string meets a minimum length.

func OneOf

func OneOf(r *Result, path, value string, allowed []string, fieldName string) bool

OneOf validates that a value is one of the allowed values.

func RequiredString

func RequiredString(r *Result, path, value, fieldName string) bool

RequiredString validates that a string is non-empty.

func RequiredStrings

func RequiredStrings(r *Result, basePath string, values []string, fieldName string) bool

RequiredStrings validates that all strings in a list are non-empty.

func UniqueStrings

func UniqueStrings(r *Result, path string, values []string, fieldName string) bool

UniqueStrings validates that all strings in a list are unique.

func ValidateCVE

func ValidateCVE(cve string) bool

ValidateCVE checks if a string is a valid CVE identifier.

func ValidateDate

func ValidateDate(date string) bool

ValidateDate checks if a string is a valid YYYY-MM-DD date.

func ValidateEmail

func ValidateEmail(email string) bool

ValidateEmail checks if a string is a valid email address.

func ValidateGHSA

func ValidateGHSA(ghsa string) bool

ValidateGHSA checks if a string is a valid GHSA identifier.

func ValidateID

func ValidateID(id string) bool

ValidateID checks if a string is a valid identifier.

func ValidateSemVer

func ValidateSemVer(version string) bool

ValidateSemVer checks if a string is a valid semantic version.

func ValidateURL

func ValidateURL(url string) bool

ValidateURL checks if a string is a valid URL.

Types

type Issue

type Issue struct {
	// Path is the JSON path to the problematic field (e.g., "metadata.title", "items[0].id").
	Path string

	// Field is the field name (deprecated, use Path).
	Field string

	// Message describes the validation issue.
	Message string

	// Severity is the issue severity level.
	Severity Severity

	// Code is an optional error code for programmatic handling.
	Code string
}

Issue represents a validation issue (error, warning, or info).

func (Issue) Error

func (i Issue) Error() string

Error implements the error interface.

func (Issue) Format

func (i Issue) Format() string

Format returns a formatted string representation of the issue.

func (Issue) FormatWithSeverity

func (i Issue) FormatWithSeverity() string

FormatWithSeverity returns a formatted string with severity prefix.

type Reporter

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

Reporter handles formatted output of validation results.

func DefaultReporter

func DefaultReporter() *Reporter

DefaultReporter returns a reporter using os.Stdout and os.Stderr.

func NewReporter

func NewReporter(stdout, stderr io.Writer) *Reporter

NewReporter creates a new Reporter with the given writers. If writers are nil, os.Stdout and os.Stderr are used.

func (*Reporter) Report

func (r *Reporter) Report(result *Result, inputPath string)

Report outputs the validation result in a human-readable format.

func (*Reporter) ReportSuccess

func (r *Reporter) ReportSuccess(docType, inputPath string)

ReportSuccess outputs a success message.

func (*Reporter) ReportSummary

func (r *Reporter) ReportSummary(result *Result)

ReportSummary outputs a summary of the validation result.

type Result

type Result struct {
	// Valid is true if validation passed (no errors).
	Valid bool

	// Errors contains validation errors.
	Errors []Issue

	// Warnings contains validation warnings.
	Warnings []Issue

	// Info contains informational messages.
	Info []Issue
}

Result represents the outcome of a validation operation.

func NewResult

func NewResult() *Result

NewResult creates a new empty validation result.

func (*Result) AddError

func (r *Result) AddError(path, message string)

AddError adds a validation error and marks the result as invalid.

func (*Result) AddErrorWithCode

func (r *Result) AddErrorWithCode(path, code, message string)

AddErrorWithCode adds a validation error with an error code.

func (*Result) AddErrorf

func (r *Result) AddErrorf(path, format string, args ...any)

AddErrorf adds a formatted validation error.

func (*Result) AddInfo

func (r *Result) AddInfo(path, message string)

AddInfo adds an informational message.

func (*Result) AddWarning

func (r *Result) AddWarning(path, message string)

AddWarning adds a validation warning.

func (*Result) AddWarningf

func (r *Result) AddWarningf(path, format string, args ...any)

AddWarningf adds a formatted validation warning.

func (*Result) AllIssues

func (r *Result) AllIssues() []Issue

AllIssues returns all issues (errors, warnings, info) combined.

func (*Result) ErrorCount

func (r *Result) ErrorCount() int

ErrorCount returns the number of errors.

func (*Result) HasInfo

func (r *Result) HasInfo() bool

HasInfo returns true if there are any info messages.

func (*Result) HasWarnings

func (r *Result) HasWarnings() bool

HasWarnings returns true if there are any warnings.

func (*Result) Merge

func (r *Result) Merge(other *Result)

Merge combines another result into this one.

func (*Result) WarningCount

func (r *Result) WarningCount() int

WarningCount returns the number of warnings.

type Severity

type Severity string

Severity represents the severity level of a validation issue.

const (
	// SeverityError indicates a validation error that must be fixed.
	SeverityError Severity = "error"

	// SeverityWarning indicates a validation warning that should be reviewed.
	SeverityWarning Severity = "warning"

	// SeverityInfo indicates informational validation feedback.
	SeverityInfo Severity = "info"
)

Jump to

Keyboard shortcuts

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