I'm fairly new to Node.js and I am having some issues. I received error in sonarqube as define a constant instead of duplicating 3 times for "Invalid Password". how can i resolved this issue.
export const MessageCodeMapping = {
REQUEST_COMPLETED_SUCCESSFULLY: {
code: "1",
description: "REQUEST_COMPLETED_SUCCESSFULLY",
detailedDescription: "REQUEST_COMPLETED_SUCCESSFULLY",
},
INVALID_CREATE_PASSWORD: {
code: "4",
description: "Invalid Password",
detailedDescription: `Invalid password.
Minimum of 8 characters and a maximum of 16.
Must have at least three of the following: uppercase letter, lowercase letter, number (0-9) and/or special character/symbol.
Password is case-sensitive.`,
},
INVALID_PSWRD: {
code: "2",
description: "Invalid Password",
detailedDescription: `Minimum of 8 and max of 16 characters - Requires 3 out of the following 4 options:
1. Lowercase characters
2. Uppercase characters
3. Numbers
4. Special characters.
Password is case sensitive.`,
},
INVALID_PASSWORD: {
code: "3",
description: "Invalid Password",
detailedDescription: `Minimum of 8 and max of 16 characters - Requires 3 out of the following 4 options:
1. Lowercase characters
2. Uppercase characters
3. Numbers
4. Special characters.
Password is case sensitive.`,
},
CodePudding user response:
Exactly what it says on the tin: declare, say, const invalidPassword = "Invalid Password"
, and use that instead of the string. More resistant to typos, for starters. Easier to change, should you want to change the short decription in the future.
Also, even if SonarQube doesn't object, I'd declare a constant for the repeated - long - detailedDescription
for codes "2"
and "3"
.
CodePudding user response:
To solve this issue, stop declaring many times similar messages. What I recommend is to declare once and use in all places needed or if you must have some slight difference between them create a function that receives a message and replace with the message key with what received.
It will be something like this:
const MessageCodeMapping = {
REQUEST_COMPLETED_SUCCESSFULLY: {
code: "1",
description: "REQUEST_COMPLETED_SUCCESSFULLY",
detailedDescription: "REQUEST_COMPLETED_SUCCESSFULLY",
},
INVALID_PASSWORD: (desc) => ({
code: "2",
description: "Invalid Password",
detailedDescription: desc || `Minimum of 8 and max of 16 characters - Requires 3 out of the following 4 options:
1. Lowercase characters
2. Uppercase characters
3. Numbers
4. Special characters.
Password is case sensitive.`,
}),
}
// Usage
// Returns what is hardcoded
MessageCodeMapping.INVALID_PASSWORD()
// Returns the object with the detailedDescription changed
MessageCodeMapping.INVALID_PASSWORD("Custom Message")