Skip to content

Commit c9ce1b4

Browse files
committed
feat(nestjs-json-rpc): simplify zParams definition and improve type safety in zRpcParams
1 parent 138ddb3 commit c9ce1b4

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

libs/json-rpc/nestjs-json-rpc/src/lib/types/payloade.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
import { z } from 'zod';
22

3-
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
4-
type Literal = z.infer<typeof literalSchema>;
5-
type Json = Literal | { [key: string]: Json } | Json[];
6-
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
7-
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
8-
);
9-
103
const zParams = z.union([
114
z.string(),
125
z.number(),
136
z.boolean(),
147
z.null(),
15-
jsonSchema,
8+
z.json(),
169
]);
1710

1811
export const zVersion = z.literal('2.0');
1912
const zMethod = z.string().transform((params, ctx) => {
2013
const result = params.split('.');
2114
if (result.length !== 2) {
2215
ctx.addIssue({
23-
code: z.ZodIssueCode.custom,
16+
code: 'custom',
2417
message: 'Not a 2 items',
2518
});
2619
return z.NEVER;
@@ -32,7 +25,7 @@ const zMethod = z.string().transform((params, ctx) => {
3225
});
3326

3427
const zRpcParams = z
35-
.union([z.array(zParams), z.record(zParams)])
28+
.union([z.array(zParams), z.record(z.string(), zParams)])
3629
.transform((params) =>
3730
Array.isArray(params) ? params : Object.values(params)
3831
);

0 commit comments

Comments
 (0)