gormrules

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package gormrules provides the database-backed exists and not_exists rules for the validator, implemented as FallibleRule over a *gorm.DB. Expressions name a table and one or more columns, for example "not_exists:users,phone,email": the value matches when any listed column equals it. Absent and zero values are skipped so presence stays the job of required, and a query failure surfaces as *validator.RuleError rather than as a field failure.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilDB = errors.New("gormrules: nil database")

ErrNilDB is returned by New when db is nil.

Functions

func New added in v0.1.1

func New(db *gorm.DB, options ...validator.Option) (*validator.Validator, error)

New constructs a Validator that has the exists and not_exists rules in addition to whatever options configure; the rules run their queries with the validation's context. Returns ErrNilDB for a nil db and any error validator.New reports for the options.

Types

type Exists

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

Exists validates that a value exists in one of the given table columns. It is registered by New and exported so a custom Validator can reuse it through validator.WithFallibleRules.

func (*Exists) CheckArgs added in v0.1.1

func (r *Exists) CheckArgs(args []string) error

CheckArgs rejects, at compile time, an expression without a table and at least one column, or with a name that is not a plain SQL identifier.

func (*Exists) Message

func (r *Exists) Message() string

Message is the fallback template; override it via validator.WithMessages.

func (*Exists) Signature

func (r *Exists) Signature() string

Signature returns "exists".

func (*Exists) Validate added in v0.1.1

func (r *Exists) Validate(f *validator.Field) (bool, error)

Validate counts matching rows; a query error is returned as-is and the validator reports it as a *validator.RuleError.

type NotExists

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

NotExists validates that a value does not exist in any of the given table columns, the usual uniqueness check on registration forms. It is registered by New and exported so a custom Validator can reuse it through validator.WithFallibleRules.

func (*NotExists) CheckArgs added in v0.1.1

func (r *NotExists) CheckArgs(args []string) error

CheckArgs rejects, at compile time, an expression without a table and at least one column, or with a name that is not a plain SQL identifier.

func (*NotExists) Message

func (r *NotExists) Message() string

Message is the fallback template; override it via validator.WithMessages.

func (*NotExists) Signature

func (r *NotExists) Signature() string

Signature returns "not_exists".

func (*NotExists) Validate added in v0.1.1

func (r *NotExists) Validate(f *validator.Field) (bool, error)

Validate counts matching rows; a query error is returned as-is and the validator reports it as a *validator.RuleError.

Jump to

Keyboard shortcuts

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