@voiceflow/common
Version:
Junk drawer of utility functions
12 lines (11 loc) • 558 B
JavaScript
/**
* Copied from the implementation found in `@nest-zod/z` since it recommended to migrate to bare `zod`
* https://github.com/BenLorantfy/nestjs-zod/blob/main/packages/z/src/z/generic-types/json.ts
*/
import { z } from 'zod';
const literal = z.union([z.string(), z.number(), z.boolean()]);
const DEFAULT_MESSAGE = 'Expected value to be a JSON-serializable';
export const zJSON = (message = DEFAULT_MESSAGE) => {
const schema = z.lazy(() => z.union([literal, z.array(schema), z.record(z.string(), schema)], { error: message }));
return schema;
};