* Parse and validate JSON file with a Zod schema
(
filePath: string,
schema: {
safeParse: (data: unknown) => {
success: boolean
data?: T
error?: {
issues: Array<{ path: PropertyKey[]; message: string }>
}
}
},
)
| 1370 | * Parse and validate JSON file with a Zod schema |
| 1371 | */ |
| 1372 | async function parseFileWithSchema<T>( |
| 1373 | filePath: string, |
| 1374 | schema: { |
| 1375 | safeParse: (data: unknown) => { |
| 1376 | success: boolean |
| 1377 | data?: T |
| 1378 | error?: { |
| 1379 | issues: Array<{ path: PropertyKey[]; message: string }> |
| 1380 | } |
| 1381 | } |
| 1382 | }, |
| 1383 | ): Promise<T> { |
| 1384 | const fs = getFsImplementation() |
| 1385 | const content = await fs.readFile(filePath, { encoding: 'utf-8' }) |
| 1386 | let data: unknown |
| 1387 | try { |
| 1388 | data = jsonParse(content) |
| 1389 | } catch (error) { |
| 1390 | throw new ConfigParseError( |
| 1391 | `Invalid JSON in ${filePath}: ${errorMessage(error)}`, |
| 1392 | filePath, |
| 1393 | content, |
| 1394 | ) |
| 1395 | } |
| 1396 | const result = schema.safeParse(data) |
| 1397 | if (!result.success) { |
| 1398 | throw new ConfigParseError( |
| 1399 | `Invalid schema: ${filePath} ${result.error?.issues.map(e => `${e.path.join('.')}: ${e.message}`).join(', ')}`, |
| 1400 | filePath, |
| 1401 | data, |
| 1402 | ) |
| 1403 | } |
| 1404 | return result.data! |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * Load and cache a marketplace from its source |
no test coverage detected