Reuse document indexes in D&D validators

This commit is contained in:
2026-07-25 12:57:31 +00:00
parent 7a66095912
commit 97cdb01357
9 changed files with 51 additions and 27 deletions

View File

@@ -48,10 +48,14 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
// Validate checks only invariants owned by normalized combat-turn output. A
// shape or source-reference failure is deliberately deferred to its owner.
func Validate(doc *source.SourceDocument, value dnd.CombatTurnList) error {
if combatshape.Validate(value) != nil || !sourceRefsValid(doc, value) {
if combatshape.Validate(value) != nil {
return nil
}
issues := issuesFor(shared.NewSourceRefOrder(doc), value)
index := source.NewDocumentIndex(doc)
if !sourceRefsValid(index, value) {
return nil
}
issues := issuesFor(shared.NewSourceRefOrderWithIndex(doc, index), value)
if len(issues) == 0 {
return nil
}
@@ -97,10 +101,10 @@ func issuesFor(order shared.SourceRefOrder, value dnd.CombatTurnList) []string {
return issues
}
func sourceRefsValid(doc *source.SourceDocument, value dnd.CombatTurnList) bool {
func sourceRefsValid(index source.DocumentIndex, value dnd.CombatTurnList) bool {
for _, turn := range value.CombatTurns {
for _, ref := range turn.SourceRefs {
if source.ValidateRef(doc, ref) != nil {
if index.ValidateRef(ref) != nil {
return false
}
}