121 lines
4.7 KiB
Go
121 lines
4.7 KiB
Go
// Package registry validates location occurrence identity against location grounding.
|
|
package registry
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
locationregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/registry"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
|
occurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/locationoccurrences/shape"
|
|
)
|
|
|
|
const (
|
|
Key = "extract/dnd/location-occurrences/registry"
|
|
ReasonCode = "invalid_location_occurrence_registry"
|
|
policy = "dnd.location_occurrences.validator.registry.v1"
|
|
)
|
|
|
|
type Options struct{}
|
|
type Validator struct{ locationResolver *locationregistry.Resolver }
|
|
|
|
var _ contracts.TypedValidator[dnd.LocationOccurrenceList] = (*Validator)(nil)
|
|
var _ contracts.ManifestMetadataProvider = (*Validator)(nil)
|
|
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
|
|
|
func New(_ Options, references ...contracts.ReferenceSet) (*Validator, error) {
|
|
if len(references) > 1 {
|
|
return nil, fmt.Errorf("location occurrence registry validator accepts at most one reference set")
|
|
}
|
|
var referenceSet contracts.ReferenceSet
|
|
if len(references) == 1 {
|
|
referenceSet = references[0]
|
|
}
|
|
resolver, err := locationregistry.NewResolver(referenceSet)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("prepare location registry: %w", err)
|
|
}
|
|
return &Validator{locationResolver: resolver}, nil
|
|
}
|
|
|
|
func (v *Validator) Name() string { return Key }
|
|
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
|
return contracts.ExecutionClassDeterministic
|
|
}
|
|
func (v *Validator) ManifestMetadata() map[string]any {
|
|
if v == nil || v.locationResolver == nil {
|
|
return nil
|
|
}
|
|
metadata := map[string]any{"policy": policy}
|
|
seeded := v.locationResolver.Seeded()
|
|
if seeded.Bound() {
|
|
metadata["location_registry_digest"] = seeded.Digest()
|
|
metadata["location_count"] = seeded.Count()
|
|
}
|
|
return metadata
|
|
}
|
|
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
|
if v == nil || v.locationResolver == nil {
|
|
return nil
|
|
}
|
|
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}, {Name: "location_registry", Value: v.locationResolver.Seeded().ProjectionDigest()}}
|
|
}
|
|
|
|
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.LocationOccurrenceList]) (contracts.ValidationResult, error) {
|
|
if occurrenceshape.Validate(req.Value) != nil {
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|
|
if v == nil || v.locationResolver == nil {
|
|
return contracts.ValidationResult{}, fmt.Errorf("location occurrence registry validator must not be nil")
|
|
}
|
|
registry, err := v.locationResolver.Resolve(req.References)
|
|
if err != nil {
|
|
return contracts.ValidationResult{}, fmt.Errorf("resolve location registry: %w", err)
|
|
}
|
|
if !registry.Bound() {
|
|
return rejection([]string{"location registry reference is required"}), nil
|
|
}
|
|
issues := make([]string, 0)
|
|
for index, occurrence := range req.Value.Occurrences {
|
|
location, ok := registry.Lookup(occurrence.LocationID)
|
|
if !ok {
|
|
issues = append(issues, fmt.Sprintf("occurrences[%d].location_id is not in the location registry: %s", index, diagnostics.Quote(occurrence.LocationID)))
|
|
continue
|
|
}
|
|
if occurrence.Name != location.Name {
|
|
issues = append(issues, fmt.Sprintf("occurrences[%d] does not match registry location %s", index, diagnostics.Quote(occurrence.LocationID)))
|
|
}
|
|
}
|
|
if len(issues) == 0 {
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|
|
return rejection(issues), nil
|
|
}
|
|
|
|
func rejection(issues []string) contracts.ValidationResult {
|
|
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: diagnostics.Aggregate("invalid location occurrence registry", issues)}
|
|
}
|
|
|
|
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, request.References)
|
|
})
|
|
}
|
|
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 }
|