validator

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	CheckKidUnique  = CheckKid("unique")
	CheckKidRefKey  = CheckKid("refKey")
	CheckKidNotNull = CheckKid("notnull")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CandidatePolicy added in v0.26.0

type CandidatePolicy struct {
	Previous    any
	FieldFilter func(string) bool
	// DeferredFields marks temporarily unavailable INSERT inputs, independently
	// of sparse coverage. A nonnil function requires nil normalized Previous.
	DeferredFields func(string) bool
	// SatisfiedReferences are trusted, exact reference receipts for this insert.
	// They require nil normalized Previous and nil DeferredFields.
	SatisfiedReferences []Reference
}

type Check

type Check struct {
	SQL            string
	Reference      Reference
	Field          *xunsafe.Field
	ErrorMsg       string
	CheckType      reflect.Type
	CheckField     *xunsafe.Field
	UniqueDep      *io.Column
	IdentityColumn *io.Column
	// contains filtered or unexported fields
}

type CheckKid

type CheckKid string

type Checks added in v0.5.1

type Checks struct {
	Type   reflect.Type
	Unique []*Check
	RefKey []*Check
	NoNull []*Check
	// contains filtered or unexported fields
}

func NewChecks added in v0.5.1

func NewChecks(p reflect.Type, presence *option.SetMarker) (*Checks, error)

func (*Checks) ValidateReferences added in v0.26.0

func (c *Checks) ValidateReferences(references []Reference) error

ValidateReferences rejects unknown, wrong-target and duplicate receipts using compiled metadata. It performs no validation callbacks or database queries.

type Option

type Option func(c *Options)

func WithCandidatePolicies added in v0.26.0

func WithCandidatePolicies(policies []CandidatePolicy) Option

WithCandidatePolicies supplies an aligned policy for each root candidate in a shallow batch. Nil Previous means insert; nil FieldFilter means full coverage. A typed nil Previous must point to the candidate's struct type. Empty typed batches must contain structs or struct pointers; empty []any is also accepted.

func WithFieldFilter added in v0.26.0

func WithFieldFilter(include func(field string) bool) Option

WithFieldFilter supplies explicit validation coverage in canonical Go field names. A nonnil filter takes precedence over WithSetMarker; it does not modify any marker. Nil leaves the default full/marker-gated behavior unchanged.

func WithLocation added in v0.5.2

func WithLocation(location string) Option

WithLocation creates with location option

func WithMaxPlaceholders added in v0.24.0

func WithMaxPlaceholders(size int) Option

WithMaxPlaceholders sets the maximum placeholder count for validator queries.

func WithPrevious added in v0.26.0

func WithPrevious(previous interface{}) Option

WithPrevious selects exact per-candidate uniqueness checks. Each previous row must already be identity-matched to the corresponding candidate by the caller. Nil means every candidate is an insert and excludes no stored row. A supplied collection must have exactly one entry per candidate; nil entries are inserts. Previous rows must have the same concrete row type and fully loaded constraint fields; a projected row must be mapped by its caller first. Use WithShallow(true): nested rows require independently matched previous rows, not positional reuse. This option never infers identity from candidate values or presence markers.

func WithRef

func WithRef(flag bool) Option

WithRef with ref key option

func WithSetMarker added in v0.7.1

func WithSetMarker() Option

func WithShallow added in v0.15.0

func WithShallow(flag bool) Option

WithShallow with shallow option

func WithTransaction added in v0.26.0

func WithTransaction(tx *sql.Tx) Option

WithTransaction executes constraint reads in the caller's transaction. Validation never commits, rolls back, or opens a replacement transaction.

func WithUnique

func WithUnique(flag bool) Option

WithUnique with unique option

type Options

type Options struct {
	CheckUnique       bool
	CheckRef          bool
	Location          string
	Shallow           bool
	MaxPlaceholders   int
	SetMarker         *option.SetMarker
	CandidatePolicies []CandidatePolicy

	Previous interface{}
	// contains filtered or unexported fields
}

func NewOptions

func NewOptions() *Options

type Path

type Path struct {
	Elements []string
	IsSlice  bool
}

func (*Path) AppendField

func (p *Path) AppendField(field string) *Path

func (*Path) AppendIndex

func (p *Path) AppendIndex(index int) *Path

func (*Path) String

func (p *Path) String() string

type Reference added in v0.26.0

type Reference struct {
	Field  string
	Schema string
	Table  string
	Column string
}

Reference identifies an exact compiled reference constraint. Schema is the declared RefDb, including an empty value; Field is the canonical Go name.

func (Reference) MatchesTarget added in v0.26.0

func (r Reference) MatchesTarget(other Reference, dialect *info.Dialect) (bool, error)

MatchesTarget compares explicit logical identity without changing the SQL or stored receipt descriptor. No default project/schema or case folding is inferred.

type Service

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

func New

func New() *Service

New creates a new validation service

func (*Service) Validate

func (s *Service) Validate(ctx context.Context, db *sql.DB, any interface{}, opts ...Option) (*Validation, error)

type Validation

type Validation struct {
	Violations []*Violation
	Failed     bool
}

func (*Validation) AppendNotNull added in v0.5.1

func (e *Validation) AppendNotNull(path *Path, field, msg string)

func (*Validation) AppendRef added in v0.5.1

func (e *Validation) AppendRef(path *Path, field string, value interface{}, msg string)

func (*Validation) AppendUnique added in v0.5.1

func (e *Validation) AppendUnique(path *Path, field string, value interface{}, msg string)

func (*Validation) Error added in v0.5.1

func (e *Validation) Error() string

func (*Validation) String added in v0.5.1

func (e *Validation) String() string

type Violation

type Violation struct {
	Location string
	Field    string
	Value    interface{}
	Message  string
	Check    string
}

Jump to

Keyboard shortcuts

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