Skip to content

safeParseAny

const safeParseAny: (yaml) => SafeParseAnyResult

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

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)

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.

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);