105 lines
3.4 KiB
Go
105 lines
3.4 KiB
Go
package spells
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/validate"
|
|
)
|
|
|
|
const (
|
|
shapeValidatorName = "dnd/spells/shape"
|
|
sourceRefValidatorName = "dnd/spells/source_refs"
|
|
|
|
reasonInvalidPayload = "invalid_payload"
|
|
reasonMissingRequiredField = "missing_required_field"
|
|
reasonMissingSourceRef = "missing_source_ref"
|
|
reasonInvalidSourceRef = "invalid_source_ref"
|
|
)
|
|
|
|
var _ contracts.Validator = ShapeValidator{}
|
|
var _ contracts.Validator = SourceRefValidator{}
|
|
|
|
type ShapeValidator struct{}
|
|
|
|
func (validator ShapeValidator) Name() string {
|
|
return shapeValidatorName
|
|
}
|
|
|
|
func (validator ShapeValidator) Validate(ctx context.Context, req contracts.ValidationRequest) (contracts.ValidationResult, error) {
|
|
decisions := make([]contracts.ValidationDecision, 0, len(req.Candidates))
|
|
for _, candidate := range req.Candidates {
|
|
decisions = append(decisions, validateShape(candidate))
|
|
}
|
|
return contracts.ValidationResult{
|
|
ValidatorName: validator.Name(),
|
|
Decisions: decisions,
|
|
}, nil
|
|
}
|
|
|
|
type SourceRefValidator struct{}
|
|
|
|
func (validator SourceRefValidator) Name() string {
|
|
return sourceRefValidatorName
|
|
}
|
|
|
|
func (validator SourceRefValidator) Validate(ctx context.Context, req contracts.ValidationRequest) (contracts.ValidationResult, error) {
|
|
if req.Source == nil {
|
|
return contracts.ValidationResult{}, fmt.Errorf("dnd spells source refs validator: source must not be nil")
|
|
}
|
|
|
|
decisions := make([]contracts.ValidationDecision, 0, len(req.Candidates))
|
|
for _, candidate := range req.Candidates {
|
|
decisions = append(decisions, validateSourceRefs(req.Source, candidate))
|
|
}
|
|
return contracts.ValidationResult{
|
|
ValidatorName: validator.Name(),
|
|
Decisions: decisions,
|
|
}, nil
|
|
}
|
|
|
|
func validateShape(candidate artifacts.ArtifactCandidate) contracts.ValidationDecision {
|
|
var payload SpellCast
|
|
if err := json.Unmarshal(candidate.Payload, &payload); err != nil {
|
|
return validate.Rejected(candidate.Index, reasonInvalidPayload, fmt.Sprintf("invalid spell cast payload: %v", err))
|
|
}
|
|
for _, field := range requiredSpellCastFields(payload) {
|
|
if strings.TrimSpace(field.value) == "" {
|
|
return validate.Rejected(candidate.Index, reasonMissingRequiredField, fmt.Sprintf("missing required field %q", field.name))
|
|
}
|
|
}
|
|
return validate.Approved(candidate.Index)
|
|
}
|
|
|
|
func validateSourceRefs(doc *source.SourceDocument, candidate artifacts.ArtifactCandidate) contracts.ValidationDecision {
|
|
if len(candidate.SourceRefs) == 0 {
|
|
return validate.Rejected(candidate.Index, reasonMissingSourceRef, "spell cast candidate must include at least one source ref")
|
|
}
|
|
for _, ref := range candidate.SourceRefs {
|
|
if err := source.ValidateRef(doc, ref); err != nil {
|
|
return validate.Rejected(candidate.Index, reasonInvalidSourceRef, err.Error())
|
|
}
|
|
}
|
|
return validate.Approved(candidate.Index)
|
|
}
|
|
|
|
func requiredSpellCastFields(payload SpellCast) []struct {
|
|
name string
|
|
value string
|
|
} {
|
|
return []struct {
|
|
name string
|
|
value string
|
|
}{
|
|
{name: "caster", value: payload.Caster},
|
|
{name: "spell", value: payload.Spell},
|
|
{name: "effect", value: payload.Effect},
|
|
{name: "narrative_description", value: payload.NarrativeDescription},
|
|
}
|
|
}
|