From 9347c92566e06e45ef1d17479600c8dc1aed08f0 Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Thu, 25 Jan 2024 10:32:27 +0100 Subject: [PATCH] fix: make stringify more robust --- packages/api/test/utils/checkAgainstSpec.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/api/test/utils/checkAgainstSpec.ts b/packages/api/test/utils/checkAgainstSpec.ts index 461e59f70015..fd93bd611b36 100644 --- a/packages/api/test/utils/checkAgainstSpec.ts +++ b/packages/api/test/utils/checkAgainstSpec.ts @@ -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): Record { 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;