Compilation and runtime errors from protovalidate are returned as Code.InvalidArgument

Open Beginner friendly
#67 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
backend

Research direction

Start in packages/validate/src/interceptor.ts, where validate() returns early on kind === "valid" and then throws a single ConnectError for everything else; branch on result.kind === "error" and use Code.Internal there while leaving kind: "invalid" as Code.InvalidArgument. Look at the existing interceptor tests in packages/validate/src to mirror their setup, then add cases for a CEL compile error (field not found: c) and a runtime error (int divide by zero) using the descriptor/router pattern from the repro. Done means the repro prints 13 int divide by zero and the invalid-rule tests still report 3.

Written by the indexing model from the issue text.

Description

When validator.validate() returns kind: "error" (CompilationError or RuntimeError, such as a CEL expression that does not compile or hits int divide by zero), createValidateInterceptor() throws Code.InvalidArgument on requests instead of Code.Internal.

Reproduction (repro.mjs):

// npm i @connectrpc/[email protected] @bufbuild/[email protected] @bufbuild/[email protected] @connectrpc/[email protected]
// node repro.mjs
import { create, setExtension, createFileRegistry } from "@bufbuild/protobuf";
import { FileDescriptorProtoSchema, MessageOptionsSchema } from "@bufbuild/protobuf/wkt";
import { file_buf_validate_validate, message as messageRules, MessageRulesSchema } from "@bufbuild/protovalidate/gen/buf/validate/validate_pb.js";
import { createRouterTransport, createClient, ConnectError } from "@connectrpc/connect";
import { createValidateInterceptor } from "@connectrpc/validate";

// message Req {
//   int64 a = 1;
//   int64 b = 2;
//   option (buf.validate.message).cel = { id: "div", expression: "this.a / this.b > 0" };
// }
const opts = create(MessageOptionsSchema);
setExtension(opts, messageRules, create(MessageRulesSchema, {
  cel: [{ id: "div", expression: "this.a / this.b > 0" }],
}));
const reg = createFileRegistry(
  create(FileDescriptorProtoSchema, {
    name: "t.proto",
    package: "t",
    syntax: "proto3",
    dependency: ["buf/validate/validate.proto"],
    messageType: [
      {
        name: "Req",
        options: opts,
        field: [
          { name: "a", number: 1, type: 3, label: 1, jsonName: "a" },
          { name: "b", number: 2, type: 3, label: 1, jsonName: "b" },
        ],
      },
      { name: "Res" },
    ],
    service: [{ name: "S", method: [{ name: "Call", inputType: ".t.Req", outputType: ".t.Res" }] }],
  }),
  (n) => (n === "buf/validate/validate.proto" ? file_buf_validate_validate : undefined),
);

const S = reg.getService("t.S");
const transport = createRouterTransport(
  ({ service }) => service(S, { call: () => ({}) }),
  { router: { interceptors: [createValidateInterceptor()] } },
);

try {
  await createClient(S, transport).call(create(reg.getMessage("t.Req"), { a: 4n, b: 0n }));
} catch (e) {
  const err = ConnectError.from(e);
  console.log(err.code, err.rawMessage);
}

Actual output:

3 int divide by zero

(3 is Code.InvalidArgument. A rule that fails to compile, such as this.c > 0, also returns 3 field not found: c on every request.)

Expected output: 13 int divide by zero (Code.Internal), reserving Code.InvalidArgument for kind: "invalid" rule violations, matching https://github.com/connectrpc/validate-go/pull/60.

In packages/validate/src/interceptor.ts, validate() checks if (result.kind === "valid") return; and throws new ConnectError(result.error.message, code, ...) for both kind: "invalid" and kind: "error".

Tested on @connectrpc/[email protected] (@bufbuild/[email protected], @bufbuild/[email protected], @connectrpc/[email protected], Node.js v26.13.0) and main (46aff93).

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
14
Forks
1
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from connectrpc/validate-es

All issues in connectrpc/validate-es

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.