Validation
Inventory should receive a meaningful SKU and a positive quantity. Put those rules beside the fields in your protobuf contract; the configured validation interceptor checks it before the handler runs.
Two rules
With buf/validate/validate.proto imported:
Proto-first validation
Try the rules
Set quantity to 0, then fix it. The error comes from the rule beside the field.
inventory.proto
message CheckStockRequest {
string sku = 1 [
(buf.validate.field).string = {
min_len: 1
}
];
int32 quantity = 2 [
(buf.validate.field).int32 = {
gt: 0
}
];
}The generated descriptor carries both rules. Protovalidate reads them on every edit.
Runs in your browser with @bufbuild/protovalidate and the example handler. No RPC is sent. The handler looks up the SKU in a small inventory fixture.
Compare example inputs
Known SKU · OK
The request passes validation. The handler looks up A-1 in the example inventory and returns its stock.
{
"sku": "A-1",
"quantity": 2
}
{
"units": 7
}
Zero quantity · InvalidArgument
Zero violates int32.gt. Validation rejects the request before the handler runs.
{
"sku": "A-1",
"quantity": 0
}
{
"code": "InvalidArgument",
"issues": [
{
"field": "quantity",
"rule": "int32.gt",
"message": "must be greater than 0"
}
],
"message": "The proto rules reject this request. The handler does not run."
}
Empty SKU · InvalidArgument
The empty string violates the proto constraint. Validation rejects the request before the handler runs.
{
"sku": "",
"quantity": 2
}
{
"code": "InvalidArgument",
"issues": [
{
"field": "sku",
"rule": "string.min_len",
"message": "must be at least 1 characters"
}
],
"message": "The proto rules reject this request. The handler does not run."
}
Unknown SKU · NotFound
The non-empty string passes validation. The handler rejects the unknown SKU with NotFound: existence is a business rule.
{
"sku": "UNKNOWN",
"quantity": 2
}
{
"code": "NotFound",
"issues": [],
"message": "SKU not found"
}
Run the RPC example and inspect the handler
From a checkout of the documentation repository, run:
cd .vitepress/validation/example
pnpm install --frozen-lockfile
pnpm generate
pnpm typecheck
pnpm test
The test starts a real Connectum server and sends requests over HTTP. It checks that invalid SKU and quantity values never reach the handler.
Default interceptors install validation. The browser uses the same contract and handler; this setup runs them behind an RPC endpoint.
import { createServer, defineService } from '@connectum/core';
import { createDefaultInterceptors } from '@connectum/interceptors';
import { InventoryService } from '../gen/shop/v1/inventory_pb.ts';
import { checkStock } from './inventory.ts';
export function createExampleServer(onHandler: () => void) {
return createServer({
services: [
defineService(InventoryService, {
checkStock(req) {
onHandler();
return checkStock(req);
},
}),
],
interceptors: createDefaultInterceptors({ errorHandler: { logErrors: false } }),
host: '127.0.0.1',
port: 0,
allowHTTP1: true,
shutdown: { autoShutdown: false },
});
}
Complete contract:
syntax = "proto3";
package shop.v1;
import "buf/validate/validate.proto";
service InventoryService {
rpc CheckStock(CheckStockRequest) returns (CheckStockResponse);
}
message CheckStockRequest {
string sku = 1 [
(buf.validate.field).string = {
min_len: 1
}
];
int32 quantity = 2 [
(buf.validate.field).int32 = {
gt: 0
}
];
}
message CheckStockResponse {
int32 units = 1;
}
Example handler and inventory data:
import { Code, ConnectError } from '@connectrpc/connect';
// These are example inventory data, not a connection to a production database.
const stock = new Map([['A-1', 7]]);
export function checkStock(req: { sku: string }) {
const units = stock.get(req.sku);
if (units === undefined) {
throw new ConnectError('SKU not found', Code.NotFound);
}
return { units };
}
Regenerate the descriptors after changing constraints. createDefaultInterceptors() includes validation by default; the validation dependencies still need to be installed. Defining the annotation alone does not install middleware on a server.
How validation is wired
Go deeper
- Set up validation and test an invalid request.
- All constraint examples, error messages and business validation.
- Understand the interceptor chain.
Find a section from the full guide
The detailed material is preserved on a separate page. Earlier section links still work here.