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';
|
2022-11-30 09:18:41 +01:00
|
|
|
|
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',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
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: 'Must be 6 or more characters long',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
2023-05-20 23:53:28 +00:00
|
|
|
}),
|
|
|
|
|
body: z.string().min(20, {
|
|
|
|
|
message: 'Must be 10 or more characters long',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
2023-05-20 23:53:28 +00:00
|
|
|
});
|
|
|
|
|
const commentInCommunityValidationSchema = z.object({
|
|
|
|
|
comment: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const orgLandingpageValidationSchema = z.object({
|
|
|
|
|
name: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
2023-05-20 23:53:28 +00:00
|
|
|
}),
|
2023-08-16 04:24:24 +01:00
|
|
|
email: z.string().email({
|
|
|
|
|
message: 'Invalid email address',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
}),
|
|
|
|
|
phone: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
}),
|
|
|
|
|
message: z.string().min(20, {
|
|
|
|
|
message: 'Must be 10 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
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: 'Invalid email address',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
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: 'Invalid email address',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
2022-11-30 09:18:41 +01:00
|
|
|
}),
|
|
|
|
|
password: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
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: 'Must be 6 or more characters long',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
2022-12-03 22:45:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const onboardingValidationSchema = {
|
|
|
|
|
stepOne: z.object({
|
|
|
|
|
fullname: z.string().min(1, { message: 'Enter your fullname' }),
|
2023-01-03 09:26:08 +01:00
|
|
|
orgName: z.string().min(1, { message: 'Enter your organisation name' }),
|
2023-08-16 04:24:24 +01:00
|
|
|
siteName: z.string().min(1, { message: 'Enter your site name' })
|
2022-12-03 22:45:01 +01:00
|
|
|
}),
|
|
|
|
|
stepTwo: z.object({
|
|
|
|
|
goal: z
|
|
|
|
|
.string({
|
|
|
|
|
required_error: 'Select an option',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Select an option'
|
2022-12-03 22:45:01 +01:00
|
|
|
})
|
|
|
|
|
.min(1),
|
|
|
|
|
source: z
|
|
|
|
|
.string({
|
|
|
|
|
required_error: 'Select an option',
|
2023-08-16 04:24:24 +01:00
|
|
|
invalid_type_error: 'Select an option'
|
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
|
|
|
|
|
? 'Does not match password'
|
2023-09-01 15:28:47 +01:00
|
|
|
: 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);
|
|
|
|
|
|
|
|
|
|
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: 'Must be 6 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
}),
|
|
|
|
|
email: z.string().email({
|
|
|
|
|
message: 'Invalid email address',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
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-09-11 11:47:30 +01:00
|
|
|
export const createOrgValidation = (fields = {}) => {
|
|
|
|
|
const schema = z.object({
|
|
|
|
|
orgName: z.string().min(5, {
|
|
|
|
|
message: 'Must be 5 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
}),
|
|
|
|
|
siteName: z.string().min(5, {
|
|
|
|
|
message: 'Must be 5 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty'
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
const { error } = schema.safeParse(fields);
|
|
|
|
|
|
|
|
|
|
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 ? `Not valid email: ${email}` : `, ${email}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return email;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
hasError,
|
|
|
|
|
error,
|
2023-08-16 04:24:24 +01:00
|
|
|
emails
|
2023-05-02 23:55:26 +00:00
|
|
|
};
|
|
|
|
|
};
|