Skip to content

Commit

Permalink
fix(validator): getValidatorLocale Maximum call stack size exceeded (#…
Browse files Browse the repository at this point in the history
…2273)

* fix: getValidatorLocale Maximum call stack size exceeded

* test(validator): add getLocaleByPath getValidateLocale tests
  • Loading branch information
wakeGISer authored Oct 9, 2021
1 parent 9ed17eb commit 200253e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 17 additions & 1 deletion packages/validator/src/__tests__/registry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getValidateLocaleIOSCode } from '../'
import {
getValidateLocaleIOSCode,
getLocaleByPath,
getValidateLocale,
setValidateLanguage,
} from '../'
import locale from '../locale'

test('getValidateLocaleIOSCode', () => {
expect(getValidateLocaleIOSCode('zh-CN')).toEqual('zh-CN')
Expand All @@ -8,3 +14,13 @@ test('getValidateLocaleIOSCode', () => {
expect(getValidateLocaleIOSCode('en')).toEqual('en')
expect(getValidateLocaleIOSCode('TW')).toEqual('zh-TW')
})

test('getLocaleByPath', () => {
expect(getLocaleByPath('pattern', 'vi')).toEqual(locale.en.pattern)
expect(getLocaleByPath('pattern')).toEqual(locale.en.pattern)
})

test('getValidateLocale', () => {
setValidateLanguage('vi')
expect(getValidateLocale('pattern')).toEqual(locale.en.pattern)
})
13 changes: 8 additions & 5 deletions packages/validator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ export const setValidateLanguage = (lang: string) => {

export const getValidateLanguage = () => registry.locales.language

export const getLocaleByPath = (path: string, lang: string = registry.locales.language) => getIn(
registry.locales.messages,
`${getISOCode(lang)}.${path}`
)


export const getValidateLocale = (path: string) => {
const message = getIn(
registry.locales.messages,
`${getISOCode(registry.locales.language)}.${path}`
)
return message || getValidateLocale('pattern')
const message = getLocaleByPath(path);
return message || getLocaleByPath('pattern') || getLocaleByPath('pattern', defaultLanguage)
}

export const getValidateMessageTemplateEngine = () => registry.template
Expand Down

0 comments on commit 200253e

Please sign in to comment.