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-05-20 23:53:28 +00:00
|
|
|
const askCommunityValidationSchema = z.object({
|
|
|
|
|
title: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty',
|
|
|
|
|
}),
|
|
|
|
|
body: z.string().min(20, {
|
|
|
|
|
message: 'Must be 10 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty',
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
const commentInCommunityValidationSchema = z.object({
|
|
|
|
|
comment: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty',
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-03 22:45:01 +01:00
|
|
|
const forgotValidationSchema = z.object({
|
|
|
|
|
email: z.string().email({
|
|
|
|
|
message: 'Invalid email address',
|
2022-11-30 09:18:41 +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',
|
|
|
|
|
invalid_type_error: 'Must not be empty',
|
|
|
|
|
}),
|
|
|
|
|
password: z.string().min(6, {
|
|
|
|
|
message: 'Must be 6 or more characters long',
|
|
|
|
|
invalid_type_error: 'Must not be empty',
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
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',
|
|
|
|
|
invalid_type_error: 'Must not be empty',
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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' }),
|
|
|
|
|
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',
|
|
|
|
|
invalid_type_error: 'Select an option',
|
|
|
|
|
})
|
|
|
|
|
.min(1),
|
|
|
|
|
source: z
|
|
|
|
|
.string({
|
|
|
|
|
required_error: 'Select an option',
|
|
|
|
|
invalid_type_error: 'Select an option',
|
|
|
|
|
})
|
|
|
|
|
.min(1),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-30 09:18:41 +01:00
|
|
|
export const getConfirmPasswordError = ({ password, confirmPassword }) => {
|
|
|
|
|
return password > 6 && confirmPassword > 6 && password !== confirmPassword
|
|
|
|
|
? 'Does not match password'
|
|
|
|
|
: null;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-03 22:45:01 +01:00
|
|
|
const processErrors = (error) => {
|
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;
|
|
|
|
|
console.log(issue);
|
|
|
|
|
if (path.length) {
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const resetValidation = (fields = {}) => {
|
|
|
|
|
const { error } = resetValidationSchema.safeParse(fields);
|
|
|
|
|
|
|
|
|
|
return processErrors(error);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const forgotValidation = (fields = {}) => {
|
|
|
|
|
const { error } = forgotValidationSchema.safeParse(fields);
|
|
|
|
|
|
|
|
|
|
return processErrors(error);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const onboardingValidation = (fields = {}, step) => {
|
|
|
|
|
const schema =
|
|
|
|
|
step === 1
|
|
|
|
|
? onboardingValidationSchema.stepOne
|
|
|
|
|
: onboardingValidationSchema.stepTwo;
|
|
|
|
|
const { error } = schema.safeParse(fields);
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
emails: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
emails,
|
|
|
|
|
};
|
|
|
|
|
};
|