Improve D&D validation reliability

This commit is contained in:
2026-08-29 01:24:45 +00:00
parent 4da9360d74
commit 917d150279
55 changed files with 1300 additions and 565 deletions

View File

@@ -16,7 +16,7 @@ import (
const (
Key = "extract/dnd/npc-occurrences/registry"
ReasonCode = "invalid_npc_occurrence_registry"
policy = "dnd.npc_occurrences.validator.registry.v1"
policy = "dnd.npc_occurrences.validator.registry.v2"
)
type Options struct{}
@@ -84,31 +84,37 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
return contracts.ValidationResult{}, fmt.Errorf("resolve NPC registry: %w", err)
}
if !npcRegistry.Bound() {
return rejection([]string{"NPC registry reference is required"}), nil
var corrections diagnostics.Corrections
corrections.Add("registry", "Use only contextual NPC names from the supplied NPC registry; omit an occurrence that cannot be matched unambiguously.", "")
return rejection([]string{"NPC registry reference is required"}, corrections), nil
}
issues := make([]string, 0)
var corrections diagnostics.Corrections
for index, occurrence := range req.Value.Occurrences {
record := fmt.Sprintf("Affected %s occurrence for NPC %s %s.", diagnostics.Quote(string(occurrence.Kind)), diagnostics.Quote(occurrence.Name), diagnostics.SourceRange(occurrence.SourceRefs))
canonical, ok := npcRegistry.LookupID(occurrence.NPCID)
if !ok {
issues = append(issues, fmt.Sprintf("occurrences[%d].npc_id is not in the NPC registry: %s", index, diagnostics.Quote(occurrence.NPCID)))
corrections.Add("registry", "Use only contextual NPC names from the supplied NPC registry; omit an occurrence that cannot be matched unambiguously.", record)
continue
}
if occurrence.Name != canonical.Name {
issues = append(issues, fmt.Sprintf("occurrences[%d].name does not match npc_id: %s", index, diagnostics.Quote(occurrence.Name)))
corrections.Add("canonical-name", "Use the exact contextual NPC name supplied by the registry.", record+" Use registry name "+diagnostics.Quote(canonical.Name)+".")
}
}
if len(issues) == 0 {
return contracts.ValidationResult{Approved: true}, nil
}
return rejection(issues), nil
return rejection(issues, corrections), nil
}
func rejection(issues []string) contracts.ValidationResult {
func rejection(issues []string, corrections diagnostics.Corrections) contracts.ValidationResult {
return contracts.ValidationResult{
Approved: false,
ReasonCode: ReasonCode,
Message: diagnostics.Aggregate("invalid NPC occurrence registry", issues),
CorrectionGuidance: "Return NPC occurrences using contextual NPC names that match an NPC in the supplied registry; omit occurrences that cannot be matched unambiguously.",
CorrectionGuidance: corrections.Guidance("Correct every rejected NPC occurrence and return the complete replacement occurrence list"),
}
}