2026-05-12 00:26:27 +00:00
package validators
import (
"context"
"strings"
2026-05-23 10:07:06 -05:00
2026-11-16 04:09:25 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/core/glossary"
2026-10-11 01:40:38 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
2026-05-12 00:26:27 +00:00
)
2026-05-23 10:07:06 -05:00
type ProposalShapeValidator struct { }
func ( v ProposalShapeValidator ) Name ( ) string { return "proposal_shape" }
func ( v ProposalShapeValidator ) Validate ( _ context . Context , req Request ) ( Result , error ) {
decisions := make ( [ ] Decision , 0 , len ( req . CandidateProposal ) )
for _ , c := range req . CandidateProposal {
switch {
case c . TargetSegmentID <= 0 :
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonInvalidTargetSegment , "proposal target segment id must be positive" ) )
2026-09-25 02:48:57 +00:00
case c . OriginalText == "" :
2026-05-23 10:07:06 -05:00
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonEmptyOriginalText , "proposal original_text must not be empty" ) )
default :
decisions = append ( decisions , approval ( c . ProposalIndex ) )
}
}
if err := EnforceDecisionCardinality ( req . CandidateProposal , decisions ) ; err != nil {
return Result { } , err
}
return Result { ValidatorName : v . Name ( ) , Decisions : decisions } , nil
}
2026-05-12 00:26:27 +00:00
type OriginalTextPresenceValidator struct { }
func ( v OriginalTextPresenceValidator ) Name ( ) string { return "original_text_presence" }
func ( v OriginalTextPresenceValidator ) Validate ( _ context . Context , req Request ) ( Result , error ) {
2026-11-11 12:28:00 +00:00
section := req . sectionLookup ( )
2026-11-16 14:11:02 +00:00
req . Section = section
2026-05-12 00:26:27 +00:00
decisions := make ( [ ] Decision , 0 , len ( req . CandidateProposal ) )
for _ , c := range req . CandidateProposal {
2026-11-11 12:28:00 +00:00
position , ok := section . Position ( c . TargetSegmentID )
2026-05-12 00:26:27 +00:00
if ! ok {
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonMissingTargetSegment , "target segment was not found" ) )
continue
}
2026-11-11 12:28:00 +00:00
if ! strings . Contains ( section . segments [ position ] . Text , c . OriginalText ) {
2026-05-12 00:26:27 +00:00
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonMissingOriginalText , "original_text was not found in current segment text" ) )
continue
}
2026-11-17 04:20:25 +00:00
if req . ReplacementPolicy == proposals . ReplacementPolicyRequireUnique && req . Preview ( c ) . SkipReason == proposals . SkipReasonAmbiguousOriginal {
2026-11-16 14:11:02 +00:00
decisions = append ( decisions , rejection ( c . ProposalIndex , string ( proposals . SkipReasonAmbiguousOriginal ) , "original_text matches multiple locations in the target segment" ) )
continue
}
2026-05-12 00:26:27 +00:00
decisions = append ( decisions , approval ( c . ProposalIndex ) )
}
if err := EnforceDecisionCardinality ( req . CandidateProposal , decisions ) ; err != nil {
return Result { } , err
}
return Result { ValidatorName : v . Name ( ) , Decisions : decisions } , nil
}
2026-09-25 02:48:57 +00:00
type NonEmptyResultingSegmentValidator struct { }
2026-05-12 00:26:27 +00:00
2026-09-25 02:48:57 +00:00
func ( v NonEmptyResultingSegmentValidator ) Name ( ) string { return "non_empty_resulting_segment" }
2026-05-12 00:26:27 +00:00
2026-09-25 02:48:57 +00:00
func ( v NonEmptyResultingSegmentValidator ) Validate ( _ context . Context , req Request ) ( Result , error ) {
2026-11-11 12:28:00 +00:00
req . Section = req . sectionLookup ( )
2026-05-23 10:07:06 -05:00
2026-05-12 00:26:27 +00:00
decisions := make ( [ ] Decision , 0 , len ( req . CandidateProposal ) )
for _ , c := range req . CandidateProposal {
2026-11-11 12:28:00 +00:00
preview := req . Preview ( c )
2026-05-23 10:07:06 -05:00
if preview . SkipReason == proposals . SkipReasonEmptyResultingText {
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonEmptyResultingText , "proposal would leave the segment empty" ) )
2026-05-12 00:26:27 +00:00
continue
}
decisions = append ( decisions , approval ( c . ProposalIndex ) )
}
if err := EnforceDecisionCardinality ( req . CandidateProposal , decisions ) ; err != nil {
return Result { } , err
}
return Result { ValidatorName : v . Name ( ) , Decisions : decisions } , nil
}
type NoEffectValidator struct { }
func ( v NoEffectValidator ) Name ( ) string { return "no_effect" }
func ( v NoEffectValidator ) Validate ( _ context . Context , req Request ) ( Result , error ) {
decisions := make ( [ ] Decision , 0 , len ( req . CandidateProposal ) )
for _ , c := range req . CandidateProposal {
if c . OriginalText == c . CorrectedText {
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonNoEffect , "original_text and corrected_text are identical" ) )
continue
}
decisions = append ( decisions , approval ( c . ProposalIndex ) )
}
if err := EnforceDecisionCardinality ( req . CandidateProposal , decisions ) ; err != nil {
return Result { } , err
}
return Result { ValidatorName : v . Name ( ) , Decisions : decisions } , nil
}
2026-11-10 17:25:08 +00:00
func ValidateProtectedTerms ( req Request ) ( Result , error ) {
2026-11-16 04:09:25 +00:00
var vocab glossary . ProtectedVocabulary
2026-11-10 20:42:28 +00:00
if req . GlossaryView != nil {
vocab = req . GlossaryView . Protection
} else {
2026-11-16 04:09:25 +00:00
vocab = glossary . NewProtectedVocabulary ( req . ProtectedTerms )
2026-11-10 20:42:28 +00:00
}
2026-05-12 00:26:27 +00:00
decisions := make ( [ ] Decision , 0 , len ( req . CandidateProposal ) )
2026-11-16 04:09:25 +00:00
hasTerms := ! vocab . Empty ( )
if hasTerms {
req . Section = req . sectionLookup ( )
}
repetitionCleanup := req . ModuleKey == modulecatalog . KeySpokenWord
2026-05-12 00:26:27 +00:00
for _ , c := range req . CandidateProposal {
2026-11-16 04:09:25 +00:00
var reason string
if hasTerms {
2026-11-11 12:28:00 +00:00
if position , ok := req . Section . Position ( c . TargetSegmentID ) ; ok {
preview := req . Preview ( c )
2026-11-16 04:09:25 +00:00
if preview . HasResult ( ) {
reason = vocab . Violation ( req . Section . segments [ position ] . Text , preview . CorrectedSegmentText , repetitionCleanup )
2026-09-25 12:23:10 +00:00
}
}
}
2026-05-12 11:20:02 +00:00
if reason != "" {
decisions = append ( decisions , rejection ( c . ProposalIndex , ReasonProtectedGlossaryTerm , reason ) )
continue
2026-05-12 00:26:27 +00:00
}
2026-05-12 11:20:02 +00:00
decisions = append ( decisions , approval ( c . ProposalIndex ) )
2026-05-12 00:26:27 +00:00
}
2026-05-12 11:20:02 +00:00
if err := EnforceDecisionCardinality ( req . CandidateProposal , decisions ) ; err != nil {
return Result { } , err
}
2026-05-14 00:28:28 +00:00
return Result { ValidatorName : "protected_terms" , Decisions : decisions } , nil
2026-05-12 00:26:27 +00:00
}