Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/internal/assert-locale-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FakerError } from '../errors/faker-error';

/**
* Checks that the value is not null or undefined and throws an error if it is.
*
* @param value The value to check.
* @param path The path to the locale data.
*
* @returns The value if it's not null or undefined.
*
* @throws {FakerError} If the value is null or undefined.
*/
export function assertLocaleData<T>(
value: T,
...path: string[]
): NonNullable<T> {
if (value === null) {
throw new FakerError(
`The locale data for '${path.join('.')}' aren't applicable to this locale.
If you think this is a bug, please report it at: https://github.com/faker-js/faker`
);
} else if (value === undefined) {
throw new FakerError(
`The locale data for '${path.join('.')}' are missing in this locale.
If this is a custom Faker instance, please make sure all required locales are used e.g. '[de_AT, de, en, base]'.
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
For more information see https://fakerjs.dev/guide/localization.html`
);
}

return value;
}
29 changes: 2 additions & 27 deletions src/internal/locale-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { LocaleDefinition } from '../definitions';
import { FakerError } from '../errors/faker-error';
import { assertLocaleData } from './assert-locale-data';

/**
* A proxy for LocaleDefinition that marks all properties as required and throws an error when an entry is accessed that is not defined.
Expand Down Expand Up @@ -54,31 +55,6 @@ export function createLocaleProxy(locale: LocaleDefinition): LocaleProxy {
}) as LocaleProxy;
}

/**
* Checks that the value is not null or undefined and throws an error if it is.
*
* @param value The value to check.
* @param path The path to the locale data.
*/
export function assertLocaleData<T>(
value: T,
...path: string[]
): asserts value is NonNullable<T> {
if (value === null) {
throw new FakerError(
`The locale data for '${path.join('.')}' aren't applicable to this locale.
If you think this is a bug, please report it at: https://github.com/faker-js/faker`
);
} else if (value === undefined) {
throw new FakerError(
`The locale data for '${path.join('.')}' are missing in this locale.
If this is a custom Faker instance, please make sure all required locales are used e.g. '[de_AT, de, en, base]'.
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
For more information see https://fakerjs.dev/guide/localization.html`
);
}
}

/**
* Creates a proxy for a category that throws an error when accessing an undefined property.
*
Expand Down Expand Up @@ -106,8 +82,7 @@ function createCategoryProxy<
return value;
}

assertLocaleData(value, categoryName, entryName.toString());
return value;
return assertLocaleData(value, categoryName, entryName.toString());
},

set: throwReadOnlyError,
Expand Down
16 changes: 13 additions & 3 deletions src/modules/airline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* responsible for setting standards relating to many aspects of airline
* operations.
*/
import { assertLocaleData } from '../../internal/assert-locale-data';
import { ModuleBase } from '../../internal/module-base';

export enum Aircraft {
Expand Down Expand Up @@ -88,7 +89,10 @@ export class AirlineModule extends ModuleBase {
*/
airport(): Airport {
return this.faker.helpers.arrayElement(
this.faker.definitions.airline.airport
assertLocaleData(
this.faker.fakerCore.locale.airline?.airport,
'airline.airport'
)
);
Comment thread
ST-DDT marked this conversation as resolved.
}

Expand All @@ -102,7 +106,10 @@ export class AirlineModule extends ModuleBase {
*/
airline(): Airline {
return this.faker.helpers.arrayElement(
this.faker.definitions.airline.airline
assertLocaleData(
this.faker.fakerCore.locale.airline?.airline,
'airline.airline'
)
);
}

Expand All @@ -116,7 +123,10 @@ export class AirlineModule extends ModuleBase {
*/
airplane(): Airplane {
return this.faker.helpers.arrayElement(
this.faker.definitions.airline.airplane
assertLocaleData(
this.faker.fakerCore.locale.airline?.airplane,
'airline.airplane'
)
);
}

Expand Down
77 changes: 61 additions & 16 deletions src/modules/animal/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertLocaleData } from '../../internal/assert-locale-data';
import { ModuleBase } from '../../internal/module-base';

/**
Expand All @@ -21,7 +22,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
dog(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.dog);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.dog, 'animal.dog')
);
}

/**
Expand All @@ -33,7 +36,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
cat(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.cat);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.cat, 'animal.cat')
);
}

/**
Expand All @@ -45,7 +50,12 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
snake(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.snake);
return this.faker.helpers.arrayElement(
assertLocaleData(
this.faker.fakerCore.locale.animal?.snake,
'animal.snake'
)
);
}

/**
Expand All @@ -57,7 +67,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
bear(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.bear);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.bear, 'animal.bear')
);
}

/**
Expand All @@ -69,7 +81,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
lion(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.lion);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.lion, 'animal.lion')
);
}

/**
Expand All @@ -82,7 +96,10 @@ export class AnimalModule extends ModuleBase {
*/
cetacean(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.cetacean
assertLocaleData(
this.faker.fakerCore.locale.animal?.cetacean,
'animal.cetacean'
)
);
}

Expand All @@ -95,7 +112,12 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
horse(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.horse);
return this.faker.helpers.arrayElement(
assertLocaleData(
this.faker.fakerCore.locale.animal?.horse,
'animal.horse'
)
);
}

/**
Expand All @@ -107,7 +129,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
bird(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.bird);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.bird, 'animal.bird')
);
}

/**
Expand All @@ -119,7 +143,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
cow(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.cow);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.cow, 'animal.cow')
);
}

/**
Expand All @@ -131,7 +157,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
fish(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.fish);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.fish, 'animal.fish')
);
}

/**
Expand All @@ -144,7 +172,10 @@ export class AnimalModule extends ModuleBase {
*/
crocodilia(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.crocodilia
assertLocaleData(
this.faker.fakerCore.locale.animal?.crocodilia,
'animal.crocodilia'
)
);
}

Expand All @@ -158,7 +189,10 @@ export class AnimalModule extends ModuleBase {
*/
insect(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.insect
assertLocaleData(
this.faker.fakerCore.locale.animal?.insect,
'animal.insect'
)
);
}

Expand All @@ -172,7 +206,10 @@ export class AnimalModule extends ModuleBase {
*/
rabbit(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.rabbit
assertLocaleData(
this.faker.fakerCore.locale.animal?.rabbit,
'animal.rabbit'
)
);
}

Expand All @@ -186,7 +223,10 @@ export class AnimalModule extends ModuleBase {
*/
rodent(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.rodent
assertLocaleData(
this.faker.fakerCore.locale.animal?.rodent,
'animal.rodent'
)
);
}

Expand All @@ -199,7 +239,9 @@ export class AnimalModule extends ModuleBase {
* @since 5.5.0
*/
type(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.type);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.animal?.type, 'animal.type')
);
}

/**
Expand All @@ -212,7 +254,10 @@ export class AnimalModule extends ModuleBase {
*/
petName(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.pet_name
assertLocaleData(
this.faker.fakerCore.locale.animal?.pet_name,
'animal.pet_name'
)
);
}
}
26 changes: 20 additions & 6 deletions src/modules/book/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertLocaleData } from '../../internal/assert-locale-data';
import { ModuleBase } from '../../internal/module-base';

/**
Expand Down Expand Up @@ -26,7 +27,9 @@ export class BookModule extends ModuleBase {
* @since 9.1.0
*/
author(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.book.author);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.book?.author, 'book.author')
);
}

/**
Expand All @@ -38,7 +41,9 @@ export class BookModule extends ModuleBase {
* @since 9.1.0
*/
format(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.book.format);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.book?.format, 'book.format')
);
}

/**
Expand All @@ -50,7 +55,9 @@ export class BookModule extends ModuleBase {
* @since 9.1.0
*/
genre(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.book.genre);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.book?.genre, 'book.genre')
);
}

/**
Expand All @@ -63,7 +70,10 @@ export class BookModule extends ModuleBase {
*/
publisher(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.book.publisher
assertLocaleData(
this.faker.fakerCore.locale.book?.publisher,
'book.publisher'
)
);
}

Expand All @@ -76,7 +86,9 @@ export class BookModule extends ModuleBase {
* @since 9.1.0
*/
series(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.book.series);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.book?.series, 'book.series')
);
}

/**
Expand All @@ -88,6 +100,8 @@ export class BookModule extends ModuleBase {
* @since 9.1.0
*/
title(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.book.title);
return this.faker.helpers.arrayElement(
assertLocaleData(this.faker.fakerCore.locale.book?.title, 'book.title')
);
}
}
Loading
Loading