Centralize output contract validation
This commit is contained in:
30
internal/domain/output_contract.go
Normal file
30
internal/domain/output_contract.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ValidateOutputContract validates source-neutral output-contract invariants.
|
||||
func ValidateOutputContract(contract OutputContract) error {
|
||||
switch contract.Format {
|
||||
case FormatText, FormatMarkdown, FormatJSON:
|
||||
default:
|
||||
return fmt.Errorf("invalid output format: %q", contract.Format)
|
||||
}
|
||||
|
||||
switch contract.ValidationMode {
|
||||
case ValidationNone, ValidationBasic, ValidationJSON, ValidationJSONSchema:
|
||||
default:
|
||||
return fmt.Errorf("invalid validation mode: %q", contract.ValidationMode)
|
||||
}
|
||||
|
||||
if contract.ValidationMode == ValidationJSONSchema && strings.TrimSpace(contract.SchemaPath) == "" {
|
||||
return errors.New("schema_path is required when validation_mode is json_schema")
|
||||
}
|
||||
if contract.RepairAttempts < 0 {
|
||||
return errors.New("repair_attempts must be greater than or equal to 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
68
internal/domain/output_contract_test.go
Normal file
68
internal/domain/output_contract_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateOutputContract(t *testing.T) {
|
||||
valid := OutputContract{
|
||||
Format: FormatText,
|
||||
ValidationMode: ValidationNone,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
change func(*OutputContract)
|
||||
wantErr string
|
||||
}{
|
||||
{name: "text format", change: func(c *OutputContract) { c.Format = FormatText }},
|
||||
{name: "markdown format", change: func(c *OutputContract) { c.Format = FormatMarkdown }},
|
||||
{name: "json format", change: func(c *OutputContract) { c.Format = FormatJSON }},
|
||||
{name: "empty format", change: func(c *OutputContract) { c.Format = "" }, wantErr: "format"},
|
||||
{name: "unsupported format", change: func(c *OutputContract) { c.Format = OutputFormat("binary") }, wantErr: "format"},
|
||||
{name: "none validation", change: func(c *OutputContract) { c.ValidationMode = ValidationNone }},
|
||||
{name: "basic validation", change: func(c *OutputContract) { c.ValidationMode = ValidationBasic }},
|
||||
{name: "json validation", change: func(c *OutputContract) { c.ValidationMode = ValidationJSON }},
|
||||
{name: "json schema validation", change: func(c *OutputContract) {
|
||||
c.ValidationMode = ValidationJSONSchema
|
||||
c.SchemaPath = "schema.json"
|
||||
}},
|
||||
{name: "empty validation mode", change: func(c *OutputContract) { c.ValidationMode = "" }, wantErr: "validation mode"},
|
||||
{name: "unsupported validation mode", change: func(c *OutputContract) { c.ValidationMode = ValidationMode("unknown") }, wantErr: "validation mode"},
|
||||
{name: "negative repair attempts", change: func(c *OutputContract) { c.RepairAttempts = -1 }, wantErr: "repair_attempts"},
|
||||
{name: "zero repair attempts", change: func(c *OutputContract) { c.RepairAttempts = 0 }},
|
||||
{name: "positive repair attempts", change: func(c *OutputContract) { c.RepairAttempts = 1 }},
|
||||
{name: "json schema empty path", change: func(c *OutputContract) {
|
||||
c.ValidationMode = ValidationJSONSchema
|
||||
c.SchemaPath = ""
|
||||
}, wantErr: "schema_path"},
|
||||
{name: "json schema whitespace path", change: func(c *OutputContract) {
|
||||
c.ValidationMode = ValidationJSONSchema
|
||||
c.SchemaPath = " \t "
|
||||
}, wantErr: "schema_path"},
|
||||
{name: "json schema nonblank path", change: func(c *OutputContract) {
|
||||
c.ValidationMode = ValidationJSONSchema
|
||||
c.SchemaPath = " schema.json "
|
||||
}},
|
||||
{name: "non-schema empty path", change: func(c *OutputContract) { c.SchemaPath = "" }},
|
||||
{name: "non-schema populated path", change: func(c *OutputContract) { c.SchemaPath = "ignored.json" }},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
contract := valid
|
||||
tt.change(&contract)
|
||||
err := ValidateOutputContract(contract)
|
||||
if tt.wantErr == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("validate output contract: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Fatalf("error = %v, want diagnostic containing %q", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user