* Validates that the YAML document doesn't contain prohibited features: * - No anchors/aliases (&anchor, *alias) * - No merge keys (<<) * - No custom tags (!tag)
(yamlContent: string)
| 55 | * - No custom tags (!tag) |
| 56 | */ |
| 57 | function validateYamlStructure(yamlContent: string): void { |
| 58 | // Check for anchors - must appear as a value after : or - (not in URLs or other content) |
| 59 | // Matches patterns like "key: &anchor" or "- &anchor" |
| 60 | if (/(?:^|\n)\s*(?:-\s+|[\w-]+:\s*)&\w+/.test(yamlContent)) { |
| 61 | throw new ProhibitedYamlFeatureError('YAML anchors (&) are not allowed in Deepnote files', { feature: 'anchor' }) |
| 62 | } |
| 63 | |
| 64 | // Check for aliases - must appear as a value after : or - (not in Markdown like *bold*) |
| 65 | // Matches patterns like "key: *alias" or "- *alias" |
| 66 | if (/(?:^|\n)\s*(?:-\s+|[\w-]+:\s*)\*\w+/.test(yamlContent)) { |
| 67 | throw new ProhibitedYamlFeatureError('YAML aliases (*) are not allowed in Deepnote files', { feature: 'alias' }) |
| 68 | } |
| 69 | |
| 70 | // Check for merge keys |
| 71 | if (/<<:/.test(yamlContent)) { |
| 72 | throw new ProhibitedYamlFeatureError('YAML merge keys (<<) are not allowed in Deepnote files', { |
| 73 | feature: 'merge-key', |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | // Check for YAML tags (must appear after : or - at start of value, not inside strings) |
| 78 | // Tags look like: "key: !tag value" or "- !tag value" |
| 79 | // Tags can contain word chars, hyphens, and slashes: !custom-type, !python/object |
| 80 | // We need to avoid false positives from ! inside quoted strings or as part of operators like !== or !event |
| 81 | const tagPattern = /(?:^|\n)\s*(?:-\s+|[\w-]+:\s*)(![\w/-]+)/gm |
| 82 | const matches = yamlContent.match(tagPattern) |
| 83 | if (matches) { |
| 84 | const tags = matches.map(m => m.match(/(![\w/-]+)/)?.[1]).filter(Boolean) |
| 85 | if (tags.length > 0) { |
| 86 | throw new ProhibitedYamlFeatureError(`YAML tags are not allowed in Deepnote files: ${tags.join(', ')}`, { |
| 87 | feature: 'tag', |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Parse and validate YAML document in a single pass. |