-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(console,ui): fix locale guard issue in settings page
- Loading branch information
1 parent
9851619
commit e200578
Showing
5 changed files
with
27 additions
and
10 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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { z } from 'zod'; | ||
|
||
import { fallback } from './utilities/zod'; | ||
|
||
export const languageKeys = ['en', 'fr', 'pt-PT', 'zh-CN', 'tr-TR', 'ko-KR'] as const; | ||
export const languageKeyGuard = z.enum(languageKeys); | ||
export type LanguageKey = z.infer<typeof languageKeyGuard>; | ||
|
||
export const getDefaultLanguage = (language: string): LanguageKey => { | ||
return languageKeyGuard.or(fallback<LanguageKey>('en')).parse(language); | ||
}; |
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,15 @@ | ||
import { any } from 'zod'; | ||
|
||
/** | ||
* https://github.com/colinhacks/zod/issues/316#issuecomment-850906479 | ||
* Create a schema matches anything and returns a value. Use it with `or`: | ||
* | ||
* const schema = zod.number(); | ||
* const tolerant = schema.or(fallback(-1)); | ||
* | ||
* schema.parse('foo') // => ZodError | ||
* tolerant.parse('foo') // -1 | ||
*/ | ||
export function fallback<T>(value: T) { | ||
return any().transform(() => value); | ||
} |
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