Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/validation/src/Review/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const proposalFapReviewCommentValidationSchema = () => {

export const proposalGradeValidationSchema = Yup.object().shape({
comment: proposalFapReviewCommentValidationSchema(),
grade: Yup.string()
.required(),
grade: Yup.string().required(),
});

export const proposalTechnicalReviewValidationSchema = Yup.object().shape({
Expand Down
38 changes: 0 additions & 38 deletions packages/validation/src/User/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,65 +139,27 @@ describe('User validation schemas', () => {
const validData = {
firstname: 'John',
lastname: 'Doe',
gender: 'male',
user_title: 'Mr',
email: 'john.doe@example.com',
password: 'Password123',
confirmPassword: 'Password123',
birthdate: new Date(1990, 1, 1),
institutionId: 1,
department: 'Research',
position: 'Researcher',
telephone: '+1234567890',
};
await expect(
UserValidation.createUserValidationSchema.validate(validData)
).resolves.toMatchObject(validData);
});

it('should fail when user is under 18', async () => {
const today = new Date();
const underageDate = new Date(
today.getFullYear() - 17,
today.getMonth(),
today.getDate()
);

const invalidData = {
firstname: 'John',
lastname: 'Doe',
gender: 'male',
user_title: 'Mr',
email: 'john.doe@example.com',
password: 'Password123',
confirmPassword: 'Password123',
birthdate: underageDate,
institutionId: 1,
department: 'Research',
position: 'Researcher',
telephone: '+1234567890',
};

await expect(
UserValidation.createUserValidationSchema.validate(invalidData)
).rejects.toThrow('You must be at least 18 years old');
});

it('should validate with optional preferredname', async () => {
const validData = {
firstname: 'John',
lastname: 'Doe',
preferredname: 'Johnny',
gender: 'male',
user_title: 'Mr',
email: 'john.doe@example.com',
password: 'Password123',
confirmPassword: 'Password123',
birthdate: new Date(1990, 1, 1),
institutionId: 1,
department: 'Research',
position: 'Researcher',
telephone: '+1234567890',
};

await expect(
Expand Down
85 changes: 0 additions & 85 deletions packages/validation/src/User/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const createUserValidationSchema = Yup.object().shape({
firstname: Yup.string().required().min(2).max(50),
preferredname: Yup.string().notRequired().max(50),
lastname: Yup.string().required().min(2).max(50),
gender: Yup.string().required(),
user_title: Yup.string().required(),
email: Yup.string().email().required(),
password: passwordValidationSchema,
Expand All @@ -41,110 +40,26 @@ export const createUserValidationSchema = Yup.object().shape({
),
})
.notRequired(),
birthdate: Yup.date()
.min(new Date(1900, 1, 1), 'You are not that old')
.test('DOB', 'You must be at least 18 years old', (value) => {
// to keep the current behavior after @types/yup upgrade:
// if value is `null` or `undefined` return true explicitly
// because new Date(null | undefined) evaluates to `Invalid date`
// which return NaN for getFullYear()
// and Number - NaN < 18 evaluates to false
if (!value) {
return true;
}

const dateOfBirth = new Date(value);
const dateNow = new Date();

if (dateNow.getFullYear() - dateOfBirth.getFullYear() < 18) {
return false;
} else {
return true;
}
})
.required('Please specify your birth date'),
institutionId: Yup.number().required(),
department: Yup.string().min(2).max(50).required(),
position: Yup.string().min(2).max(50).required(),
telephone: Yup.string()
.min(2)
.max(30)
.matches(phoneRegExp, 'telephone number is not valid')
.required(),
});

export const updateUserValidationSchema = Yup.object().shape({
firstname: Yup.string().min(2).max(50).required(),
preferredname: Yup.string().notRequired().max(50),
lastname: Yup.string().min(2).max(50).required(),
gender: Yup.string().required(),
user_title: Yup.string().required(),
email: Yup.string().email().required(),
birthdate: Yup.date()
.min(new Date(1900, 1, 1), 'You are not that old')
.test('DOB', 'You must be at least 18 years old', (value) => {
// to keep the current behavior after @types/yup upgrade:
// if value is `null` or `undefined` return true explicitly
// because new Date(null | undefined) evaluates to `Invalid date`
// which return NaN for getFullYear()
// and Number - NaN < 18 evaluates to false
if (!value) {
return true;
}

const dateOfBirth = new Date(value);
const dateNow = new Date();

if (dateNow.getFullYear() - dateOfBirth.getFullYear() < 18) {
return false;
} else {
return true;
}
})
.required('Please specify your birth date'),
institutionId: Yup.number().required(),
department: Yup.string().min(2).max(50).required(),
position: Yup.string().min(2).max(50).required(),
telephone: Yup.string()
.min(2)
.max(30)
.matches(phoneRegExp, 'telephone number is not valid')
.required(),
});

export const updateUserValidationBackendSchema = Yup.object().shape({
id: Yup.number().required(),
firstname: Yup.string().min(2).max(50).notRequired(),
preferredname: Yup.string().notRequired().max(50),
lastname: Yup.string().min(2).max(50).notRequired(),
gender: Yup.string().notRequired(),
user_title: Yup.string().notRequired(),
email: Yup.string().email().notRequired(),
birthdate: Yup.date()
.min(new Date(1900, 1, 1), 'You are not that old')
.test('DOB', 'You must be at least 18 years old', (value) => {
if (!value) {
return true;
}

const dateOfBirth = new Date(value);
const dateNow = new Date();

if (dateNow.getFullYear() - dateOfBirth.getFullYear() < 18) {
return false;
} else {
return true;
}
})
.notRequired(),
institutionId: Yup.number().notRequired(),
department: Yup.string().min(2).max(50).notRequired(),
position: Yup.string().min(2).max(50).notRequired(),
telephone: Yup.string()
.min(2)
.max(30)
.matches(phoneRegExp, 'telephone number is not valid')
.notRequired(),
});

export const updateUserRolesValidationSchema = Yup.object().shape({
Expand Down