validation

package
v1.145.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package validation builds the protovalidate validator and the gRPC interceptor that enforces the buf.validate rules declared in the .proto files. A request that violates its rules is rejected with INVALID_ARGUMENT before reaching the handler. Handlers validate only the records of a bulk request themselves, through Validator, so one bad record refuses that record and not the call.

Index

Constants

View Source
const MaxReportedViolations = 20

MaxReportedViolations keeps the status trailers under the 8 KiB soft metadata limit of C-core clients such as grpc-ruby. Past it, a client sees INTERNAL instead of the real status.

View Source
const ReasonViolationsTruncated = "VIOLATIONS_TRUNCATED"

ReasonViolationsTruncated is the ErrorInfo reason Err adds when it reports fewer violations than it recorded. Its "total" metadata holds the full count.

Variables

This section is empty.

Functions

func NewUnaryInterceptor

func NewUnaryInterceptor() (grpc.UnaryServerInterceptor, error)

NewUnaryInterceptor returns a unary interceptor that validates every incoming request message against its buf.validate rules.

func Validator added in v1.104.1

func Validator() (protovalidate.Validator, error)

Validator returns the memoized validator. Handlers use it for per-record checks the interceptor skips.

Types

type FieldPath added in v1.145.0

type FieldPath []protoreflect.FieldDescriptor

FieldPath is the chain of fields from a request message down to the field a violation names. MustPath resolves it once at package init, so a misspelt name fails at startup.

func MustPath added in v1.145.0

func MustPath(root protoreflect.MessageDescriptor, names ...protoreflect.Name) FieldPath

func (FieldPath) At added in v1.145.0

func (p FieldPath) At(indexes ...int) *validate.FieldPath

At builds the path, giving each repeated field in turn the next of indexes. A repeated field with no index left names the whole list.

type Rule added in v1.145.0

type Rule struct {
	ID     string
	Denies bool
}

Rule is a check a handler runs after the interceptor. Its ID is API contract, like the rule ids in the .proto files. A rule that Denies makes the call PERMISSION_DENIED instead of INVALID_ARGUMENT.

type Violations added in v1.145.0

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

Violations collects every failed check in a bulk request into the buf.validate.Violations detail the interceptor uses, so callers read one error shape for both. It keeps at most MaxReportedViolations violations in total, denied ones first, and only counts the rest.

func (*Violations) Add added in v1.145.0

func (v *Violations) Add(rule Rule, message string, path FieldPath, indexes ...int)

Add records a failure of rule at path, giving each repeated field on the path the next of indexes.

func (*Violations) Err added in v1.145.0

func (v *Violations) Err() error

Err returns nil when nothing failed. Denied violations take their slots before the others, so a caller can explain a PERMISSION_DENIED. The details are in request order, and the message names the violation behind the code.

Jump to

Keyboard shortcuts

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