@JoGrenat
@JoGrenat
Jordane Grenat
npx comet-craft
type Result =
| { type: 'success', data: MyData }
| { type: 'error', error: AnError }
const ResultSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('success'), data: MyDataSchema }),
z.object({ type: z.literal('error'), error: ErrorSchema }),
]);
describe('createUser', () => {
it('should save user', () => {
const anyUser = ZodFastCheck().inputOf(UserSchema);
fc.assert(
fc.property(anyUser, (userInput) => {
await createUser(userInput);
const foundUser = await users.findByEmail(userInput.email);
expect(foundUser).toBeDefined();
})
);
});
});
import { zfd } from "zod-form-data";
const schema = zfd.formData({
name: zfd.text(),
age: zfd.numeric(z.number().min(25).max(50)),
likesPizza: zfd.checkbox(),
});
export const action = async ({ request }) => {
const { name, age, likesPizza } = schema.parse(await request.formData());
};
const appRouter = router({
getUserById: publicProcedure
.input(z.number())
.query(async (req) => {
const userId = req.input;
return findUser(userId);
}),
createUser: procedure
.input(UserCreationSchema)
.mutation(async (req) => {
const userToCreate = req.input;
const newUser = await createUser(userToCreate);
return newUser;
}),
});
export type AppRouter = typeof appRouter;
const user1 = await trpc.getUserById.query(1);
const createdUser = await trpc.createUser.mutate({
name: 'Jordane',
email: "jordane@devoxx.fr"
});
Awesome article by Alexis King
@JoGrenat
Awesome article by Alexis King
@JoGrenat