gormrules

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package gormrules provides database-backed validation rules on top of gorm: exists and not_exists check a value against table columns.

Usage in a rule expression:

exists:table,column[,column...]        the value exists in any column
not_exists:table,column[,column...]    the value exists in no column

Example: `validate:"required && not_exists:users,phone,email"`.

Register both on a validator with Register(v, db). Messages default to English; override them per validator with validator.WithMessages, e.g. {"exists": "{field} 不存在", "not_exists": "{field} 已存在"}.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(v *validator.Validator, db *gorm.DB)

Register adds the database-backed rules (exists, not_exists) to v.

Types

type Exists

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

Exists validates that a value exists in one of the given table columns.

func NewExists

func NewExists(db *gorm.DB) *Exists

func (*Exists) Message

func (r *Exists) Message() string

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

func (*Exists) PassesE

func (r *Exists) PassesE(f validator.Field) error

func (*Exists) Signature

func (r *Exists) Signature() string

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.

func NewNotExists

func NewNotExists(db *gorm.DB) *NotExists

func (*NotExists) Message

func (r *NotExists) Message() string

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

func (*NotExists) PassesE

func (r *NotExists) PassesE(f validator.Field) error

func (*NotExists) Signature

func (r *NotExists) Signature() string

Jump to

Keyboard shortcuts

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