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 ¶
- Variables
- func FormatTOON(result *Result) string
- func InRange(r *Result, path string, value, min, max float64, fieldName string) bool
- func MaxLength(r *Result, path, value string, max int, fieldName string) bool
- func MinLength(r *Result, path, value string, min int, fieldName string) bool
- func OneOf(r *Result, path, value string, allowed []string, fieldName string) bool
- func RequiredString(r *Result, path, value, fieldName string) bool
- func RequiredStrings(r *Result, basePath string, values []string, fieldName string) bool
- func UniqueStrings(r *Result, path string, values []string, fieldName string) bool
- func ValidateCVE(cve string) bool
- func ValidateDate(date string) bool
- func ValidateEmail(email string) bool
- func ValidateGHSA(ghsa string) bool
- func ValidateID(id string) bool
- func ValidateSemVer(version string) bool
- func ValidateURL(url string) bool
- type Issue
- type Reporter
- type Result
- func (r *Result) AddError(path, message string)
- func (r *Result) AddErrorWithCode(path, code, message string)
- func (r *Result) AddErrorf(path, format string, args ...any)
- func (r *Result) AddInfo(path, message string)
- func (r *Result) AddWarning(path, message string)
- func (r *Result) AddWarningf(path, format string, args ...any)
- func (r *Result) AllIssues() []Issue
- func (r *Result) ErrorCount() int
- func (r *Result) HasInfo() bool
- func (r *Result) HasWarnings() bool
- func (r *Result) Merge(other *Result)
- func (r *Result) WarningCount() int
- type Severity
Constants ¶
This section is empty.
Variables ¶
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 ¶
FormatTOON returns the validation result in TOON format (token-efficient).
func RequiredString ¶
RequiredString validates that a string is non-empty.
func RequiredStrings ¶
RequiredStrings validates that all strings in a list are non-empty.
func UniqueStrings ¶
UniqueStrings validates that all strings in a list are unique.
func ValidateCVE ¶
ValidateCVE checks if a string is a valid CVE identifier.
func ValidateDate ¶
ValidateDate checks if a string is a valid YYYY-MM-DD date.
func ValidateEmail ¶
ValidateEmail checks if a string is a valid email address.
func ValidateGHSA ¶
ValidateGHSA checks if a string is a valid GHSA identifier.
func ValidateID ¶
ValidateID checks if a string is a valid identifier.
func ValidateSemVer ¶
ValidateSemVer checks if a string is a valid semantic version.
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) FormatWithSeverity ¶
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 ¶
NewReporter creates a new Reporter with the given writers. If writers are nil, os.Stdout and os.Stderr are used.
func (*Reporter) ReportSuccess ¶
ReportSuccess outputs a success message.
func (*Reporter) ReportSummary ¶
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 (*Result) AddErrorWithCode ¶
AddErrorWithCode adds a validation error with an error code.
func (*Result) AddWarning ¶
AddWarning adds a validation warning.
func (*Result) AddWarningf ¶
AddWarningf adds a formatted validation warning.
func (*Result) ErrorCount ¶
ErrorCount returns the number of errors.
func (*Result) HasWarnings ¶
HasWarnings returns true if there are any warnings.
func (*Result) WarningCount ¶
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" )