Refactor validators into built-in registry chains
This commit is contained in:
84
internal/validators/chains.go
Normal file
84
internal/validators/chains.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package validators
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/contracts"
|
||||
frameworkvalidators "gitea.maximumdirect.net/eric/audita/internal/framework/validators"
|
||||
)
|
||||
|
||||
var builtInChains = map[string][]string{
|
||||
"glossary": {
|
||||
KeyNoEffect,
|
||||
KeyOriginalTextPresence,
|
||||
KeyConfidenceThreshold,
|
||||
KeyProtectedTerms,
|
||||
KeyNonEmptyCorrectedText,
|
||||
KeySpokenFormPlausibility,
|
||||
KeyMeaningReversalReview,
|
||||
},
|
||||
"homophones": {
|
||||
KeyNoEffect,
|
||||
KeyOriginalTextPresence,
|
||||
KeyConfidenceThreshold,
|
||||
KeyProtectedTerms,
|
||||
KeyNonEmptyCorrectedText,
|
||||
KeySpokenFormPlausibility,
|
||||
KeyMeaningReversalReview,
|
||||
},
|
||||
"spoken_word": {
|
||||
KeyNoEffect,
|
||||
KeyOriginalTextPresence,
|
||||
KeyConfidenceThreshold,
|
||||
KeyProtectedTerms,
|
||||
KeyNonEmptyCorrectedText,
|
||||
KeySpokenWordReview,
|
||||
KeyMeaningReversalReview,
|
||||
},
|
||||
"grammar": {
|
||||
KeyNoEffect,
|
||||
KeyOriginalTextPresence,
|
||||
KeyConfidenceThreshold,
|
||||
KeyProtectedTerms,
|
||||
KeyNonEmptyCorrectedText,
|
||||
KeyGrammarReview,
|
||||
KeyMeaningReversalReview,
|
||||
},
|
||||
}
|
||||
|
||||
func BuiltInChainKeys(moduleKey string) ([]string, error) {
|
||||
keys, ok := builtInChains[moduleKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no built-in validator chain for module %q", moduleKey)
|
||||
}
|
||||
out := make([]string, len(keys))
|
||||
copy(out, keys)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func ResolveBuiltInChain(moduleKey string, registry *Registry) ([]contracts.Validator, error) {
|
||||
keys, err := BuiltInChainKeys(moduleKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if registry == nil {
|
||||
registry = NewBuiltInRegistry()
|
||||
}
|
||||
|
||||
out := make([]contracts.Validator, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
if moduleKey == "glossary" && key == KeyProtectedTerms {
|
||||
// Glossary stages preserve current stricter protection semantics while
|
||||
// reporting the stable protected_terms key.
|
||||
v := frameworkvalidators.GlossaryStageProtectedGlossaryTermValidator{}
|
||||
out = append(out, v)
|
||||
continue
|
||||
}
|
||||
v, buildErr := registry.MustBuild(key)
|
||||
if buildErr != nil {
|
||||
return nil, buildErr
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user