Reuse document indexes in D&D validators
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
if err := combatshape.Validate(req.Value); err != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := sourceRefIssues(req.Source, req.Value)
|
||||
issues := sourceRefIssues(source.NewDocumentIndex(req.Source), req.Value)
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
@@ -48,11 +48,11 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sourceRefIssues(doc *source.SourceDocument, value dnd.CombatTurnList) []string {
|
||||
func sourceRefIssues(index source.DocumentIndex, value dnd.CombatTurnList) []string {
|
||||
issues := make([]string, 0)
|
||||
for turnIndex, turn := range value.CombatTurns {
|
||||
for refIndex, ref := range turn.SourceRefs {
|
||||
if err := source.ValidateRef(doc, ref); err != nil {
|
||||
if err := index.ValidateRef(ref); err != nil {
|
||||
issues = append(issues, fmt.Sprintf("combat_turns[%d].source_refs[%d]: %s", turnIndex, refIndex, diagnostics.Truncate(err.Error())))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user