|
| 1 | +/** |
| 2 | + * Public `validate()` entry point — universal dispatcher. |
| 3 | + * |
| 4 | + * Accepts a markup string, JSON IR (MCNode), or AST, and dispatches to the |
| 5 | + * appropriate underlying walker: |
| 6 | + * |
| 7 | + * - `string` → tokenize + parse, then run the AST validator. |
| 8 | + * Parse errors are returned as structured `errors`, not thrown. |
| 9 | + * - `ASTNode` → AST validator (identified by the presence of `loc`). |
| 10 | + * - `MCNode` → JSON-IR validator (different walker — it does extra checks |
| 11 | + * the AST walker does not, e.g. `id` uniqueness). |
| 12 | + * |
| 13 | + * Anything else (number, null, MCDocument, etc.) returns a structured |
| 14 | + * `INVALID_INPUT` error rather than throwing — public APIs must never crash |
| 15 | + * the caller on a bad input type. |
| 16 | + * |
| 17 | + * @module validate |
| 18 | + */ |
| 19 | +import type { ASTNode, ValidationResult } from './types.js'; |
| 20 | +import type { MCNode } from './json/schema.js'; |
| 21 | +import { tokenize } from './tokenizer/index.js'; |
| 22 | +import { parse } from './parser/index.js'; |
| 23 | +import { validate as validateAST } from './validator/index.js'; |
| 24 | +import { validateJSON as validateMCNode } from './json/validator.js'; |
| 25 | +import { MCError } from './errors/mc-error.js'; |
| 26 | +import { ErrorCode } from './errors/codes.js'; |
| 27 | + |
| 28 | +/** Accepted shapes for `validate()`. */ |
| 29 | +export type ValidateInput = string | ASTNode | MCNode; |
| 30 | + |
| 31 | +/** |
| 32 | + * Validates markup, JSON IR, or AST and returns all errors/warnings. |
| 33 | + * |
| 34 | + * Replaces the older AST-only `validate(ast)` (still used internally) with a |
| 35 | + * universal entry point that mirrors `compile()`'s input flexibility. |
| 36 | + * |
| 37 | + * @param input - Markup string, JSON IR (`MCNode`), or parsed `ASTNode`. |
| 38 | + * @returns A `ValidationResult` with `isValid`, `errors`, and `warnings`. |
| 39 | + */ |
| 40 | +export function validate(input: ValidateInput): ValidationResult { |
| 41 | + // ── (1) Markup string → tokenize + parse + AST walk ───────────────── |
| 42 | + if (typeof input === 'string') { |
| 43 | + try { |
| 44 | + const tokens = tokenize(input); |
| 45 | + const ast = parse(tokens); |
| 46 | + return validateAST(ast); |
| 47 | + } catch (e) { |
| 48 | + // Parse/tokenize errors arrive as MCError. Anything else is unexpected |
| 49 | + // (defensive — should not happen with valid string input). Either way, |
| 50 | + // surface as a structured validation error instead of throwing. |
| 51 | + if (e instanceof MCError) { |
| 52 | + return { |
| 53 | + isValid: false, |
| 54 | + errors: [{ |
| 55 | + code: e.code, |
| 56 | + message: e.message, |
| 57 | + severity: 'error', |
| 58 | + ...(e.loc ? { loc: { line: e.loc.start.line, col: e.loc.start.col } } : {}), |
| 59 | + ...(e.fix ? { fix: e.fix } : {}), |
| 60 | + }], |
| 61 | + warnings: [], |
| 62 | + }; |
| 63 | + } |
| 64 | + return { |
| 65 | + isValid: false, |
| 66 | + errors: [{ |
| 67 | + code: ErrorCode.INVALID_INPUT, |
| 68 | + message: e instanceof Error ? e.message : String(e), |
| 69 | + severity: 'error', |
| 70 | + }], |
| 71 | + warnings: [], |
| 72 | + }; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // ── (2) Reject non-object inputs cleanly ───────────────────────────── |
| 77 | + if (input === null || typeof input !== 'object') { |
| 78 | + return { |
| 79 | + isValid: false, |
| 80 | + errors: [{ |
| 81 | + code: ErrorCode.INVALID_INPUT, |
| 82 | + message: |
| 83 | + `validate() expects a markup string, JSON IR (MCNode), or ASTNode. Received: ${input === null ? 'null' : typeof input}.`, |
| 84 | + severity: 'error', |
| 85 | + }], |
| 86 | + warnings: [], |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + // ── (3) Reject objects that lack `type` — likely an MCDocument or junk ─ |
| 91 | + // Catching this here keeps the JSON walker simple and gives users |
| 92 | + // a clear pointer rather than a cryptic crash inside fuzzyMatch. |
| 93 | + const obj = input as { type?: unknown; template?: unknown }; |
| 94 | + if (typeof obj.type !== 'string' || obj.type === '') { |
| 95 | + const looksLikeDocument = |
| 96 | + 'template' in obj && typeof obj.template === 'object' && obj.template !== null; |
| 97 | + const hint = looksLikeDocument |
| 98 | + ? ' This looks like an MCDocument — use validateDocument(doc) instead.' |
| 99 | + : ''; |
| 100 | + return { |
| 101 | + isValid: false, |
| 102 | + errors: [{ |
| 103 | + code: ErrorCode.INVALID_INPUT, |
| 104 | + message: `validate() input is missing a "type" field.${hint}`, |
| 105 | + severity: 'error', |
| 106 | + }], |
| 107 | + warnings: [], |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + // ── (4) Discriminate AST vs JSON IR by presence of `loc` ───────────── |
| 112 | + // AST nodes always carry a SourceLocation; JSON IR nodes never do. |
| 113 | + if ('loc' in obj) { |
| 114 | + return validateAST(input as ASTNode); |
| 115 | + } |
| 116 | + return validateMCNode(input as MCNode); |
| 117 | +} |
0 commit comments