validation

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package validation provides validation rules for Spectr changes and specs.

Package validation provides validation result printing functions. This file contains functions for outputting validation results in both JSON and human-readable formats.

Package validation provides validation helper functions.

Package validation provides functions for collecting validation items.

Index

Constants

View Source
const (
	ColorError   = "1" // Red
	ColorWarning = "3" // Yellow
)

Color constants for validation output styling

View Source
const (
	// ItemTypeChange represents a change item type
	ItemTypeChange = "change"
	// ItemTypeSpec represents a spec item type
	ItemTypeSpec = "spec"
	// SpectrDir is the base directory for spectr files
	SpectrDir = "spectr"
)
View Source
const MinPurposeLength = 50

MinPurposeLength is the minimum recommended length for the Purpose section.

Variables

This section is empty.

Functions

func ContainsShallOrMust

func ContainsShallOrMust(text string) bool

ContainsShallOrMust checks if text contains SHALL or MUST (case-insensitive)

func ExtractScenarios

func ExtractScenarios(requirementBlock string) []string

ExtractScenarios finds all #### Scenario: blocks in a requirement

func ExtractSections

func ExtractSections(content string) map[string]string

ExtractSections returns a map of section headers (## headers) to their content Example: "## Purpose" -> "This is the purpose..."

func NormalizeRequirementName

func NormalizeRequirementName(name string) string

NormalizeRequirementName normalizes requirement names for duplicate detection Trims whitespace, converts to lowercase, and removes extra spaces

func PrintBulkHumanResults

func PrintBulkHumanResults(results []BulkResult)

PrintBulkHumanResults prints bulk validation results in human format with improved formatting: visual separation between failed items, relative paths, issues grouped by file, colored error/warning labels, enhanced summary, and type indicators.

func PrintBulkJSONResults

func PrintBulkJSONResults(results []BulkResult)

PrintBulkJSONResults prints bulk validation results as JSON

func PrintHumanReport

func PrintHumanReport(
	itemName string,
	report *ValidationReport,
)

PrintHumanReport prints a single validation report in human format

func PrintJSONReport

func PrintJSONReport(
	report *ValidationReport,
)

PrintJSONReport prints a single validation report as JSON

func RunInteractiveValidation

func RunInteractiveValidation(projectPath string, strict bool, jsonOutput bool) error

RunInteractiveValidation runs the interactive validation TUI

func ToRelativePath

func ToRelativePath(absPath string) string

ToRelativePath converts an absolute path to a path relative to the spectr/ directory. It removes everything up to and including the spectr/ prefix. Example: /home/user/project/spectr/changes/foo/spec.md -> changes/foo/spec.md

func ValidatePreMerge

func ValidatePreMerge(baseSpecPath string, deltaPlan *parsers.DeltaPlan, specExists bool) error

ValidatePreMerge validates delta operations against base spec. It checks that: - ADDED requirements don't already exist in base spec - MODIFIED/REMOVED/RENAMED requirements DO exist in base spec - RENAMED TO requirements don't already exist (unless renaming to itself)

If specExists is false, only ADDED operations are allowed.

Types

type BulkResult

type BulkResult struct {
	Name   string            `json:"name"`
	Type   string            `json:"type"`
	Valid  bool              `json:"valid"`
	Report *ValidationReport `json:"report,omitempty"`
	Error  string            `json:"error,omitempty"`
}

BulkResult represents the result of validating a single item

func ValidateSingleItem

func ValidateSingleItem(
	validator *Validator,
	item ValidationItem,
) (BulkResult, error)

ValidateSingleItem validates a single item and returns the bulk result

type DeltaType

type DeltaType string

DeltaType represents the type of delta operation

const (
	DeltaAdded    DeltaType = "ADDED"
	DeltaModified DeltaType = "MODIFIED"
	DeltaRemoved  DeltaType = "REMOVED"
	DeltaRenamed  DeltaType = "RENAMED"
)

type ItemTypeInfo

type ItemTypeInfo struct {
	ItemType string
	IsChange bool
	IsSpec   bool
}

ItemTypeInfo holds information about an item's type

func DetermineItemType

func DetermineItemType(
	projectPath, itemName string,
	typeFlag *string,
) (ItemTypeInfo, error)

DetermineItemType determines if an item is a change or spec

type RenamedRequirement

type RenamedRequirement struct {
	FromName string
	ToName   string
}

RenamedRequirement represents a FROM -> TO rename pair

type Requirement

type Requirement struct {
	Name      string
	Content   string
	Scenarios []string
}

Requirement represents a parsed requirement with its content and scenarios

func ExtractRequirements

func ExtractRequirements(content string) []Requirement

ExtractRequirements returns all requirements found in content Looks for ### Requirement: headers

type ValidationIssue

type ValidationIssue struct {
	Level   ValidationLevel `json:"level"`
	Path    string          `json:"path"`
	Line    int             `json:"line,omitempty"`
	Message string          `json:"message"`
}

ValidationIssue represents a single validation problem or note

type ValidationItem

type ValidationItem struct {
	Name     string
	ItemType string // "change" or "spec"
	Path     string
}

ValidationItem represents an item to validate

func CreateValidationItems

func CreateValidationItems(
	_ string,
	ids []string,
	itemType, basePath string,
) []ValidationItem

CreateValidationItems creates validation items from IDs and item type. The projectPath parameter is intentionally unused for now but kept for potential future use in path construction.

func GetAllItems

func GetAllItems(
	projectPath string,
) ([]ValidationItem, error)

GetAllItems returns all changes and specs from the project path.

func GetChangeItems

func GetChangeItems(
	projectPath string,
) ([]ValidationItem, error)

GetChangeItems returns all changes from the project path.

func GetSpecItems

func GetSpecItems(
	projectPath string,
) ([]ValidationItem, error)

GetSpecItems returns all specs from the project path.

type ValidationLevel

type ValidationLevel string

ValidationLevel represents the severity of a validation issue

const (
	// LevelError indicates a critical validation failure
	LevelError ValidationLevel = "ERROR"
	// LevelWarning indicates a non-critical issue that should be addressed
	LevelWarning ValidationLevel = "WARNING"
	// LevelInfo provides informational feedback
	LevelInfo ValidationLevel = "INFO"
)

type ValidationReport

type ValidationReport struct {
	Valid   bool              `json:"valid"`
	Issues  []ValidationIssue `json:"issues"`
	Summary ValidationSummary `json:"summary"`
}

ValidationReport contains the complete validation results for an item

func NewValidationReport

func NewValidationReport(issuesParam []ValidationIssue) *ValidationReport

NewValidationReport creates a new ValidationReport from a list of issues

func ValidateChangeDeltaSpecs

func ValidateChangeDeltaSpecs(
	changeDir string,
	spectrRoot string,
	strictMode bool,
) (*ValidationReport, error)

ValidateChangeDeltaSpecs validates all delta spec files in a change directory. changeDir should be the path to a change directory (e.g., spectr/changes/add-feature). spectrRoot should be the path to the spectr/ directory (e.g., /path/to/project/spectr). Returns ValidationReport with all issues found, or error for issues

func ValidateItemByType

func ValidateItemByType(
	validator *Validator,
	projectPath, itemName, itemType string,
) (*ValidationReport, error)

ValidateItemByType validates an item based on its type

func ValidateSpecFile

func ValidateSpecFile(path string, strictMode bool) (*ValidationReport, error)

ValidateSpecFile validates a spec file according to Spectr rules Returns a ValidationReport containing all issues found, or an error for filesystem issues

type ValidationSummary

type ValidationSummary struct {
	Errors   int `json:"errors"`
	Warnings int `json:"warnings"`
	Info     int `json:"info"`
}

ValidationSummary provides aggregate counts of validation issues

type Validator

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

Validator is the main orchestrator for validation operations. It coordinates validation of specs and changes using the underlying rule functions.

func NewValidator

func NewValidator(strictMode bool) *Validator

NewValidator creates a new Validator with the specified strict mode. When strictMode is true, warnings are treated as errors in the validation reports.

func (*Validator) CreateReport

func (*Validator) CreateReport(
	issues []ValidationIssue,
) *ValidationReport

CreateReport creates a ValidationReport from a list of issues. This is a helper method that applies strict mode logic to the report. When strictMode is enabled, warnings in the issues are already converted to errors by the underlying validation functions before reaching this point.

func (*Validator) ValidateChange

func (v *Validator) ValidateChange(
	changeDir string,
) (*ValidationReport, error)

ValidateChange validates all delta spec files in a change directory. This is a wrapper around ValidateChangeDeltaSpecs that applies the validator's strictMode setting. changeDir should be the path to a change directory (e.g., spectr/changes/add-feature). Returns a ValidationReport with all issues found, or an error for filesystem issues.

func (*Validator) ValidateSpec

func (v *Validator) ValidateSpec(path string) (*ValidationReport, error)

ValidateSpec validates a specification file at the given path. This is a wrapper around ValidateSpecFile that applies the validator's strictMode setting. Returns a ValidationReport with all issues found, or an error for filesystem issues.

Jump to

Keyboard shortcuts

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