validators

package
v1.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Overview

Package validators provides content validation for LLM responses and user inputs.

This package implements various validators to ensure conversation quality:

  • Length and sentence count limits
  • Banned word detection
  • Role integrity (preventing role confusion)
  • Required field presence
  • Question and commit block validation

Validators are used during test execution to catch policy violations and ensure LLM responses meet quality standards.

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the global validator registry.

Functions

This section is empty.

Types

type BannedWordsValidator

type BannedWordsValidator struct {
	// contains filtered or unexported fields
}

BannedWordsValidator checks for banned words

func NewBannedWordsValidator

func NewBannedWordsValidator(bannedWords []string) *BannedWordsValidator

NewBannedWordsValidator creates a new banned words validator

func (*BannedWordsValidator) SupportsStreaming

func (v *BannedWordsValidator) SupportsStreaming() bool

SupportsStreaming returns true as banned words can be detected incrementally

func (*BannedWordsValidator) Validate

func (v *BannedWordsValidator) Validate(content string, params map[string]interface{}) ValidationResult

Validate checks for banned words in content

func (*BannedWordsValidator) ValidateChunk

func (v *BannedWordsValidator) ValidateChunk(chunk providers.StreamChunk, params ...map[string]interface{}) error

ValidateChunk validates a stream chunk for banned words and aborts if found

type CommitValidator

type CommitValidator struct{}

CommitValidator checks for commit/decision blocks in conversation responses

func NewCommitValidator

func NewCommitValidator() *CommitValidator

NewCommitValidator creates a new commit validator

func (*CommitValidator) SupportsStreaming

func (v *CommitValidator) SupportsStreaming() bool

SupportsStreaming returns false as commit validation requires complete content

func (*CommitValidator) Validate

func (v *CommitValidator) Validate(content string, params map[string]interface{}) ValidationResult

Validate checks for commit block with required fields

type LengthValidator

type LengthValidator struct{}

LengthValidator checks content length limits

func NewLengthValidator

func NewLengthValidator() *LengthValidator

NewLengthValidator creates a new length validator

func (*LengthValidator) SupportsStreaming

func (v *LengthValidator) SupportsStreaming() bool

SupportsStreaming returns true as length can be checked incrementally

func (*LengthValidator) Validate

func (v *LengthValidator) Validate(content string, params map[string]interface{}) ValidationResult

Validate checks content length against limits

func (*LengthValidator) ValidateChunk

func (v *LengthValidator) ValidateChunk(chunk providers.StreamChunk, params ...map[string]interface{}) error

ValidateChunk validates stream chunk against length limits and aborts if exceeded

type MaxSentencesValidator

type MaxSentencesValidator struct{}

MaxSentencesValidator checks sentence count limits

func NewMaxSentencesValidator

func NewMaxSentencesValidator() *MaxSentencesValidator

NewMaxSentencesValidator creates a new sentence count validator

func (*MaxSentencesValidator) SupportsStreaming

func (v *MaxSentencesValidator) SupportsStreaming() bool

SupportsStreaming returns false as sentence counting requires complete content

func (*MaxSentencesValidator) Validate

func (v *MaxSentencesValidator) Validate(content string, params map[string]interface{}) ValidationResult

Validate checks sentence count against max limit

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry maps validator type names to factory functions. This allows dynamic instantiation of validators from configuration.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new validator registry with built-in validators.

func (*Registry) Get

func (r *Registry) Get(validatorType string) (ValidatorFactory, bool)

Get retrieves a validator factory by type.

func (*Registry) HasValidator

func (r *Registry) HasValidator(validatorType string) bool

HasValidator returns true if a validator type is registered.

func (*Registry) Register

func (r *Registry) Register(validatorType string, factory ValidatorFactory)

Register adds a validator factory to the registry.

type RequiredFieldsValidator

type RequiredFieldsValidator struct{}

RequiredFieldsValidator checks for required fields in content

func NewRequiredFieldsValidator

func NewRequiredFieldsValidator() *RequiredFieldsValidator

NewRequiredFieldsValidator creates a new required fields validator

func (*RequiredFieldsValidator) SupportsStreaming

func (v *RequiredFieldsValidator) SupportsStreaming() bool

SupportsStreaming returns false as required fields must be in complete content

func (*RequiredFieldsValidator) Validate

func (v *RequiredFieldsValidator) Validate(content string, params map[string]interface{}) ValidationResult

Validate checks for required fields in content

type StreamingValidator

type StreamingValidator interface {
	Validator

	// ValidateChunk validates a stream chunk and returns error to abort stream
	// Returns nil to continue, ValidationAbortError to abort stream
	ValidateChunk(chunk providers.StreamChunk, params ...map[string]interface{}) error

	// SupportsStreaming returns true if this validator can validate incrementally
	SupportsStreaming() bool
}

StreamingValidator interface for validators that can check content incrementally and abort streaming early if validation fails

type ValidationResult

type ValidationResult struct {
	Passed  bool        `json:"passed"`
	Details interface{} `json:"details,omitempty"`
}

ValidationResult holds the result of a validation check

type Validator

type Validator interface {
	Validate(content string, params map[string]interface{}) ValidationResult
}

Validator interface for all validation checks

type ValidatorConfig

type ValidatorConfig struct {
	Type   string                 `json:"type" yaml:"type"`
	Params map[string]interface{} `json:"params" yaml:"params"`
}

ValidatorConfig defines a validator configuration from a prompt pack. This is just configuration data - validators are instantiated by the registry.

type ValidatorFactory

type ValidatorFactory func(params map[string]interface{}) Validator

ValidatorFactory creates a validator instance from configuration params. Params from the config are passed at construction time to allow validators to pre-compile patterns, build state, etc.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL