Skip to content

Parsing & validating in your own code

Most projects never parse the YAML themselves: <ml-map>, the CLI and the Astro components all use the library’s parser. If your code reads a map document itself, for example a custom loader or an editor that reads and writes the file, parse it the way the library does. Otherwise your code and the map can read the same file differently.

There are two things to use.

The library parses map documents with two options the plain yaml package doesn’t turn on: YAML merge keys (<<: *base) and the !html tag. Both change what a document means. Import the options rather than writing your own copy:

import { parse } from "yaml";
import { YAML_PARSE_OPTIONS } from "@maplibre-yaml/core";
const value = parse(source, YAML_PARSE_OPTIONS);

A hand-written copy is how !html silently turns into a plain string, or a merge key stops merging.

YAMLParser parses and validates in one step, and returns the same errors and warnings the CLI and <ml-map> report:

import { YAMLParser } from "@maplibre-yaml/core";
const result = YAMLParser.safeParseMapBlock(source);
if (!result.success) {
for (const error of result.errors) {
console.error(`${error.path}: ${error.message} (line ${error.line})`);
}
}
  • result.errors and result.warnings are lists of { path, message, line, column }, with an optional suggestion (the key you probably meant). path points into the document with dots (config.center); line and column are there when the source was text (YAML or JSON here, or any format through parseDocumentText). These are the fields of the CLI’s JSON output (Using AI agents).
  • result.data is the validated document when result.success is true. It keeps the format it was written in, v1 or v2; normalizeMapBlock(result.data) turns either into the one model the renderer and the exporter read.
MethodValidates
safeParseMapBlock(yaml)A map document
safeParseScrollytellingBlock(yaml)A story file
safeParseAny(yaml)Either; blockType says which it was
safeParseMapBlockValue(value)A map document you already hold as an object

Every method and type is in the TypeScript API reference, and rendering the result with MapRenderer is on the JavaScript API page.

The YAMLParser methods read YAML, and JSON, which is part of YAML 1.2. For a document that may be YAML, JSON or TOML, use parseDocumentText. It is what <ml-map>, the CLI, the Astro loaders and the playground call, so it reads the format the way they do and words each error for it:

import { parseDocumentText } from "@maplibre-yaml/core";
const result = await parseDocumentText(text, {
kind: "map", // "map", "story", or "any" (the default)
signals: { fileName: "maps/city.toml" },
});
if (!result.success) {
for (const error of result.errors) {
console.error(`${error.format} ${error.path}: ${error.message} (line ${error.line})`);
}
}
  • The format comes from format ("yaml", "json" or "toml") when you pass it; else from signals: scriptType, url, fileName, responseUrl and contentType, in that order; else from the text itself (How the format is chosen).
  • The result is what the matching YAMLParser method returns, plus format. With kind: "any" it is { blockType, result, format }, as safeParseAny returns.
  • Every error carries format, the format the text was read as. A syntax error has code: "yaml-syntax" in YAML and in JSON (JSON is read by the YAML parser), and code: "toml-syntax" in TOML, with its line and column in every format.
  • It is async because TOML is read by a module that loads the first time TOML arrives; YAML and JSON never load it. The promise never rejects because of what the text says: a syntax error or an invalid document resolves as an error result. It rejects only when that TOML module can’t be downloaded.

convertDocument is what mlym convert runs. It writes the document as authored in the target format and lists what the target can’t keep:

import { convertDocument } from "@maplibre-yaml/core";
const out = await convertDocument(yamlText, { to: "toml" });
if (out.success) {
for (const loss of out.losses) console.warn(loss.message); // comments, anchors, nulls
console.log(out.text);
} else {
console.error(out.errors); // a syntax error, or a value TOML can't hold, by path
}

from (or signals) names the input’s format; without it the text decides. out.validation is the input’s parseDocumentText result. A document that parses converts even when it doesn’t validate, so check out.validation.result.success before you save the output, as mlym convert does.

A value you parsed yourself: normalizeDocumentValue

Section titled “A value you parsed yourself: normalizeDocumentValue”

When a document reaches your code as a value (a JSON or TOML library parsed it, or Astro’s content layer did) and you check it with your own schema, pass it through normalizeDocumentValue first. It returns { value, issues, warnings }: a copy with dates as text, the root $schema key removed and nested locale tables flattened into dotted keys, which is what every format goes through before validation. issues are problems that fail the document, such as a locale key written both nested and dotted. The YAMLParser methods that take a value (safeParseMapBlockValue) do this themselves.

YAML aliases can be nested so that a small document expands into an enormous one (the “billion laughs” attack). The library keeps the yaml package’s alias-expansion limit on, so such a document is rejected with a parse error instead of being expanded:

YAML could not be expanded: Excessive alias count indicates a resource exhaustion attack

It is an ordinary validation error, not a crash, so a collaborative editor or a validation endpoint can report it like any other malformed input. Parse through YAMLParser (above) to get the same guard.