2026-07-07 21:37:58 +00:00
package shape
import (
"context"
2026-07-17 07:02:30 +00:00
"fmt"
"strings"
2026-07-07 21:37:58 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
2026-07-17 07:02:30 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
2026-07-07 21:37:58 +00:00
)
2026-07-21 14:43:20 +00:00
const (
Key = "extract/dnd/spells/shape"
ReasonCode = "invalid_spell_shape"
policy = "dnd.spells.validator.shape.v1"
)
2026-07-07 21:37:58 +00:00
2026-07-17 07:02:30 +00:00
type Options struct { }
2026-07-07 21:37:58 +00:00
type Validator struct { }
2026-07-17 07:02:30 +00:00
var _ contracts . TypedValidator [ dnd . SpellList ] = ( * Validator ) ( nil )
2026-07-21 14:43:20 +00:00
var _ pipeline . CheckpointFingerprintProvider = ( * Validator ) ( nil )
2026-07-07 21:37:58 +00:00
2026-07-17 07:02:30 +00:00
func New ( Options ) * Validator { return & Validator { } }
func ( v * Validator ) Name ( ) string { return Key }
2026-07-07 21:37:58 +00:00
func ( v * Validator ) ExecutionClass ( ) contracts . ExecutionClass {
return contracts . ExecutionClassDeterministic
}
2026-07-21 14:43:20 +00:00
func ( v * Validator ) CheckpointFingerprints ( ) [ ] pipeline . CheckpointFingerprint {
return [ ] pipeline . CheckpointFingerprint { { Name : "policy" , Value : policy } }
}
2026-07-17 07:02:30 +00:00
func ( v * Validator ) Validate ( _ context . Context , req contracts . TypedValidationRequest [ dnd . SpellList ] ) ( contracts . ValidationResult , error ) {
if err := Validate ( req . Value ) ; err != nil {
2026-07-07 21:37:58 +00:00
return rejection ( err . Error ( ) ) , nil
}
return contracts . ValidationResult { Approved : true } , nil
}
2026-07-17 07:02:30 +00:00
func Validate ( value dnd . SpellList ) error {
if value . SpellCasts == nil {
return fmt . Errorf ( "spell_casts must be present" )
}
for index , spell := range value . SpellCasts {
if strings . TrimSpace ( spell . Caster ) == "" {
return fmt . Errorf ( "spell_casts[%d].caster must not be empty" , index )
}
if strings . TrimSpace ( spell . Spell ) == "" {
return fmt . Errorf ( "spell_casts[%d].spell must not be empty" , index )
}
if len ( spell . SourceRefs ) == 0 {
return fmt . Errorf ( "spell_casts[%d].source_refs must not be empty" , index )
}
2026-07-07 21:37:58 +00:00
}
2026-07-17 07:02:30 +00:00
return nil
2026-07-07 21:37:58 +00:00
}
2026-07-17 07:02:30 +00:00
func Spec ( ) pipeline . ValidatorSpec {
return pipeline . ValidatorSpec { Key : Key , ExecutionClass : contracts . ExecutionClassDeterministic }
}
2026-07-07 21:37:58 +00:00
func Register ( registry * pipeline . ValidatorRegistry ) error {
2026-07-17 07:02:30 +00:00
return pipeline . RegisterTypedValidatorBuilder ( registry , dnd . SpellListKind , Spec ( ) , validateOptions , func ( request pipeline . BuildRequest ) ( contracts . TypedValidator [ dnd . SpellList ] , error ) {
options , err := DecodeOptions ( request . Options )
if err != nil {
return nil , err
}
return New ( options ) , nil
2026-07-07 21:37:58 +00:00
} )
}
2026-07-17 07:02:30 +00:00
func DecodeOptions ( options map [ string ] any ) ( Options , error ) {
if err := pipeline . RejectUnknownOptions ( options ) ; err != nil {
return Options { } , err
2026-07-07 21:37:58 +00:00
}
2026-07-17 07:02:30 +00:00
return Options { } , nil
}
func validateOptions ( options map [ string ] any ) error { _ , err := DecodeOptions ( options ) ; return err }
func rejection ( message string ) contracts . ValidationResult {
2026-08-27 11:50:37 +00:00
return contracts . ValidationResult { Approved : false , ReasonCode : ReasonCode , Message : message , CorrectionGuidance : "Return a complete spell-cast list with a nonblank spell name and caster plus valid source references for every cast." }
2026-07-07 21:37:58 +00:00
}