Improve D&D validation reliability
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/location-occurrences/registry"
|
||||
ReasonCode = "invalid_location_occurrence_registry"
|
||||
policy = "dnd.location_occurrences.validator.registry.v1"
|
||||
policy = "dnd.location_occurrences.validator.registry.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -76,27 +76,33 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
return contracts.ValidationResult{}, fmt.Errorf("resolve location registry: %w", err)
|
||||
}
|
||||
if !registry.Bound() {
|
||||
return rejection([]string{"location registry reference is required"}), nil
|
||||
var corrections diagnostics.Corrections
|
||||
corrections.Add("registry", "Use only proper contextual location names from the supplied location registry; omit an occurrence that cannot be matched unambiguously.", "")
|
||||
return rejection([]string{"location registry reference is required"}, corrections), nil
|
||||
}
|
||||
issues := make([]string, 0)
|
||||
var corrections diagnostics.Corrections
|
||||
for index, occurrence := range req.Value.Occurrences {
|
||||
record := fmt.Sprintf("Affected %s occurrence for location %s %s.", diagnostics.Quote(string(occurrence.Kind)), diagnostics.Quote(occurrence.Name), diagnostics.SourceRange(occurrence.SourceRefs))
|
||||
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)))
|
||||
corrections.Add("registry", "Use only proper contextual location names from the supplied location registry; omit an occurrence that cannot be matched unambiguously.", record)
|
||||
continue
|
||||
}
|
||||
if occurrence.Name != location.Name {
|
||||
issues = append(issues, fmt.Sprintf("occurrences[%d] does not match registry location %s", index, diagnostics.Quote(occurrence.LocationID)))
|
||||
corrections.Add("canonical-name", "Use the exact proper location name supplied by the registry.", record+" Use registry name "+diagnostics.Quote(location.Name)+".")
|
||||
}
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
return rejection(issues), nil
|
||||
return rejection(issues, corrections), nil
|
||||
}
|
||||
|
||||
func rejection(issues []string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: diagnostics.Aggregate("invalid location occurrence registry", issues), CorrectionGuidance: "Return location occurrences using proper contextual location names that match a location in the supplied registry; omit occurrences that cannot be matched unambiguously."}
|
||||
func rejection(issues []string, corrections diagnostics.Corrections) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: diagnostics.Aggregate("invalid location occurrence registry", issues), CorrectionGuidance: corrections.Guidance("Correct every rejected location occurrence and return the complete replacement occurrence list")}
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
|
||||
Reference in New Issue
Block a user