2023-12-10 17:12:20 +01:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
2024-05-18 16:55:08 +02:00
|
|
|
import { createCustomErrorParams } from "./form/i18n";
|
|
|
|
|
|
2023-12-10 17:12:20 +01:00
|
|
|
const usernameSchema = z.string().min(3).max(255);
|
|
|
|
|
const passwordSchema = z.string().min(8).max(255);
|
|
|
|
|
|
2024-05-18 16:55:08 +02:00
|
|
|
const confirmPasswordRefine = [
|
2024-05-19 22:38:39 +02:00
|
|
|
(data: { password: string; confirmPassword: string }) => data.password === data.confirmPassword,
|
2024-05-18 16:55:08 +02:00
|
|
|
{
|
|
|
|
|
path: ["confirmPassword"],
|
|
|
|
|
params: createCustomErrorParams("passwordsDoNotMatch"),
|
|
|
|
|
},
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
] satisfies [(args: any) => boolean, unknown];
|
|
|
|
|
|
2024-03-02 17:46:03 +01:00
|
|
|
const createUserSchema = z
|
2023-12-10 17:12:20 +01:00
|
|
|
.object({
|
|
|
|
|
username: usernameSchema,
|
|
|
|
|
password: passwordSchema,
|
2023-12-19 23:09:41 +01:00
|
|
|
confirmPassword: z.string(),
|
2024-05-18 16:55:08 +02:00
|
|
|
email: z.string().email().or(z.string().length(0).optional()),
|
2023-12-10 17:12:20 +01:00
|
|
|
})
|
2024-05-18 16:55:08 +02:00
|
|
|
.refine(confirmPasswordRefine[0], confirmPasswordRefine[1]);
|
2023-12-10 17:12:20 +01:00
|
|
|
|
2024-03-02 17:46:03 +01:00
|
|
|
const initUserSchema = createUserSchema;
|
|
|
|
|
|
2024-01-02 14:18:37 +01:00
|
|
|
const signInSchema = z.object({
|
2024-05-18 16:55:08 +02:00
|
|
|
name: z.string().min(1),
|
|
|
|
|
password: z.string().min(1),
|
2023-12-10 17:12:20 +01:00
|
|
|
});
|
2024-01-02 14:18:37 +01:00
|
|
|
|
2024-05-12 10:04:20 +02:00
|
|
|
const registrationSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
username: usernameSchema,
|
|
|
|
|
password: passwordSchema,
|
|
|
|
|
confirmPassword: z.string(),
|
|
|
|
|
})
|
2024-05-18 16:55:08 +02:00
|
|
|
.refine(confirmPasswordRefine[0], confirmPasswordRefine[1]);
|
2024-05-12 10:04:20 +02:00
|
|
|
|
|
|
|
|
const registrationSchemaApi = registrationSchema.and(
|
|
|
|
|
z.object({
|
|
|
|
|
inviteId: z.string(),
|
|
|
|
|
token: z.string(),
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2024-03-05 21:10:19 +01:00
|
|
|
const editProfileSchema = z.object({
|
2024-05-12 16:27:56 +02:00
|
|
|
id: z.string(),
|
2024-03-05 21:10:19 +01:00
|
|
|
name: usernameSchema,
|
|
|
|
|
email: z
|
|
|
|
|
.string()
|
|
|
|
|
.email()
|
|
|
|
|
.or(z.literal(""))
|
|
|
|
|
.transform((value) => (value === "" ? null : value))
|
|
|
|
|
.optional()
|
|
|
|
|
.nullable(),
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-12 16:27:56 +02:00
|
|
|
const changePasswordSchema = z
|
|
|
|
|
.object({
|
2024-05-18 16:55:08 +02:00
|
|
|
previousPassword: z.string().min(1),
|
2024-05-12 16:27:56 +02:00
|
|
|
password: passwordSchema,
|
|
|
|
|
confirmPassword: z.string(),
|
|
|
|
|
})
|
2024-05-18 16:55:08 +02:00
|
|
|
.refine(confirmPasswordRefine[0], confirmPasswordRefine[1]);
|
2024-05-12 16:27:56 +02:00
|
|
|
|
2024-05-19 22:38:39 +02:00
|
|
|
const changePasswordApiSchema = changePasswordSchema.and(z.object({ userId: z.string() }));
|
2024-03-06 21:20:41 +01:00
|
|
|
|
2024-01-02 14:18:37 +01:00
|
|
|
export const userSchemas = {
|
|
|
|
|
signIn: signInSchema,
|
2024-05-12 10:04:20 +02:00
|
|
|
registration: registrationSchema,
|
|
|
|
|
registrationApi: registrationSchemaApi,
|
2024-01-02 14:18:37 +01:00
|
|
|
init: initUserSchema,
|
2024-03-02 17:46:03 +01:00
|
|
|
create: createUserSchema,
|
2024-02-27 21:53:53 +01:00
|
|
|
password: passwordSchema,
|
2024-03-05 21:10:19 +01:00
|
|
|
editProfile: editProfileSchema,
|
2024-03-06 21:20:41 +01:00
|
|
|
changePassword: changePasswordSchema,
|
2024-05-12 16:27:56 +02:00
|
|
|
changePasswordApi: changePasswordApiSchema,
|
2024-01-02 14:18:37 +01:00
|
|
|
};
|