-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate property names in linking config
- Loading branch information
Showing
6 changed files
with
102 additions
and
0 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
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,32 @@ | ||
const formatToList = (items: string[]) => | ||
items.map((key) => `- ${key}`).join('\n'); | ||
|
||
export default function validatePathConfig(config: any, root = true) { | ||
const validKeys = ['initialRouteName', 'screens']; | ||
|
||
if (!root) { | ||
validKeys.push('path', 'exact', 'stringify', 'parse'); | ||
} | ||
|
||
const invalidKeys = Object.keys(config).filter( | ||
(key) => !validKeys.includes(key) | ||
); | ||
|
||
if (invalidKeys.length) { | ||
throw new Error( | ||
`Found invalid properties in the configuration:\n${formatToList( | ||
invalidKeys | ||
)}\n\nDid you forget to specify them under a 'screens' property?\n\nYou can only specify the following properties:\n${formatToList( | ||
validKeys | ||
)}\n\nSee https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.` | ||
); | ||
} | ||
|
||
if (config.screens) { | ||
Object.entries(config.screens).forEach(([_, value]) => { | ||
if (typeof value !== 'string') { | ||
validatePathConfig(value, false); | ||
} | ||
}); | ||
} | ||
} |
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