Add D&D location occurrence validators
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/locations/source_refs"
|
||||
ReasonCode = "invalid_location_source_refs"
|
||||
policy = "dnd.locations.validator.source_refs.v1"
|
||||
policy = "dnd.locations.validator.source_refs.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -35,6 +35,9 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.LocationList]) (contracts.ValidationResult, error) {
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk == nil {
|
||||
return contracts.ValidationResult{}, fmt.Errorf("location source-reference validator requires the current extraction chunk")
|
||||
}
|
||||
if err := locationshape.Validate(req.Value); err != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
@@ -44,6 +47,10 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
for refIndex, ref := range location.SourceRefs {
|
||||
if err := index.ValidateRef(ref); err != nil {
|
||||
issues = append(issues, fmt.Sprintf("locations[%d].source_refs[%d]: %s", locationIndex, refIndex, diagnostics.Truncate(err.Error())))
|
||||
continue
|
||||
}
|
||||
if req.Stage == string(pipeline.StageExtract) && !chunkContainsRef(req.Chunk, ref) {
|
||||
issues = append(issues, fmt.Sprintf("locations[%d].source_refs[%d]: source reference is outside the current extraction chunk", locationIndex, refIndex))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,6 +60,19 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
return rejection(diagnostics.Aggregate("invalid location source references", issues)), nil
|
||||
}
|
||||
|
||||
func chunkContainsRef(chunk *source.Chunk, ref source.SourceRef) bool {
|
||||
if chunk == nil || ref.SourceID != chunk.SourceID {
|
||||
return false
|
||||
}
|
||||
startFound := false
|
||||
endFound := false
|
||||
for _, unit := range chunk.Units {
|
||||
startFound = startFound || unit.ID == ref.StartUnitID
|
||||
endFound = endFound || unit.ID == ref.EndUnitID
|
||||
}
|
||||
return startFound && endFound
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user