79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
type rawValidationKey struct {
|
|
stage ModuleStage
|
|
module string
|
|
}
|
|
|
|
type RawValidationRegistry struct {
|
|
chains map[rawValidationKey][]contracts.Validator
|
|
}
|
|
|
|
func NewRawValidationRegistry() *RawValidationRegistry {
|
|
return &RawValidationRegistry{
|
|
chains: make(map[rawValidationKey][]contracts.Validator),
|
|
}
|
|
}
|
|
|
|
func (r *RawValidationRegistry) Register(stage ModuleStage, module string, validators ...contracts.Validator) error {
|
|
if r == nil {
|
|
return fmt.Errorf("raw validation registry must not be nil")
|
|
}
|
|
normalizedModule := strings.TrimSpace(module)
|
|
if normalizedModule == "" {
|
|
return fmt.Errorf("raw validation module key must not be empty")
|
|
}
|
|
switch stage {
|
|
case StageChunk, StageExtract, StageMerge, StageNormalize:
|
|
default:
|
|
return fmt.Errorf("raw validation stage %q is not supported", stage)
|
|
}
|
|
if len(validators) == 0 {
|
|
return fmt.Errorf("raw validation chain for %q %q must not be empty", stage, normalizedModule)
|
|
}
|
|
|
|
chain := make([]contracts.Validator, 0, len(validators))
|
|
for i, validator := range validators {
|
|
if validator == nil {
|
|
return fmt.Errorf("raw validator %d for %q %q must not be nil", i, stage, normalizedModule)
|
|
}
|
|
if strings.TrimSpace(validator.Name()) == "" {
|
|
return fmt.Errorf("raw validator %d for %q %q must not have an empty name", i, stage, normalizedModule)
|
|
}
|
|
switch validator.ExecutionClass() {
|
|
case contracts.ExecutionClassDeterministic, contracts.ExecutionClassLLMBacked:
|
|
default:
|
|
return fmt.Errorf("raw validator %q for %q %q has unsupported execution class %q", validator.Name(), stage, normalizedModule, validator.ExecutionClass())
|
|
}
|
|
chain = append(chain, validator)
|
|
}
|
|
|
|
if r.chains == nil {
|
|
r.chains = make(map[rawValidationKey][]contracts.Validator)
|
|
}
|
|
key := rawValidationKey{stage: stage, module: normalizedModule}
|
|
if _, exists := r.chains[key]; exists {
|
|
return fmt.Errorf("raw validation chain for %q %q is already registered", stage, normalizedModule)
|
|
}
|
|
r.chains[key] = append([]contracts.Validator(nil), chain...)
|
|
return nil
|
|
}
|
|
|
|
func (r *RawValidationRegistry) Validators(stage ModuleStage, module string) []contracts.Validator {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
chain := r.chains[rawValidationKey{stage: stage, module: strings.TrimSpace(module)}]
|
|
if len(chain) == 0 {
|
|
return nil
|
|
}
|
|
return append([]contracts.Validator(nil), chain...)
|
|
}
|