diff --git a/package.json b/package.json index e962b7a..b6b86ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "libshv-js", - "version": "3.3.1", + "version": "3.3.2", "description": "Typescript implementation of libshv", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/src/zod.ts b/src/zod.ts index 95ef462..cd7de52 100644 --- a/src/zod.ts +++ b/src/zod.ts @@ -1,9 +1,9 @@ import {z, type ZodType} from 'zod'; -import {shvMapType, UInt} from './rpcvalue'; +import {IMap, isIMap, isShvMap, RpcValue, ShvMap, shvMapType, UInt} from './rpcvalue'; -export const map = >>(schema: T) => z.object(schema).and(z.object({[shvMapType]: z.literal('map')})); -export const recmap = >(schema: T) => z.record(z.string(), schema).and(z.object({[shvMapType]: z.literal('map')})); -export const imap = >>(schema: T) => z.object(schema).and(z.object({[shvMapType]: z.literal('imap')})); +export const map = >>(schema: T) => z.custom}>>((data: RpcValue) => isShvMap(data) && z.object(schema).safeParse(data).success); +export const recmap = >(schema: T) => z.custom>>>((data: RpcValue) => isShvMap(data) && z.record(z.string(), schema).safeParse(data).success); +export const imap = >>(schema: T) => z.custom}>>((data: RpcValue) => isIMap(data) && z.object(schema).safeParse(data).success); export const int = () => z.number(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments -- Zod needs the default argument, otherwise it'll infer as UInt export const uint = () => z.instanceof(UInt);