Skip to content

Commit

Permalink
fix: allow blank website to clear existing value
Browse files Browse the repository at this point in the history
  • Loading branch information
viet nguyen committed Oct 18, 2023
1 parent 9a08eec commit bc2351a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/model/UserDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class UserDataSource extends MongoDataSource<User> {
}

/**
* Update user profile. Create a new user object if not defined.
* Update user profile. Create a new user object if not defined. Optional fields (displayName, bio, website, avatar) can take blank ("") and null. Blank to clear the field; null to skip the update.
* @param updater UUID of the account doing the update
* @param input profile params
* @returns true if successful
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class UserDataSource extends MongoDataSource<User> {
throw new Error('Nothing to update. Must provide at least one field.')
}

if (website != null && !isValidUrl(website)) {
if (website !== '' && website != null && !isValidUrl(website)) {
throw new Error('Invalid website address.')
}

Expand Down Expand Up @@ -266,7 +266,11 @@ const isValidUsername = (username?: string): boolean => {
)
}

/**
* Validate non-empty and non-null url
*/
const isValidUrl = (url: string): boolean => {
if (url == null) return false
try {
const newUrl = new URL(url)
return newUrl.protocol === 'http:' || newUrl.protocol === 'https:'
Expand Down
16 changes: 15 additions & 1 deletion src/model/__tests__/UserDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('UserDataSource', () => {

await users.createOrUpdateUserProfile(updater, input)

const u2 = await users.getUserPublicProfile(username)
let u2 = await users.getUserPublicProfile(username)

// check selected fields
expect(u2).toMatchObject({
Expand All @@ -83,6 +83,20 @@ describe('UserDataSource', () => {
})

expect(u2?._id.toUUID().toString()).toEqual(input.userUuid)

// should allow website as an empty string to clear existing value
await users.createOrUpdateUserProfile(updater, { userUuid: input.userUuid, website: '' })

u2 = await users.getUserPublicProfile(username)

// verify
expect(u2).toMatchObject({
username: input.username,
displayName: input.displayName,
bio: input.bio,
website: '',
email: input.email
})
})

it('should require an email when creating new profile', async () => {
Expand Down

0 comments on commit bc2351a

Please sign in to comment.