Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/schemas/easy-eda-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
22 changes: 22 additions & 0 deletions tests/szlcsc-schema-id.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
})
Loading