Add D&D location occurrence validators

This commit is contained in:
2026-08-04 00:33:39 +00:00
parent dd61a4efda
commit a168c13b85
14 changed files with 1034 additions and 3 deletions

View File

@@ -0,0 +1,107 @@
// Package shape validates required D&D location occurrence candidate fields.
package shape
import (
"context"
"fmt"
"strings"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
)
const (
Key = "extract/dnd/location-occurrences/shape"
ReasonCode = "invalid_location_occurrence_shape"
policy = "dnd.location_occurrences.validator.shape.v1"
)
type Options struct{}
type Validator struct{}
var _ contracts.TypedValidator[dnd.LocationOccurrenceList] = (*Validator)(nil)
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
func New(Options) *Validator { return &Validator{} }
func (v *Validator) Name() string { return Key }
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
return contracts.ExecutionClassDeterministic
}
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
}
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.LocationOccurrenceList]) (contracts.ValidationResult, error) {
if err := Validate(req.Value); err != nil {
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: err.Error()}, nil
}
return contracts.ValidationResult{Approved: true}, nil
}
func Validate(value dnd.LocationOccurrenceList) error {
issues := issuesFor(value)
if len(issues) == 0 {
return nil
}
return fmt.Errorf("%s", diagnostics.Aggregate("invalid location occurrence shape", issues))
}
func issuesFor(value dnd.LocationOccurrenceList) []string {
if value.Occurrences == nil {
return []string{"occurrences must be present"}
}
issues := make([]string, 0)
for index, occurrence := range value.Occurrences {
prefix := fmt.Sprintf("occurrences[%d]", index)
if strings.TrimSpace(occurrence.LocationID) == "" {
issues = append(issues, prefix+".location_id must not be empty")
}
if strings.TrimSpace(occurrence.Name) == "" {
issues = append(issues, prefix+".name must not be empty")
}
if !validKind(occurrence.Kind) {
issues = append(issues, prefix+".kind is unsupported: "+diagnostics.Quote(string(occurrence.Kind)))
}
if len(occurrence.SourceRefs) == 0 {
issues = append(issues, prefix+".source_refs must contain at least one reference")
}
}
return issues
}
func validKind(value dnd.LocationOccurrenceKind) bool {
switch value {
case dnd.LocationOccurrenceKindVisited,
dnd.LocationOccurrenceKindPlanned,
dnd.LocationOccurrenceKindRecalled,
dnd.LocationOccurrenceKindMentioned:
return true
default:
return false
}
}
func Spec() pipeline.ValidatorSpec {
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
}
func Register(registry *pipeline.ValidatorRegistry) error {
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.LocationOccurrenceListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.LocationOccurrenceList], error) {
options, err := DecodeOptions(request.Options)
if err != nil {
return nil, err
}
return New(options), nil
})
}
func DecodeOptions(options map[string]any) (Options, error) {
if err := pipeline.RejectUnknownOptions(options); err != nil {
return Options{}, err
}
return Options{}, nil
}
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }

View File

@@ -0,0 +1,62 @@
package shape
import (
"context"
"reflect"
"strings"
"testing"
"unicode/utf8"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func TestValidatorAcceptsEverySupportedKind(t *testing.T) {
kinds := []dnd.LocationOccurrenceKind{dnd.LocationOccurrenceKindVisited, dnd.LocationOccurrenceKindPlanned, dnd.LocationOccurrenceKindRecalled, dnd.LocationOccurrenceKindMentioned}
occurrences := make([]dnd.LocationOccurrence, len(kinds))
for index, kind := range kinds {
occurrences[index] = occurrence(kind)
}
result, err := New(Options{}).Validate(context.Background(), request(dnd.LocationOccurrenceList{Occurrences: occurrences}))
if err != nil || !result.Approved {
t.Fatalf("Validate() = %#v, %v", result, err)
}
}
func TestValidatorRejectsRequiredFieldsAndUnsupportedKindWithoutMutation(t *testing.T) {
value := dnd.LocationOccurrenceList{Occurrences: []dnd.LocationOccurrence{{Name: strings.Repeat("火", 220), Kind: "other", SourceRefs: []source.SourceRef{}}}}
before := value
result, err := New(Options{}).Validate(context.Background(), request(value))
if err != nil || result.Approved || result.ReasonCode != ReasonCode || !strings.Contains(result.Message, "location_id must not be empty") || !strings.Contains(result.Message, "kind is unsupported") {
t.Fatalf("Validate() = %#v, %v", result, err)
}
if !reflect.DeepEqual(value, before) || len(result.Message) > 4096 || !utf8.ValidString(result.Message) {
t.Fatal("validation mutated value or returned unsafe diagnostics")
}
result, err = New(Options{}).Validate(context.Background(), request(dnd.LocationOccurrenceList{}))
if err != nil || result.Approved || !strings.Contains(result.Message, "occurrences must be present") {
t.Fatalf("missing list = %#v, %v", result, err)
}
}
func TestValidatorRegisters(t *testing.T) {
registry := pipeline.NewValidatorRegistry()
if err := Register(registry); err != nil {
t.Fatal(err)
}
if got, ok := registry.Spec(Key); !ok || got != Spec() || got.ExecutionClass != contracts.ExecutionClassDeterministic {
t.Fatalf("registered spec = %#v, ok = %t", got, ok)
}
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Value != policy {
t.Fatalf("fingerprints = %#v", got)
}
}
func request(value dnd.LocationOccurrenceList) contracts.TypedValidationRequest[dnd.LocationOccurrenceList] {
return contracts.TypedValidationRequest[dnd.LocationOccurrenceList]{Value: value}
}
func occurrence(kind dnd.LocationOccurrenceKind) dnd.LocationOccurrence {
return dnd.LocationOccurrence{LocationID: "location:one", Name: "Moon Gate", Kind: kind, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}
}