132 lines
5.0 KiB
Go
132 lines
5.0 KiB
Go
// Package invariants validates normalized D&D item-event artifacts.
|
|
package invariants
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"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"
|
|
itemeventmodel "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/itemevents"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
|
itemeventshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/itemevents/shape"
|
|
)
|
|
|
|
const (
|
|
Key = "normalize/dnd/item-events/invariants"
|
|
ReasonCode = "invalid_normalized_item_event_invariants"
|
|
policy = "dnd.item_events.normalize_invariants.v1"
|
|
)
|
|
|
|
type Options struct{}
|
|
type Validator struct{}
|
|
|
|
var _ contracts.TypedValidator[dnd.ItemOccurrenceList] = (*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.ItemOccurrenceList]) (contracts.ValidationResult, error) {
|
|
if err := Validate(req.Source, req.Value); err != nil {
|
|
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: err.Error()}, nil
|
|
}
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|
|
|
|
// Validate checks only invariants introduced by item-event normalization.
|
|
// Shape and source-reference failures remain owned by their earlier validators.
|
|
func Validate(doc *source.SourceDocument, value dnd.ItemOccurrenceList) error {
|
|
if itemeventshape.Validate(value) != nil {
|
|
return nil
|
|
}
|
|
index := source.NewDocumentIndex(doc)
|
|
if !sourceRefsValid(index, value) {
|
|
return nil
|
|
}
|
|
issues := issuesFor(shared.NewSourceRefOrderFromIndex(index), value)
|
|
if len(issues) == 0 {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%s", diagnostics.Aggregate("invalid normalized item occurrence invariants", issues))
|
|
}
|
|
|
|
func issuesFor(order shared.SourceRefOrder, value dnd.ItemOccurrenceList) []string {
|
|
issues := make([]string, 0)
|
|
seenIdentity := make(map[string]int)
|
|
for eventIndex, event := range value.Occurrences {
|
|
prefix := fmt.Sprintf("occurrences[%d]", eventIndex)
|
|
if event.Name != itemeventmodel.DisplayValue(event.Name) {
|
|
issues = append(issues, prefix+".name is not display-normalized")
|
|
}
|
|
if event.From != itemeventmodel.DisplayValue(event.From) {
|
|
issues = append(issues, prefix+".from is not display-normalized")
|
|
}
|
|
if event.To != itemeventmodel.DisplayValue(event.To) {
|
|
issues = append(issues, prefix+".to is not display-normalized")
|
|
}
|
|
for refIndex := 1; refIndex < len(event.SourceRefs); refIndex++ {
|
|
previous := event.SourceRefs[refIndex-1]
|
|
current := event.SourceRefs[refIndex]
|
|
if order.Less(current, previous) {
|
|
issues = append(issues, fmt.Sprintf("%s.source_refs are not in canonical order at index %d", prefix, refIndex))
|
|
} else if current == previous {
|
|
issues = append(issues, fmt.Sprintf("%s.source_refs[%d] duplicates the previous reference", prefix, refIndex))
|
|
}
|
|
}
|
|
key := itemeventmodel.ExactIdentity(order, event)
|
|
if previous, exists := seenIdentity[key]; exists {
|
|
issues = append(issues, fmt.Sprintf("%s duplicates item occurrence %d under normalized identity", prefix, previous))
|
|
} else {
|
|
seenIdentity[key] = eventIndex
|
|
}
|
|
}
|
|
for eventIndex := 1; eventIndex < len(value.Occurrences); eventIndex++ {
|
|
if itemeventmodel.Less(order, value.Occurrences[eventIndex], value.Occurrences[eventIndex-1]) {
|
|
issues = append(issues, fmt.Sprintf("occurrences[%d] is out of canonical order", eventIndex))
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func sourceRefsValid(index source.DocumentIndex, value dnd.ItemOccurrenceList) bool {
|
|
for _, event := range value.Occurrences {
|
|
if !itemeventmodel.ValidSourceRefs(index, event.SourceRefs) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func Spec() pipeline.ValidatorSpec {
|
|
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
|
}
|
|
|
|
func Register(registry *pipeline.ValidatorRegistry) error {
|
|
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.ItemOccurrenceListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.ItemOccurrenceList], 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 }
|