gluestick/src/schemas.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-04-26 21:21:28 -04:00
import * as Yup from "yup";
const REQUIRED = "Required.";
const USERNAME = Yup.string()
.required(REQUIRED)
.min(1, "Username is too short.");
const PASSWORD = Yup.string()
.required(REQUIRED)
.min(12, "Password must be at least 12 characters long.");
export const loginSchema = Yup.object().shape({
username: USERNAME,
password: PASSWORD
});
export type LoginFormValues = {
username: string;
password: string;
};
export const registerSchema: Yup.Schema<RegisterFormValues> =
Yup.object().shape({
username: USERNAME,
displayName: Yup.string()
.required(REQUIRED)
.min(1, "Display name is too short."),
email: Yup.string().required(REQUIRED).email("Not an email."),
confirmPassword: Yup.string()
.required(REQUIRED)
.oneOf([Yup.ref("password", {})], "Passwords must match."),
password: PASSWORD,
avatar: Yup.mixed<File>()
.test("fileSize", "File is larger than 1 MB.", (value) => {
if (value == null) return true;
return value.size <= 1_000_000;
})
.optional()
});
export interface RegisterFormValues {
username: string;
displayName: string;
email: string;
password: string;
confirmPassword: string;
avatar?: File;
}