-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Warn for invalid namespace keys (#106)
- Loading branch information
Showing
6 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import AbstractIntlMessages from './AbstractIntlMessages'; | ||
import IntlError, {IntlErrorCode} from './IntlError'; | ||
|
||
function validateMessagesSegment( | ||
messages: AbstractIntlMessages, | ||
invalidKeyLabels: Array<string>, | ||
parentPath?: string | ||
) { | ||
Object.entries(messages).forEach(([key, messageOrMessages]) => { | ||
if (key.includes('.')) { | ||
let keyLabel = key; | ||
if (parentPath) keyLabel += ` (at ${parentPath})`; | ||
invalidKeyLabels.push(keyLabel); | ||
} | ||
if (typeof messageOrMessages === 'object') { | ||
validateMessagesSegment( | ||
messageOrMessages, | ||
invalidKeyLabels, | ||
[parentPath, key].filter((part) => part != null).join('.') | ||
); | ||
} | ||
}); | ||
} | ||
|
||
export default function validateMessages( | ||
messages: AbstractIntlMessages, | ||
onError: (error: IntlError) => void | ||
) { | ||
const invalidKeyLabels: Array<string> = []; | ||
validateMessagesSegment(messages, invalidKeyLabels); | ||
|
||
if (invalidKeyLabels.length > 0) { | ||
onError( | ||
new IntlError( | ||
IntlErrorCode.INVALID_KEY, | ||
`Namespace keys can not contain the character "." as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid ${ | ||
invalidKeyLabels.length === 1 ? 'key' : 'keys' | ||
}: ${invalidKeyLabels.join(', ')}` | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e86ab28
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-intl – ./
next-intl-git-main-amann.vercel.app
next-intl-amann.vercel.app
next-intl-docs.vercel.app