aboutsummaryrefslogtreecommitdiff
path: root/scripts/apidocs/utils/value-checks.ts
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-09-10 00:56:58 +0200
committerGitHub <[email protected]>2024-09-09 22:56:58 +0000
commitacb8b5258fa645e499831fca43b319b0439c0baf (patch)
tree249085a072923558c7cacc9a276c16a11697f617 /scripts/apidocs/utils/value-checks.ts
parentf128d77194003192d27a3eb897f4a7ad02980ed0 (diff)
downloadfaker-acb8b5258fa645e499831fca43b319b0439c0baf.tar.xz
faker-acb8b5258fa645e499831fca43b319b0439c0baf.zip
infra: improve error messages for parameter properties (#3082)
Diffstat (limited to 'scripts/apidocs/utils/value-checks.ts')
-rw-r--r--scripts/apidocs/utils/value-checks.ts56
1 files changed, 40 insertions, 16 deletions
diff --git a/scripts/apidocs/utils/value-checks.ts b/scripts/apidocs/utils/value-checks.ts
index f8578ccc..2a4ee1a9 100644
--- a/scripts/apidocs/utils/value-checks.ts
+++ b/scripts/apidocs/utils/value-checks.ts
@@ -1,7 +1,11 @@
-export function exactlyOne<T>(input: ReadonlyArray<T>, property: string): T {
+export function exactlyOne<T>(
+ input: ReadonlyArray<T>,
+ property: string,
+ extraDescription: string = ''
+): T {
if (input.length !== 1) {
throw new Error(
- `Expected exactly one element for ${property}, got ${input.length}`
+ `Expected exactly one ${property} element, got ${input.length}. ${extraDescription}`
);
}
@@ -10,11 +14,12 @@ export function exactlyOne<T>(input: ReadonlyArray<T>, property: string): T {
export function optionalOne<T>(
input: ReadonlyArray<T>,
- property: string
+ property: string,
+ extraDescription: string = ''
): T | undefined {
if (input.length > 1) {
throw new Error(
- `Expected one optional element for ${property}, got ${input.length}`
+ `Expected one optional ${property} element, got ${input.length}. ${extraDescription}`
);
}
@@ -23,10 +28,13 @@ export function optionalOne<T>(
export function required<T>(
input: T | undefined,
- property: string
+ property: string,
+ extraDescription: string = ''
): NonNullable<T> {
if (input == null) {
- throw new Error(`Expected a value for ${property}, got undefined`);
+ throw new Error(
+ `Expected a value for ${property}, got undefined. ${extraDescription}`
+ );
}
return input;
@@ -34,17 +42,23 @@ export function required<T>(
export function allRequired<T>(
input: ReadonlyArray<T | undefined>,
- property: string
+ property: string,
+ extraDescription: string = ''
): Array<NonNullable<T>> {
- return input.map((v, i) => required(v, `${property}[${i}]`));
+ return input.map((v, i) =>
+ required(v, `${property}[${i}]`, extraDescription)
+ );
}
export function atLeastOne<T>(
input: ReadonlyArray<T>,
- property: string
+ property: string,
+ extraDescription: string = ''
): ReadonlyArray<T> {
if (input.length === 0) {
- throw new Error(`Expected at least one element for ${property}`);
+ throw new Error(
+ `Expected at least one ${property} element. ${extraDescription}`
+ );
}
return input;
@@ -52,18 +66,28 @@ export function atLeastOne<T>(
export function atLeastOneAndAllRequired<T>(
input: ReadonlyArray<T | undefined>,
- property: string
+ property: string,
+ extraDescription: string = ''
): ReadonlyArray<NonNullable<T>> {
- return atLeastOne(allRequired(input, property), property);
+ return atLeastOne(
+ allRequired(input, property, extraDescription),
+ property,
+ extraDescription
+ );
}
-export function valueForKey<T>(input: Record<string, T>, key: string): T {
- return required(input[key], key);
+export function valueForKey<T>(
+ input: Record<string, T>,
+ key: string,
+ extraDescription: string = ''
+): T {
+ return required(input[key], key, extraDescription);
}
export function valuesForKeys<T>(
input: Record<string, T>,
- keys: string[]
+ keys: string[],
+ extraDescription: string = ''
): T[] {
- return keys.map((key) => valueForKey(input, key));
+ return keys.map((key) => valueForKey(input, key, extraDescription));
}