Skip to Content

Getting started

Add the plugin to buf.gen.yaml and generate a schema.

Install

The plugin needs Node.js 24 or later.

npm install --save-dev protoc-gen-pvschema-ts @bufbuild/protoc-gen-es @bufbuild/buf
npm install @bufbuild/protobuf @bufbuild/protovalidate valibot

Install zod in place of valibot for lib=zod or lib=zod-mini.

Add the plugin

Add the plugin to buf.gen.yaml next to protoc-gen-es. Set lib to valibot, zod, or zod-mini.

buf.gen.yaml
version: v2
plugins:
  - local: protoc-gen-es
    out: src/gen
    opt:
      - target=ts
      # The generated messages import the buf.validate descriptor from @bufbuild/protovalidate.
      - rewrite_imports=./buf/validate/*_pb.js:@bufbuild/protovalidate/gen
  - local: protoc-gen-pvschema-ts
    out: src/gen
    opt:
      - target=ts
      - lib=valibot

Write the rules

user.proto
message CreateUserRequest {
  string name = 1 [(buf.validate.field).string = {min_len: 1, max_len: 50}];
  string email = 2 [(buf.validate.field).string.email = true];
  optional int32 age = 3 [(buf.validate.field).int32 = {gte: 0, lt: 150}];
}

Generate

buf generate

The plugin writes user_pvschema.ts. With lib=valibot:

user_pvschema.ts
import * as v from "valibot";

export const CreateUserRequestValidationSchema = v.object({
  name: v.pipe(v.string(), v.minLength(1), v.maxLength(50)),
  email: v.pipe(v.string(), v.email()),
  age: v.optional(v.pipe(v.number(), v.integer(), v.ltValue(150), v.minValue(0))),
});

/** The value that a form holds before validation. */
export type CreateUserRequestInput = v.InferInput<typeof CreateUserRequestValidationSchema>;

With lib=zod or lib=zod-mini. Both use the functional API, and only the import path differs.

user_pvschema.ts
import * as z from "zod";

export const CreateUserRequestValidationSchema = z.object({
  name: z.string().check(z.minLength(1), z.maxLength(50)),
  email: z.string().check(z.email()),
  age: z.optional(z.int32().check(z.lt(150), z.gte(0))),
});

/** The value that a form holds before validation. */
export type CreateUserRequestInput = z.input<typeof CreateUserRequestValidationSchema>;

Names

The names do not depend on the schema library.

ItemName
Fileuser_pvschema.ts
SchemaCreateUserRequestValidationSchema
Input typeCreateUserRequestInput
Nested messageOuter_InnerValidationSchema

protoc-gen-es exports CreateUserRequest and CreateUserRequestSchema from user_pb.ts. The plugin uses other names, so you can import both files in one module.

To generate schemas for two libraries, give each one its own out directory.