diff --git a/lib/schemas/easy-eda-json-schema.ts b/lib/schemas/easy-eda-json-schema.ts index 5c516640..ad0d554c 100644 --- a/lib/schemas/easy-eda-json-schema.ts +++ b/lib/schemas/easy-eda-json-schema.ts @@ -11,7 +11,9 @@ export const maybeNumber = z .pipe(z.number().nullable().optional()) export const SzlcscSchema = z.object({ - id: z.number(), + // EasyEDA returns this id as a number for some parts and a string for others + // (e.g. C356849); the value is unused, so accept either to avoid import crashes. + id: z.union([z.number(), z.string()]), number: z.string(), step: z.number().optional(), min: z.number().optional(), @@ -22,7 +24,7 @@ export const SzlcscSchema = z.object({ }) export const LcscSchema = z.object({ - id: z.number(), + id: z.union([z.number(), z.string()]), number: z.string(), step: z.number().optional(), min: z.number().optional(), diff --git a/tests/szlcsc-schema-id.test.ts b/tests/szlcsc-schema-id.test.ts new file mode 100644 index 00000000..ac0fee45 --- /dev/null +++ b/tests/szlcsc-schema-id.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, test } from "bun:test" +import { SzlcscSchema, LcscSchema } from "lib/schemas/easy-eda-json-schema" + +// EasyEDA returns the szlcsc/lcsc `id` as a number for some parts and a string +// for others (e.g. C356849), which used to crash `tsci import` on those parts. +describe("szlcsc/lcsc id accepts number or string", () => { + for (const [name, schema] of [ + ["SzlcscSchema", SzlcscSchema], + ["LcscSchema", LcscSchema], + ] as const) { + test(`${name} accepts a numeric id`, () => { + expect(schema.safeParse({ id: 2384679, number: "C356849" }).success).toBe( + true, + ) + }) + test(`${name} accepts a string id`, () => { + expect( + schema.safeParse({ id: "2384679", number: "C356849" }).success, + ).toBe(true) + }) + } +})