Refactor validators into built-in registry chains
This commit is contained in:
104
internal/validators/registry.go
Normal file
104
internal/validators/registry.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package validators
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/contracts"
|
||||
frameworkvalidators "gitea.maximumdirect.net/eric/audita/internal/framework/validators"
|
||||
)
|
||||
|
||||
const (
|
||||
KeyConfidenceThreshold = "confidence_threshold"
|
||||
KeyOriginalTextPresence = "original_text_presence"
|
||||
KeyNonEmptyCorrectedText = "non_empty_corrected_text"
|
||||
KeyNoEffect = "no_effect"
|
||||
KeyProtectedTerms = "protected_terms"
|
||||
|
||||
KeySpokenFormPlausibility = "spoken_form_plausibility"
|
||||
KeyMeaningReversalReview = "meaning_reversal_review"
|
||||
KeyEditorialReview = "editorial_review"
|
||||
KeyGrammarReview = "grammar_review"
|
||||
KeySpokenWordReview = "spoken_word_review"
|
||||
)
|
||||
|
||||
type BuiltInValidatorDefinition struct {
|
||||
Key string
|
||||
Build func() (contracts.Validator, error)
|
||||
LLMBacked bool
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
definitions map[string]BuiltInValidatorDefinition
|
||||
}
|
||||
|
||||
func NewBuiltInRegistry() *Registry {
|
||||
defs := []BuiltInValidatorDefinition{
|
||||
{Key: KeyConfidenceThreshold, Build: func() (contracts.Validator, error) { return frameworkvalidators.ConfidenceThresholdValidator{}, nil }},
|
||||
{Key: KeyOriginalTextPresence, Build: func() (contracts.Validator, error) { return frameworkvalidators.OriginalTextPresenceValidator{}, nil }},
|
||||
{Key: KeyNonEmptyCorrectedText, Build: func() (contracts.Validator, error) { return frameworkvalidators.NonEmptyCorrectionValidator{}, nil }},
|
||||
{Key: KeyNoEffect, Build: func() (contracts.Validator, error) { return frameworkvalidators.NoEffectValidator{}, nil }},
|
||||
{Key: KeyProtectedTerms, Build: func() (contracts.Validator, error) { return frameworkvalidators.ProtectedGlossaryTermValidator{}, nil }},
|
||||
{Key: KeySpokenFormPlausibility, LLMBacked: true, Build: func() (contracts.Validator, error) {
|
||||
return frameworkvalidators.NewLLMBackedValidator(KeySpokenFormPlausibility, frameworkvalidators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
}},
|
||||
{Key: KeyMeaningReversalReview, LLMBacked: true, Build: func() (contracts.Validator, error) {
|
||||
return frameworkvalidators.NewLLMBackedValidator(KeyMeaningReversalReview, frameworkvalidators.LLMValidatorTypeMeaningReversal, "")
|
||||
}},
|
||||
{Key: KeyEditorialReview, LLMBacked: true, Build: func() (contracts.Validator, error) {
|
||||
return frameworkvalidators.NewLLMBackedValidator(KeyEditorialReview, frameworkvalidators.LLMValidatorTypeEditorialReview, "")
|
||||
}},
|
||||
{Key: KeyGrammarReview, LLMBacked: true, Build: func() (contracts.Validator, error) {
|
||||
return frameworkvalidators.NewLLMBackedValidator(KeyGrammarReview, frameworkvalidators.LLMValidatorTypeGrammarReview, "")
|
||||
}},
|
||||
{Key: KeySpokenWordReview, LLMBacked: true, Build: func() (contracts.Validator, error) {
|
||||
return frameworkvalidators.NewLLMBackedValidator(KeySpokenWordReview, frameworkvalidators.LLMValidatorTypeSpokenWordReview, "")
|
||||
}},
|
||||
}
|
||||
|
||||
m := make(map[string]BuiltInValidatorDefinition, len(defs))
|
||||
for _, def := range defs {
|
||||
m[def.Key] = def
|
||||
}
|
||||
return &Registry{definitions: m}
|
||||
}
|
||||
|
||||
func (r *Registry) Lookup(key string) (BuiltInValidatorDefinition, bool) {
|
||||
if r == nil {
|
||||
return BuiltInValidatorDefinition{}, false
|
||||
}
|
||||
def, ok := r.definitions[strings.TrimSpace(key)]
|
||||
return def, ok
|
||||
}
|
||||
|
||||
func (r *Registry) MustBuild(key string) (contracts.Validator, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("validator registry is nil")
|
||||
}
|
||||
def, ok := r.Lookup(key)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown validator key %q", key)
|
||||
}
|
||||
v, err := def.Build()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build validator %q: %w", key, err)
|
||||
}
|
||||
if v == nil {
|
||||
return nil, fmt.Errorf("build validator %q: returned nil validator", key)
|
||||
}
|
||||
if v.Name() != def.Key {
|
||||
return nil, fmt.Errorf("validator key/name mismatch for %q: got %q", key, v.Name())
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (r *Registry) RegisteredKeys() []string {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
keys := make([]string, 0, len(r.definitions))
|
||||
for k := range r.definitions {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
Reference in New Issue
Block a user