Enforce durable D&D evidence ranges
This commit is contained in:
24
internal/modules/dnd/codec/sourcerange/sourcerange.go
Normal file
24
internal/modules/dnd/codec/sourcerange/sourcerange.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Package sourcerange validates durable D&D source-reference ranges.
|
||||
package sourcerange
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
// Validate checks the source-independent numeric constraints of a durable
|
||||
// source reference. Source membership and extraction-chunk coverage require
|
||||
// source context and remain the responsibility of source-aware validators.
|
||||
func Validate(ref source.SourceRef) error {
|
||||
if ref.StartUnitID <= 0 {
|
||||
return fmt.Errorf("start_unit_id must be positive")
|
||||
}
|
||||
if ref.EndUnitID <= 0 {
|
||||
return fmt.Errorf("end_unit_id must be positive")
|
||||
}
|
||||
if ref.StartUnitID > ref.EndUnitID {
|
||||
return fmt.Errorf("start_unit_id must not exceed end_unit_id")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
31
internal/modules/dnd/codec/sourcerange/sourcerange_test.go
Normal file
31
internal/modules/dnd/codec/sourcerange/sourcerange_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package sourcerange
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
func TestValidateChecksOnlyDurableNumericRange(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
ref source.SourceRef
|
||||
want string
|
||||
}{
|
||||
{name: "valid without source ID", ref: source.SourceRef{StartUnitID: 1, EndUnitID: 2}},
|
||||
{name: "nonpositive start", ref: source.SourceRef{StartUnitID: 0, EndUnitID: 1}, want: "start_unit_id"},
|
||||
{name: "nonpositive end", ref: source.SourceRef{StartUnitID: 1, EndUnitID: 0}, want: "end_unit_id"},
|
||||
{name: "reversed", ref: source.SourceRef{StartUnitID: 2, EndUnitID: 1}, want: "must not exceed"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := Validate(test.ref)
|
||||
if test.want == "" && err != nil {
|
||||
t.Fatalf("Validate() error = %v, want nil", err)
|
||||
}
|
||||
if test.want != "" && (err == nil || !strings.Contains(err.Error(), test.want)) {
|
||||
t.Fatalf("Validate() error = %v, want %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user