diff --git a/nodejs/__test__/arrow.test.ts b/nodejs/__test__/arrow.test.ts index 2735f7f72..9e20e3c04 100644 --- a/nodejs/__test__/arrow.test.ts +++ b/nodejs/__test__/arrow.test.ts @@ -991,6 +991,37 @@ describe.each([arrow15, arrow16, arrow17, arrow18])( expectValidMapField(roundTripped.schema.fields[0]); }); + + it("preserves string schema metadata", function () { + const metadata = new Map([["source", "fixture"]]); + const schema = new Schema( + [new Field("value", new Int32(), true)], + metadata, + ); + + expect(makeEmptyTable(schema).schema.metadata.get("source")).toBe( + "fixture", + ); + }); + + it.each([ + ["non-string keys", new Map([[42, "fixture"]])], + ["non-string values", new Map([["source", 42]])], + [ + "non-string keys and values", + new Map([[42, false]]), + ], + ])("rejects schema metadata with %s", function (_, metadataLike) { + const metadata = metadataLike as unknown as Map; + const schema = new Schema( + [new Field("value", new Int32(), true)], + metadata, + ); + + expect(() => makeEmptyTable(schema)).toThrow( + "Expected metadata, if present, to be a Map but it had non-string keys or values", + ); + }); }); describe("when using two versions of arrow", function () { diff --git a/nodejs/lancedb/sanitize.ts b/nodejs/lancedb/sanitize.ts index 9f834a7f6..ae0bc0179 100644 --- a/nodejs/lancedb/sanitize.ts +++ b/nodejs/lancedb/sanitize.ts @@ -84,7 +84,7 @@ export function sanitizeMetadata( throw Error("Expected metadata, if present, to be a Map"); } for (const item of metadataLike) { - if (!(typeof item[0] === "string" || !(typeof item[1] === "string"))) { + if (typeof item[0] !== "string" || typeof item[1] !== "string") { throw Error( "Expected metadata, if present, to be a Map but it had non-string keys or values", );