57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const maxOutputRepairAttempts = 3
|
|
|
|
// 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 {
|
|
count := 0
|
|
if contract.Schema != nil {
|
|
count++
|
|
}
|
|
if contract.SchemaFile != nil {
|
|
if strings.TrimSpace(*contract.SchemaFile) == "" {
|
|
return errors.New("schema_file must not be blank")
|
|
}
|
|
count++
|
|
}
|
|
if contract.SchemaPath != "" {
|
|
if strings.TrimSpace(contract.SchemaPath) == "" {
|
|
return errors.New("schema_path must not be blank")
|
|
}
|
|
count++
|
|
}
|
|
if count != 1 {
|
|
return errors.New("exactly one of schema, schema_file, or 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")
|
|
}
|
|
if contract.RepairAttempts > maxOutputRepairAttempts {
|
|
return fmt.Errorf("repair_attempts must be less than or equal to %d", maxOutputRepairAttempts)
|
|
}
|
|
if contract.ValidationMode == ValidationNone && contract.RepairAttempts > 0 {
|
|
return errors.New("repair_attempts requires basic, json, or json_schema validation")
|
|
}
|
|
return nil
|
|
}
|