/
validateConfig.ts
70 lines (62 loc) 路 2.14 KB
/
validateConfig.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { logger } from "@prisma/internals";
import z from "zod";
const booleanStringLiteral = z
.union([z.boolean(), z.literal("true"), z.literal("false")])
.transform((arg) => {
if (typeof arg === "boolean") return arg;
return arg === "true";
});
export const configValidator = z
.object({
// Meta information (not provided through user input)
databaseProvider: z.union([
z.literal("postgresql"),
z.literal("cockroachdb"),
z.literal("mysql"),
z.literal("sqlite"),
z.literal("sqlserver"),
]),
// Output overrides
fileName: z.string().optional().default("types.ts"),
enumFileName: z.string().optional(),
// Typescript type overrides
stringTypeOverride: z.string().optional(),
booleanTypeOverride: z.string().optional(),
intTypeOverride: z.string().optional(),
bigIntTypeOverride: z.string().optional(),
floatTypeOverride: z.string().optional(),
decimalTypeOverride: z.string().optional(),
dateTimeTypeOverride: z.string().optional(),
jsonTypeOverride: z.string().optional(),
bytesTypeOverride: z.string().optional(),
unsupportedTypeOverride: z.string().optional(),
// Support the Kysely camel case plugin
camelCase: booleanStringLiteral.default(false),
// Use GeneratedAlways for IDs instead of Generated
readOnlyIds: booleanStringLiteral.default(false),
})
.strict()
.transform((config) => {
if (!config.enumFileName) {
config.enumFileName = config.fileName;
}
return config as Omit<typeof config, "enumFileName"> &
Required<Pick<typeof config, "enumFileName">>;
});
export type Config = z.infer<typeof configValidator>;
export const validateConfig = (config: unknown) => {
const parsed = configValidator.safeParse(config);
if (!parsed.success) {
logger.error("Invalid prisma-kysely config");
Object.entries(parsed.error.flatten().fieldErrors).forEach(
([key, value]) => {
logger.error(`${key}: ${value.join(", ")}`);
}
);
Object.values(parsed.error.flatten().formErrors).forEach((value) => {
logger.error(`${value}`);
});
process.exit(1);
}
return parsed.data;
};