Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow blank website to clear existing value #357

Merged
merged 1 commit into from
Oct 18, 2023
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
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