/
writeJsonValue.ts
35 lines (31 loc) · 1.02 KB
/
writeJsonValue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { type ContentWriterOptions } from '../../types';
export const writeJsonValue = ({
fileWriter: { writer, writeImport },
dmmf,
getSingleFileContent = false,
}: ContentWriterOptions) => {
const { useMultipleFiles, prismaClientPath } = dmmf.generatorConfig;
if (useMultipleFiles && !getSingleFileContent) {
writeImport('{ z }', 'zod');
writeImport('type { Prisma }', prismaClientPath);
}
writer
.blankLine()
.writeLine(
`export const JsonValue: z.ZodType<Prisma.JsonValue> = z.union([`,
)
.withIndentationLevel(1, () => {
writer
.writeLine(`z.string(),`)
.writeLine(`z.number(),`)
.writeLine(`z.boolean(),`)
.writeLine(`z.lazy(() => z.array(JsonValue)),`)
.writeLine(`z.lazy(() => z.record(JsonValue)),`);
})
.writeLine(`]);`)
.blankLine()
.writeLine(`export type JsonValueType = z.infer<typeof JsonValue>;`);
if (useMultipleFiles && !getSingleFileContent) {
writer.blankLine().writeLine(`export default JsonValue`);
}
};