Skip to content

Commit

Permalink
fix(users): allow saving users with date_of_birth as empty string (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff authored Aug 24, 2020
1 parent c26896b commit 668b68a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ User.beforeValidate(async (user) => {
if (typeof user.phone === 'string') user.phone = user.phone.trim();
if (typeof user.address === 'string') user.address = user.address.trim();
if (typeof user.about_me === 'string') user.about_me = user.about_me.trim();

if (typeof user.date_of_birth === 'string' && user.date_of_birth === '') user.date_of_birth = null;
});

async function encryptPassword(user) {
Expand Down
7 changes: 6 additions & 1 deletion test/unit/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ describe('Users testing', () => {
}
});

test('should allow empty date_of_birth', async () => {
test('should allow null date_of_birth', async () => {
const user = generator.generateUser();
delete user.date_of_birth;

await User.create(user);
});

test('should allow empty string date_of_birth', async () => {
const user = generator.generateUser({ date_of_birth: '' });
await User.create(user);
});

test('should fail with date_of_birth in the future', async () => {
try {
const user = generator.generateUser({
Expand Down

0 comments on commit 668b68a

Please sign in to comment.