@cratestack/validator-zod 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Stephane Segning
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @cratestack/validator-zod
2
+
3
+ An [`RpcLink`](https://github.com/cratestack/cratestack/tree/main/packages/cratestack-ts-types)
4
+ ([issue #182](https://github.com/cratestack/cratestack/issues/182)) for CrateStack's generated
5
+ TypeScript RPC client (`transport rpc` schemas) that validates `request.input` against a per-op
6
+ [zod](https://zod.dev/) schema before the call reaches the network — client-side validation with
7
+ the exact same schema shape your server-side procedure/model input already has, instead of hoping
8
+ the generated types alone catch a bad call at compile time.
9
+
10
+ ## Usage
11
+
12
+ ```ts
13
+ import { createZodValidatorLink } from "@cratestack/validator-zod";
14
+ import { CratestackRpcRuntime } from "./generated/runtime"; // your project's generated client
15
+ import { z } from "zod";
16
+
17
+ const runtime = new CratestackRpcRuntime("https://api.example.com", {
18
+ links: [
19
+ createZodValidatorLink({
20
+ "model.Order.create": z.object({ total: z.number().positive() }),
21
+ }),
22
+ ],
23
+ });
24
+ ```
25
+
26
+ Ops with no configured schema pass through unvalidated — this is opt-in per op, not a blanket
27
+ gate. On success, the schema's *parsed* output (defaults, `z.coerce`, transforms) becomes the
28
+ actual request `input`, not just a type-level assertion. On failure, the chain short-circuits —
29
+ the real network call never happens — and the call rejects the same way a server-side validation
30
+ failure would, with an `RpcErrorBody` carrying `code: "invalid_argument"`.
@@ -0,0 +1,21 @@
1
+ import type { RpcLink } from "@cratestack/ts-types";
2
+ import type { ZodTypeAny } from "zod";
3
+ /** Maps a `request.opId` to the schema its `input` must satisfy. Ops
4
+ * with no entry pass through unvalidated — this link is opt-in per op,
5
+ * not a blanket gate. */
6
+ export type ZodValidatorSchemas = Record<string, ZodTypeAny>;
7
+ /** Validates `request.input` against a per-op `zod` schema before
8
+ * calling `next()`, shipped as an
9
+ * [`RpcLink`](https://github.com/cratestack/cratestack/tree/main/packages/cratestack-ts-types)
10
+ * ([issue #182](https://github.com/cratestack/cratestack/issues/182)) so it composes with
11
+ * `@cratestack/link-batch`/`@cratestack/link-logger` instead of requiring its own call site.
12
+ *
13
+ * On success, `next()` receives the schema's *parsed* output as
14
+ * `input` — so `z.coerce.number()`/defaults/transforms actually take
15
+ * effect on the wire, not just at the type level. On failure, the
16
+ * chain short-circuits: `next()` is never called and the link resolves
17
+ * its own `Response` carrying an `RpcErrorBody` with
18
+ * `code: "invalid_argument"`, mirroring what the server itself would
19
+ * return for a rejected input (see `docs/design/rpc-transport.md`). */
20
+ export declare function createZodValidatorLink(schemas: ZodValidatorSchemas): RpcLink;
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEtC;;0BAE0B;AAC1B,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE7D;;;;;;;;;;;;wEAYwE;AACxE,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAmB5E"}
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ /** Validates `request.input` against a per-op `zod` schema before
2
+ * calling `next()`, shipped as an
3
+ * [`RpcLink`](https://github.com/cratestack/cratestack/tree/main/packages/cratestack-ts-types)
4
+ * ([issue #182](https://github.com/cratestack/cratestack/issues/182)) so it composes with
5
+ * `@cratestack/link-batch`/`@cratestack/link-logger` instead of requiring its own call site.
6
+ *
7
+ * On success, `next()` receives the schema's *parsed* output as
8
+ * `input` — so `z.coerce.number()`/defaults/transforms actually take
9
+ * effect on the wire, not just at the type level. On failure, the
10
+ * chain short-circuits: `next()` is never called and the link resolves
11
+ * its own `Response` carrying an `RpcErrorBody` with
12
+ * `code: "invalid_argument"`, mirroring what the server itself would
13
+ * return for a rejected input (see `docs/design/rpc-transport.md`). */
14
+ export function createZodValidatorLink(schemas) {
15
+ return async (request, next) => {
16
+ const schema = schemas[request.opId];
17
+ if (!schema) {
18
+ return next(request);
19
+ }
20
+ const result = schema.safeParse(request.input);
21
+ if (result.success) {
22
+ return next({ ...request, input: result.data });
23
+ }
24
+ const body = {
25
+ code: "invalid_argument",
26
+ message: `input for "${request.opId}" failed validation`,
27
+ details: result.error.flatten(),
28
+ };
29
+ return { response: new Response(request.codec.encode(body), { status: 400 }) };
30
+ };
31
+ }
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;wEAYwE;AACxE,MAAM,UAAU,sBAAsB,CAAC,OAA4B;IACjE,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC/C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAClD,CAAC;QAED,MAAM,IAAI,GAAiB;YACzB,IAAI,EAAE,kBAAkB;YACxB,OAAO,EAAE,cAAc,OAAO,CAAC,IAAI,qBAAqB;YACxD,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE;SAChC,CAAC;QACF,OAAO,EAAE,QAAQ,EAAE,IAAI,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;IACjF,CAAC,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@cratestack/validator-zod",
3
+ "version": "0.5.2",
4
+ "description": "An RpcLink for CrateStack's generated TypeScript RPC client that validates request input against per-op zod schemas.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/cratestack/cratestack.git",
9
+ "directory": "packages/cratestack-validator-zod"
10
+ },
11
+ "homepage": "https://cratestack.dev",
12
+ "bugs": "https://github.com/cratestack/cratestack/issues",
13
+ "keywords": ["cratestack", "cstack", "rpc", "zod", "validation", "trpc-links"],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": ["dist", "README.md", "LICENSE"],
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.json",
27
+ "test": "vitest run",
28
+ "lint": "biome check ."
29
+ },
30
+ "dependencies": {
31
+ "@cratestack/ts-types": "0.5.2"
32
+ },
33
+ "peerDependencies": {
34
+ "zod": "^3.23.0 || ^4.0.0"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "zod": { "optional": true }
38
+ },
39
+ "devDependencies": {
40
+ "typescript": "^5.7.0",
41
+ "vitest": "^3.0.0",
42
+ "zod": "^4.0.0"
43
+ },
44
+ "engines": {
45
+ "node": ">=18"
46
+ }
47
+ }