Reuse document indexes in D&D validators
This commit is contained in:
@@ -36,10 +36,14 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.SceneDescriptionList]) (contracts.ValidationResult, error) {
|
||||
if shape.Validate(req.Value) != nil || !sourceRefsValid(req.Source, req.Value) {
|
||||
if shape.Validate(req.Value) != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := issuesFor(req.Source, req.Value)
|
||||
index := source.NewDocumentIndex(req.Source)
|
||||
if !sourceRefsValid(index, req.Value) {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := issuesFor(index, req.Value)
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
@@ -49,20 +53,20 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sourceRefsValid(doc *source.SourceDocument, value dnd.SceneDescriptionList) bool {
|
||||
func sourceRefsValid(index source.DocumentIndex, value dnd.SceneDescriptionList) bool {
|
||||
for _, scene := range value.Scenes {
|
||||
if source.ValidateRef(doc, scene.SourceRef) != nil {
|
||||
if index.ValidateRef(scene.SourceRef) != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func issuesFor(doc *source.SourceDocument, value dnd.SceneDescriptionList) []string {
|
||||
func issuesFor(index source.DocumentIndex, value dnd.SceneDescriptionList) []string {
|
||||
issues := make([]string, 0)
|
||||
if !sort.SliceIsSorted(value.Scenes, func(left, right int) bool {
|
||||
leftStart, _ := source.UnitIndex(doc, value.Scenes[left].SourceRef.StartUnitID)
|
||||
rightStart, _ := source.UnitIndex(doc, value.Scenes[right].SourceRef.StartUnitID)
|
||||
leftStart, _ := index.Position(value.Scenes[left].SourceRef.StartUnitID)
|
||||
rightStart, _ := index.Position(value.Scenes[right].SourceRef.StartUnitID)
|
||||
if leftStart != rightStart {
|
||||
return leftStart < rightStart
|
||||
}
|
||||
|
||||
@@ -42,9 +42,10 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk == nil {
|
||||
issues = append(issues, "current extraction chunk must not be nil")
|
||||
}
|
||||
for index, scene := range req.Value.Scenes {
|
||||
prefix := fmt.Sprintf("scenes[%d]", index)
|
||||
if err := source.ValidateRef(req.Source, scene.SourceRef); err != nil {
|
||||
index := source.NewDocumentIndex(req.Source)
|
||||
for sceneIndex, scene := range req.Value.Scenes {
|
||||
prefix := fmt.Sprintf("scenes[%d]", sceneIndex)
|
||||
if err := index.ValidateRef(scene.SourceRef); err != nil {
|
||||
issues = append(issues, prefix+".source_ref: "+diagnostics.Truncate(err.Error()))
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user