(path: string | undefined, options: ValidateOptions)
| 65 | } |
| 66 | |
| 67 | async function validateDeepnoteFile(path: string | undefined, options: ValidateOptions): Promise<void> { |
| 68 | const { absolutePath } = await resolvePathToDeepnoteFile(path) |
| 69 | |
| 70 | debug('Reading file contents...') |
| 71 | const rawBytes = await fs.readFile(absolutePath) |
| 72 | const yamlContent = decodeUtf8NoBom(rawBytes) |
| 73 | |
| 74 | debug('Parsing YAML...') |
| 75 | let parsed: unknown |
| 76 | try { |
| 77 | parsed = parseYaml(yamlContent) |
| 78 | } catch (error) { |
| 79 | const message = error instanceof Error ? error.message : String(error) |
| 80 | if (options.output === 'json') { |
| 81 | outputJson({ |
| 82 | success: true, |
| 83 | path: absolutePath, |
| 84 | valid: false, |
| 85 | issues: [{ path: '', message: `Invalid YAML: ${message}`, code: 'yaml_parse_error' }], |
| 86 | } satisfies ValidationResult) |
| 87 | } else { |
| 88 | logError(`Invalid YAML: ${message}`) |
| 89 | } |
| 90 | throw new ValidationHandledError(ExitCode.InvalidUsage) |
| 91 | } |
| 92 | |
| 93 | debug('Validating against schema...') |
| 94 | const result = deepnoteFileSchema.safeParse(parsed) |
| 95 | |
| 96 | if (result.success) { |
| 97 | if (options.output === 'json') { |
| 98 | outputJson({ |
| 99 | success: true, |
| 100 | path: absolutePath, |
| 101 | valid: true, |
| 102 | issues: [], |
| 103 | } satisfies ValidationResult) |
| 104 | } else { |
| 105 | const chalk = getChalk() |
| 106 | output(`${chalk.green('✓')} ${absolutePath} is valid`) |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | // Validation failed - format the errors |
| 112 | const issues = formatZodErrors(result.error) |
| 113 | |
| 114 | if (options.output === 'json') { |
| 115 | outputJson({ |
| 116 | success: true, |
| 117 | path: absolutePath, |
| 118 | valid: false, |
| 119 | issues, |
| 120 | } satisfies ValidationResult) |
| 121 | } else { |
| 122 | const chalk = getChalk() |
| 123 | output(`${chalk.red('✗')} ${absolutePath} is invalid`) |
| 124 | output('') |
no test coverage detected