Improve D&D validation reliability
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/item-registry/shape"
|
||||
ReasonCode = "invalid_item_shape"
|
||||
policy = "dnd.item_registry.validator.shape.v1"
|
||||
policy = "dnd.item_registry.validator.shape.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -34,39 +34,58 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemRegistry]) (contracts.ValidationResult, error) {
|
||||
issues := issuesFor(req.Value)
|
||||
normalization := req.Stage == string(pipeline.StageNormalize)
|
||||
issues, corrections := assess(req.Value, normalization)
|
||||
if len(issues) > 0 {
|
||||
return rejection(diagnostics.Aggregate("invalid item shape", issues)), nil
|
||||
prefix := "Correct every rejected item and return the complete replacement registry"
|
||||
if normalization {
|
||||
prefix = "Correct the duplicate-group proposals and return the complete replacement proposal response"
|
||||
}
|
||||
return rejection(diagnostics.Aggregate("invalid item shape", issues), corrections.Guidance(prefix)), nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
|
||||
func Validate(value dnd.ItemRegistry) error {
|
||||
issues := issuesFor(value)
|
||||
issues, _ := assess(value, false)
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", diagnostics.Aggregate("invalid item shape", issues))
|
||||
}
|
||||
|
||||
func issuesFor(value dnd.ItemRegistry) []string {
|
||||
func assess(value dnd.ItemRegistry, normalization bool) ([]string, diagnostics.Corrections) {
|
||||
var corrections diagnostics.Corrections
|
||||
nameRule := "Provide one nonblank, transcript-supported item name for every registry entry."
|
||||
refRule := "Provide at least one transcript source range that directly supports every named item."
|
||||
listRule := "Return an `items` array; use an empty array when the transcript establishes no named items."
|
||||
if normalization {
|
||||
nameRule = "Revise the duplicate-group proposals so every selected canonical item has a nonblank, transcript-supported name."
|
||||
refRule = "Revise the duplicate-group proposals so every selected canonical item preserves direct transcript evidence."
|
||||
listRule = "Revise the duplicate-group proposals so normalization retains the complete item candidate registry."
|
||||
}
|
||||
if value.Items == nil {
|
||||
return []string{"items must be present"}
|
||||
corrections.Add("list", listRule, "")
|
||||
return []string{"items must be present"}, corrections
|
||||
}
|
||||
issues := make([]string, 0)
|
||||
for index, item := range value.Items {
|
||||
prefix := fmt.Sprintf("items[%d]", index)
|
||||
record := "Affected item " + diagnostics.Quote(strings.TrimSpace(item.Name)) + " " + diagnostics.SourceRange(item.SourceRefs) + "."
|
||||
if strings.TrimSpace(item.ID) == "" {
|
||||
issues = append(issues, prefix+".id must not be empty")
|
||||
corrections.Add("name", nameRule, record)
|
||||
}
|
||||
if strings.TrimSpace(item.Name) == "" {
|
||||
issues = append(issues, prefix+".name must not be empty")
|
||||
corrections.Add("name", nameRule, record)
|
||||
}
|
||||
if len(item.SourceRefs) == 0 {
|
||||
issues = append(issues, prefix+".source_refs must not be empty")
|
||||
corrections.Add("source-refs", refRule, record)
|
||||
}
|
||||
}
|
||||
return issues
|
||||
return issues, corrections
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
@@ -92,6 +111,6 @@ func DecodeOptions(options map[string]any) (Options, error) {
|
||||
|
||||
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }
|
||||
|
||||
func rejection(message string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: message, CorrectionGuidance: "Return a complete item registry containing only properly named items with valid source references."}
|
||||
func rejection(message, guidance string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: message, CorrectionGuidance: guidance}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user