From 5ea8252f727e2e577c2adca9650ac8f24a171632 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Tue, 14 Jun 2022 14:18:50 +0200 Subject: feat: throw error on unknown locale (#1071) --- src/faker.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/faker.ts b/src/faker.ts index d6a288c6..c84d77a4 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -45,8 +45,34 @@ const metadataKeys: ReadonlyArray = [ export class Faker { locales: UsedLocales; - locale: UsableLocale; - localeFallback: UsableLocale; + private _locale: UsableLocale; + private _localeFallback: UsableLocale; + + get locale(): UsableLocale { + return this._locale; + } + + set locale(locale: UsableLocale) { + if (!this.locales[locale]) { + throw new FakerError( + `Locale ${locale} is not supported. You might want to add the requested locale first to \`faker.locales\`.` + ); + } + this._locale = locale; + } + + get localeFallback(): UsableLocale { + return this._localeFallback; + } + + set localeFallback(localeFallback: UsableLocale) { + if (!this.locales[localeFallback]) { + throw new FakerError( + `Locale ${localeFallback} is not supported. You might want to add the requested locale first to \`faker.locales\`.` + ); + } + this._localeFallback = localeFallback; + } readonly definitions: LocaleDefinition = this.initDefinitions(); -- cgit v1.2.3