( content: string, filename: string, schema: z.ZodType<T>, filetype: string | undefined = undefined )
| 58 | * @returns Validated and typed config object |
| 59 | */ |
| 60 | export function parseConfig<T>( |
| 61 | content: string, |
| 62 | filename: string, |
| 63 | schema: z.ZodType<T>, |
| 64 | filetype: string | undefined = undefined |
| 65 | ): T { |
| 66 | const raw = parseRawConfig(content, filename, filetype); |
| 67 | |
| 68 | const result = schema.safeParse(raw); |
| 69 | if (!result.success) { |
| 70 | const issues = result.error.issues |
| 71 | .map(issue => { |
| 72 | const path = issue.path.length > 0 ? issue.path.join('.') : '(root)'; |
| 73 | return ` - ${path}: ${issue.message}`; |
| 74 | }) |
| 75 | .join('\n'); |
| 76 | throw new PythonAnalysisError({ |
| 77 | message: `Invalid config in "${filename}":\n${issues}`, |
| 78 | code: 'PYTHON_CONFIG_VALIDATION_ERROR', |
| 79 | path: filename, |
| 80 | fileContent: content, |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | return result.data; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Read a config file if it exists and validate it against a zod schema. |
no test coverage detected