Skip to content

safeParseAny

const safeParseAny: (yaml, source?) => SafeParseAnyResult

Defined in: packages/core/src/parser/yaml-parser.ts:2633

Convenience function to detect and validate any supported document type

Detect the document type of a YAML string and validate it against the matching schema

ParameterTypeDescription
yamlstringYAML string to parse (a map block, a scrollytelling block, or a root document)
source?ValueSource-

SafeParseAnyResult

Discriminated result with the detected block type and the corresponding safeParse result

Dispatches on the document’s top-level type: field:

Any other type: value produces a failure result listing the valid values, as does a document that has neither type: nor pages:. This method never throws.

A source says that yaml was synthesized from a value read from other text (TOML): positions then point into that text.

const { blockType, result } = YAMLParser.safeParseAny(yamlString);
if (!result.success) {
result.errors.forEach(err => console.error(`${err.path}: ${err.message}`));
} else if (blockType === 'map') {
renderMap(result.data);
}

yaml

YAML string to parse (map block, scrollytelling block, or root document)

Discriminated result with the detected block type and safeParse result

This is an alias for YAMLParser.safeParseAny for convenient imports.

import { safeParseAny } from '@maplibre-yaml/core/parser';
const { blockType, result } = safeParseAny(yamlString);