Skip to content

Commit

Permalink
fix: make stringify more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluard committed Jan 25, 2024
1 parent 1b42805 commit 9347c92
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/api/test/utils/checkAgainstSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,24 @@ function prettyAjvErrors(errors: ErrorObject[] | null | undefined): string {
return errors.map((e) => `${e.instancePath ?? "."} - ${e.message}`).join("\n");
}

type StringifiedProperty = string | StringifiedProperty[] | null | undefined;

function stringifyProperty(value: unknown): StringifiedProperty {
if (value === undefined || value === null || typeof value === "string") {
// Handle specifically null as `typeof null === "object"`
return value;
} else if (typeof value === "number") {
return value.toString(10);
} else if (Array.isArray(value)) {
return value.map(stringifyProperty);
}
return String(value);
}

function stringifyProperties(obj: Record<string, unknown>): Record<string, unknown> {
for (const key of Object.keys(obj)) {
const value = obj[key];
if (typeof value === "number") {
obj[key] = value.toString(10);
} else if (Array.isArray(value)) {
obj[key] = value.map((value) => value.toString(10));
}
obj[key] = stringifyProperty(value);
}

return obj;
Expand Down

0 comments on commit 9347c92

Please sign in to comment.