Files
Homarr/packages/validation/src/certificates.ts
Meier Lukas 75ba3f2ae7 refactor: remove central validation export to improve typescript performance (#2810)
* refactor: remove central validation export to improve typescript performance

* fix: missing package exports change in validation package

* chore: address pull request feedback
2025-04-06 10:37:28 +00:00

49 lines
1.1 KiB
TypeScript

import { z } from "zod";
import { createCustomErrorParams } from "./form/i18n";
export const certificateValidFileNameSchema = z.string().regex(/^[\w\-. ]+$/);
export const superRefineCertificateFile = (value: File | null, context: z.RefinementCtx) => {
if (!value) {
return context.addIssue({
code: "invalid_type",
expected: "object",
received: "null",
});
}
const result = certificateValidFileNameSchema.safeParse(value.name);
if (!result.success) {
return context.addIssue({
code: "custom",
params: createCustomErrorParams({
key: "invalidFileName",
params: {},
}),
});
}
if (value.type !== "application/x-x509-ca-cert" && value.type !== "application/pkix-cert") {
return context.addIssue({
code: "custom",
params: createCustomErrorParams({
key: "invalidFileType",
params: { expected: ".crt" },
}),
});
}
if (value.size > 1024 * 1024) {
return context.addIssue({
code: "custom",
params: createCustomErrorParams({
key: "fileTooLarge",
params: { maxSize: "1 MB" },
}),
});
}
return null;
};