TypeScript's type system instead of a Zod schema for input validation in next-safe-action
Answered
American posted this in #help-forum
AmericanOP
How can I use TypeScript's type system instead of a Zod schema for input validation in next-safe-action?
For example, in the following code, I want to enforce TypeScript type checking in parsedInput without using Zod:
How can I achieve this?
For example, in the following code, I want to enforce TypeScript type checking in parsedInput without using Zod:
export const uploadFileAction = actionClient.action(
async ({ parsedInput }: { files : File[]}) => {
try {
// Perform file upload
const { files } = parsedInput;
const formData = new FormData();
files.forEach((file) => {
formData.append('files', file, file.name);
});
const resp = await prisma.file.create({
data: {},
});
} catch (e) {}
}
);
How can I achieve this?
Answered by luis_llanes
As far as I know there is no way you can do that with TypeScript, if you only enforce types with TypeScript you’re risking security once TS transpiles to JS and types are gone
4 Replies
As far as I know there is no way you can do that with TypeScript, if you only enforce types with TypeScript you’re risking security once TS transpiles to JS and types are gone
Answer
Zod not only defines a typed schema, it also checks your object against the schema and throws/fails if it’s wrong
typescript is a compile-time type checker, it cannot check for runtime values
AmericanOP
yeah right