Skip to content

Commit

Permalink
Added invalid nickname error for conversations (#615)
Browse files Browse the repository at this point in the history
* Added string's length checks to the Contracts class

* Add the usage of the string's length checks for the ConversationImpl nickname

* Changed minimum nickname length from 1 to 2

* Fixed the throwIfNullOrTooShort method of Contracts

* Update Conversation.ts

---------

Co-authored-by: Japou Pou <[email protected]>
  • Loading branch information
Jxpxn and r-ard authored Feb 22, 2023
1 parent 82b4eb8 commit 5076d93
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/sdk/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ export class Contracts {
}
}

public static throwIfNullOrTooLong(param: string, name: string, maxLength: number): void {
Contracts.throwIfNullOrUndefined(param, name);

if (("" + param).length > maxLength) {
throw new Error("throwIfNullOrTooLong:" + name + " (more than " + maxLength.toString() + " characters)");
}
}


public static throwIfNullOrTooShort(param: string, name: string, minLength: number): void {
Contracts.throwIfNullOrUndefined(param, name);

if (("" + param).length < minLength) {
throw new Error("throwIfNullOrTooShort:" + name + " (less than " + minLength.toString() + " characters)");
}
}

public static throwIfDisposed(isDisposed: boolean): void {
if (isDisposed) {
throw new Error("the object is already disposed");
Expand Down
4 changes: 3 additions & 1 deletion src/sdk/Transcription/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ export class ConversationImpl extends Conversation implements IDisposable {
// TODO: specify the regex required. Nicknames must be unique or get the duplicate nickname error
// TODO: check what the max length is and if a truncation is required or if the service handles it without an error
let hostNickname: string = speechConfig.getProperty(PropertyId[PropertyId.ConversationTranslator_Name]);
if (hostNickname === undefined || hostNickname === null || hostNickname.length <= 1 || hostNickname.length > 50) {
if (hostNickname === undefined || hostNickname === null) {
hostNickname = "Host";
}
Contracts.throwIfNullOrTooLong(hostNickname, "nickname", 50);
Contracts.throwIfNullOrTooShort(hostNickname, "nickname", 2);
speechConfig.setProperty(PropertyId[PropertyId.ConversationTranslator_Name], hostNickname);

} else {
Expand Down

0 comments on commit 5076d93

Please sign in to comment.