Skip to content

Commit

Permalink
WIP: fix JsonRpcRequest to be valid json
Browse files Browse the repository at this point in the history
fix/types: JsonRpcParams should be valid JSON values

`undefined` is not valid JSON.

https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
(Section 5):

    A JSON value can be an object, array, number, string, true, false, or null.

fix/json: coerce away `undefined` params in JsonRpcParamsStruct
  • Loading branch information
legobeat committed Aug 24, 2023
1 parent 20bb059 commit 2e2f5d4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,28 @@ export type JsonRpcError = OptionalField<
export const JsonRpcParamsStruct = optional(
union([record(string(), JsonStruct), array(JsonStruct)]),
);

export type JsonRpcParams = Infer<typeof JsonRpcParamsStruct>;

export const JsonRpcRequestStruct = object({
export const JsonRpcRequestStruct: Struct<
{
id: number | null | string;
method: string;
jsonrpc: '2.0';
params?: Json[] | Record<string, Json>;
},
{
id: Struct<string | number | null, null>;
jsonrpc: Struct<'2.0', '2.0'>;
method: Struct<string, null>;
params?: any;
}
> = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
method: string(),
params: JsonRpcParamsStruct,
});
}) as any;

export type InferWithParams<
Type extends Struct<any>,
Expand Down

0 comments on commit 2e2f5d4

Please sign in to comment.