Files
classroomio/apps/dashboard/src/lib/utils/functions/validator.js
T

334 lines
9.5 KiB
JavaScript
Raw Normal View History

2023-06-17 19:11:29 +00:00
import isNumber from 'lodash/isNumber';
2022-11-30 09:18:41 +01:00
import z from 'zod';
2023-05-02 23:55:26 +00:00
import { validateEmail } from './validateEmail';
import { t } from '$lib/utils/functions/translations';
2022-11-30 09:18:41 +01:00
const getOrgNameValidation = () =>
z
.string()
.min(5, { message: `${t.get('validations.organization_name.min_char')}` })
.refine((val) => !/^[-]|[-]$/.test(val), {
message: `${t.get('validations.organization_name.hyphen_rule')}`
});
const getSiteNameValidation = () =>
z
.string()
.min(5, { message: `${t.get('validations.site_name.min_char')}` })
.refine((val) => !/^[-]|[-]$/.test(val), {
message: `${t.get('validations.site_name.hyphen_rule')}`
});
const getNewsfeedValidation = () =>
z.string().min(5, { message: `${t.get('validations.news_feed.min_char')}` });
2024-05-20 17:26:43 +01:00
const lessonSchema = z.object({
title: z.string().nonempty({ message: `${t.get('validations.lesson_schema.empty_title')}` }),
2024-05-20 17:26:43 +01:00
lesson_at: z.string().optional(),
call_url: z.string().nullable().optional(),
is_unlocked: z.boolean()
});
2023-06-17 19:11:29 +00:00
const createQuizValidationSchema = z.object({
title: z.string().min(6, {
message: 'Must be 6 or more characters long',
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
})
2023-06-17 19:11:29 +00:00
});
2023-05-20 23:53:28 +00:00
const askCommunityValidationSchema = z.object({
title: z.string().min(6, {
message: `${t.get('validations.ask_community.title.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-05-20 23:53:28 +00:00
}),
body: z.string().min(20, {
message: `${t.get('validations.ask_community.body.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
}),
courseId: z.string().min(36, {
message: `${t.get('validations.ask_community.course_id.select_course')}`,
invalid_type_error: `${t.get('validations.ask_community.course_id.select_course')}`
2023-08-16 04:24:24 +01:00
})
2023-05-20 23:53:28 +00:00
});
const commentInCommunityValidationSchema = z.object({
comment: z.string().min(6, {
message: `${t.get('validations.comment_in_community.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
})
});
const orgLandingpageValidationSchema = z.object({
name: z.string().min(6, {
message: `${t.get('validations.org_landing_page.name.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-05-20 23:53:28 +00:00
}),
2023-08-16 04:24:24 +01:00
email: z.string().email({
message: `${t.get('validations.org_landing_page.email.invalid_email')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
}),
phone: z.string().min(6, {
message: `${t.get('validations.org_landing_page.phone.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
}),
message: z.string().min(20, {
message: `${t.get('validations.org_landing_page.message.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
})
2023-05-20 23:53:28 +00:00
});
2022-12-03 22:45:01 +01:00
const forgotValidationSchema = z.object({
email: z.string().email({
message: `${t.get('validations.forgot.invalid_email')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
})
2022-12-03 22:45:01 +01:00
});
const authValidationSchema = z.object({
2022-11-30 09:18:41 +01:00
email: z.string().email({
message: `${t.get('validations.auth.email.invalid_email')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2022-11-30 09:18:41 +01:00
}),
password: z.string().min(6, {
message: `${t.get('validations.auth.password.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
})
2022-11-30 09:18:41 +01:00
});
2022-12-03 22:45:01 +01:00
const resetValidationSchema = z.object({
password: z.string().min(6, {
message: `${t.get('validations.reset.password.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-08-16 04:24:24 +01:00
})
2022-12-03 22:45:01 +01:00
});
const onboardingValidationSchema = {
stepOne: z.object({
fullname: z
.string()
.min(5, { message: `${t.get('validations.onboarding.step_one.full_name.min_char')}` }),
orgName: getOrgNameValidation(),
siteName: getSiteNameValidation()
2022-12-03 22:45:01 +01:00
}),
stepTwo: z.object({
goal: z
.string({
required_error: `${t.get('validations.onboarding.step_two.goal.required')}`,
invalid_type_error: `${t.get('validations.onboarding.step_two.goal.required')}`
2022-12-03 22:45:01 +01:00
})
.min(1),
source: z
.string({
required_error: `${t.get('validations.onboarding.step_two.source.required')}`,
invalid_type_error: `${t.get('validations.onboarding.step_two.source.required')}`
2022-12-03 22:45:01 +01:00
})
2023-08-16 04:24:24 +01:00
.min(1)
})
2022-12-03 22:45:01 +01:00
};
2022-11-30 09:18:41 +01:00
export const getConfirmPasswordError = ({ password, confirmPassword }) => {
return password > 6 && confirmPassword > 6 && password !== confirmPassword
? `${t.get('validations.confirm_password.not_match')}`
: undefined;
2022-11-30 09:18:41 +01:00
};
2023-07-31 17:20:04 +00:00
export const processErrors = (error, mapToId) => {
2022-11-30 09:18:41 +01:00
const errors = {};
if (Array.isArray(error?.issues)) {
for (const issue of error.issues) {
const { message, path } = issue;
2023-06-17 19:11:29 +00:00
if (!path.length) {
continue;
}
if (mapToId) {
let value = '';
path.forEach((p, i) => {
const isLast = !path[i + 1];
const formatP = isNumber(p) ? `[${p}]` : p;
value += !value ? formatP : `.${formatP}`;
});
const key = value.replace('label', 'id');
errors[key] = value;
} else {
2022-11-30 09:18:41 +01:00
errors[path[0]] = message;
}
}
}
return errors;
};
2022-12-03 22:45:01 +01:00
export const authValidation = (fields = {}) => {
const { error } = authValidationSchema.safeParse(fields);
2024-05-20 17:26:43 +01:00
return processErrors(error);
};
2022-12-03 22:45:01 +01:00
2024-05-20 17:26:43 +01:00
export const lessonValidation = (lesson = {}) => {
const { error } = lessonSchema.safeParse(lesson);
2022-12-03 22:45:01 +01:00
return processErrors(error);
};
2023-09-15 08:26:26 +01:00
export const coursePaymentValidation = (fields = {}) => {
const schema = z.object({
fullname: z.string().min(6, {
message: `${t.get('validations.course_payment.full_name.min_char')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-09-15 08:26:26 +01:00
}),
email: z.string().email({
message: `${t.get('validations.course_payment.email.invalid_email')}`,
invalid_type_error: `${t.get('validations.invalid_type_error')}`
2023-09-15 08:26:26 +01:00
})
});
const { error } = schema.safeParse(fields);
return processErrors(error);
};
2022-12-03 22:45:01 +01:00
export const resetValidation = (fields = {}) => {
const { error } = resetValidationSchema.safeParse(fields);
return processErrors(error);
};
export const forgotValidation = (fields = {}) => {
const { error } = forgotValidationSchema.safeParse(fields);
return processErrors(error);
};
2023-08-16 04:24:24 +01:00
export const orgLandingpageValidation = (fields = {}) => {
const { error } = orgLandingpageValidationSchema.safeParse(fields);
return processErrors(error);
};
2022-12-03 22:45:01 +01:00
export const onboardingValidation = (fields = {}, step) => {
const schema =
2023-08-16 04:24:24 +01:00
step === 1 ? onboardingValidationSchema.stepOne : onboardingValidationSchema.stepTwo;
2022-12-03 22:45:01 +01:00
const { error } = schema.safeParse(fields);
2023-06-17 19:11:29 +00:00
return processErrors(error);
};
2023-10-04 08:18:33 +01:00
// export const createTemplateExerciseValidation = (fields = {}) => {
// const schema = z.object({
// orgName: z.string().min(5, {
// message: 'Must be 5 or more characters long',
// }),
// siteName: z.string().min(5, {
// message: 'Must be 5 or more characters long',
// })
// });
// const { error } = schema.safeParse(fields);
// return processErrors(error);
// };
export const createNewsfeedValidation = (newPost) => {
const schema = z.object({
newPost: getNewsfeedValidation()
});
const { error } = schema.safeParse({ newPost });
return processErrors(error);
};
export const addNewsfeedCommentValidation = (newComment) => {
const schema = z.object({
newComment: getNewsfeedValidation()
});
const { error } = schema.safeParse({ newComment });
return processErrors(error);
};
2023-09-11 11:47:30 +01:00
export const createOrgValidation = (fields = {}) => {
const schema = z.object({
orgName: getOrgNameValidation(),
siteName: getSiteNameValidation()
2023-09-11 11:47:30 +01:00
});
const { error } = schema.safeParse(fields);
return processErrors(error);
};
export const updateOrgNameValidation = (orgName) => {
const schema = z.object({
orgName: getOrgNameValidation()
});
const { error } = schema.safeParse({ orgName });
return processErrors(error);
};
export const updateOrgSiteNameValidation = (siteName) => {
const schema = z.object({
siteName: getSiteNameValidation()
});
const { error } = schema.safeParse({ siteName });
return processErrors(error);
};
export const updateProfileEmailValidation = (email) => {
const schema = z.object({
email: z.string().email({ message: `${t.get('validations.user_profile.email')}` })
});
const { error } = schema.safeParse({ email });
return processErrors(error);
};
2023-06-17 19:11:29 +00:00
export const createQuizValidation = (fields = {}) => {
const { error } = createQuizValidationSchema.safeParse(fields);
2022-12-03 22:45:01 +01:00
return processErrors(error);
};
2023-05-20 23:53:28 +00:00
export const askCommunityValidation = (fields = {}) => {
const { error } = askCommunityValidationSchema.safeParse(fields);
return processErrors(error);
};
export const commentInCommunityValidation = (fields = {}) => {
const { error } = commentInCommunityValidationSchema.safeParse(fields);
return processErrors(error);
};
2022-11-30 09:18:41 +01:00
export const getDisableSubmit = ({ password, confirmPassword }) => {
return !!(password && confirmPassword && password !== confirmPassword);
};
2023-05-02 23:55:26 +00:00
export const validateEmailInString = (emailsStr) => {
let error = '';
let hasError = false;
if (!emailsStr) {
return {
hasError: true,
error: 'Enter an email',
2023-08-16 04:24:24 +01:00
emails: []
2023-05-02 23:55:26 +00:00
};
}
const emails = emailsStr.split(',').map((_email) => {
const email = _email.trim();
if (!validateEmail(email)) {
hasError = true;
error += !error.length ? `${t.get('validations.email.not_valid')}: ${email}` : `, ${email}`;
2023-05-02 23:55:26 +00:00
}
return email;
});
return {
hasError,
error,
2023-08-16 04:24:24 +01:00
emails
2023-05-02 23:55:26 +00:00
};
};