Skip to content

YAMLParser

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

YAML parser with schema validation and reference resolution

This class provides static methods for parsing YAML configuration files, validating them against schemas, and resolving references between configuration sections. All methods are static as the parser maintains no internal state.

Parse and validate YAML (throws on error)

const config = YAMLParser.parse(yamlString);

Safe parse (returns result object)

const result = YAMLParser.safeParse(yamlString);
if (!result.success) {
console.error('Validation errors:', result.errors);
}

new YAMLParser(): YAMLParser

YAMLParser

static parse(yaml): any

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

Parse YAML string and validate against schema

ParameterTypeDescription
yamlstringYAML string to parse

any

Validated configuration object

If YAML syntax is invalid

If validation fails

This method parses the YAML, validates it against the RootSchema, resolves all references, and returns the validated config. If any step fails, it throws an error.

try {
const config = YAMLParser.parse(yamlString);
console.log('Valid config:', config);
} catch (error) {
console.error('Parse error:', error.message);
}

static parseMapBlock(yaml): object

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

Parse YAML string for a single map block and validate against MapBlockSchema

ParameterTypeDescription
yamlstringYAML string to parse (should be a map block, not a full document)

object

Validated map block object

If YAML syntax is invalid

If validation fails

This method is specifically for parsing individual map blocks (e.g., in documentation or component usage). Unlike parse, it validates against MapBlockSchema rather than RootSchema, so it expects a single map configuration without the pages array wrapper.

const yaml = `
type: map
id: example
config:
center: [0, 0]
zoom: 2
mapStyle: "https://example.com/style.json"
layers:
- id: points
type: circle
source:
type: geojson
data: { type: "FeatureCollection", features: [] }
`;
const mapBlock = YAMLParser.parseMapBlock(yaml);

static parseScrollytellingBlock(yaml): object

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

Parse YAML string for a scrollytelling block and validate against ScrollytellingBlockSchema

ParameterTypeDescription
yamlstringYAML string to parse (should be a scrollytelling block, not a full document)

object

Validated scrollytelling block object

If YAML syntax is invalid

If validation fails

This method is specifically for parsing individual scrollytelling blocks (e.g., in documentation or component usage). Unlike parse, it validates against ScrollytellingBlockSchema rather than RootSchema, so it expects a single scrollytelling configuration without the pages array wrapper.

const yaml = `
type: scrollytelling
id: story
config:
center: [0, 0]
zoom: 2
mapStyle: "https://example.com/style.json"
chapters:
- id: intro
title: "Introduction"
center: [0, 0]
zoom: 3
description: "Welcome to our story."
- id: chapter2
title: "Chapter 2"
center: [10, 10]
zoom: 5
description: "The story continues."
`;
const scrollyBlock = YAMLParser.parseScrollytellingBlock(yaml);

static resolveReferences(config): any

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

Resolve $ref references to global layers and sources

ParameterTypeDescription
configanyConfiguration object with potential references

any

Configuration with all references resolved

If a reference cannot be resolved

References use JSON Pointer-like syntax: #/layers/layerName or #/sources/sourceName. This method walks the configuration tree, finds all objects with a $ref property, looks up the referenced item in config.layers or config.sources, and replaces the reference object with the actual item.

  • #/layers/myLayer - Reference to a layer in the global layers section
  • #/sources/mySource - Reference to a source in the global sources section
const config = {
layers: {
myLayer: { id: 'layer1', type: 'circle', ... }
},
pages: [{
blocks: [{
type: 'map',
layers: [{ $ref: '#/layers/myLayer' }]
}]
}]
};
const resolved = YAMLParser.resolveReferences(config);
// resolved.pages[0].blocks[0].layers[0] now contains the full layer object

static safeParse(yaml): ParseResult

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

Parse YAML string and validate, returning a result object

ParameterTypeDescription
yamlstringYAML string to parse

ParseResult

Result object with success flag and either data or errors

This is the non-throwing version of parse. Instead of throwing errors, it returns a result object that indicates success or failure. Use this when you want to handle errors gracefully without try/catch.

const result = YAMLParser.safeParse(yamlString);
if (result.success) {
console.log('Config:', result.data);
} else {
result.errors.forEach(err => {
console.error(`Error at ${err.path}: ${err.message}`);
});
}

static safeParseAny(yaml): SafeParseAnyResult

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

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

static safeParseMapBlock(yaml): ParseResult<{[key: string]: any; }>

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

Parse YAML string for a single map block, returning a result object

ParameterTypeDescription
yamlstringYAML string to parse (should be a map block, not a full document)

ParseResult<{[key: string]: any; }>

Result object with success flag and either data or errors

This is the non-throwing version of parseMapBlock. Instead of throwing errors, it returns a result object that indicates success or failure. Use this when you want to handle errors gracefully without try/catch.

const result = YAMLParser.safeParseMapBlock(yamlString);
if (result.success) {
console.log('Map config:', result.data);
} else {
result.errors.forEach(err => {
console.error(`Error at ${err.path}: ${err.message}`);
});
}

static safeParseMapBlockValue(value): ParseResult<{[key: string]: any; }>

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

Validate an already-parsed map block — a JSON attribute, or a config object set programmatically.

ParameterType
valueunknown

ParseResult<{[key: string]: any; }>

The YAML entry points validate; these did not, so a well-formed object that failed the schema went straight to the renderer and produced a broken map with no diagnostic. Reuses the YAML pipeline by round-tripping through YAML, so errors, unknown-key suggestions and deprecation warnings are identical across every entry path.

Positions are stripped: they would refer to the serialized YAML, not to anything the caller wrote, and a line number pointing at text the author never saw is worse than none.


static safeParseScrollytellingBlock(yaml): ParseResult<{[key: string]: any; }>

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

Parse YAML string for a scrollytelling block, returning a result object

ParameterTypeDescription
yamlstringYAML string to parse (should be a scrollytelling block, not a full document)

ParseResult<{[key: string]: any; }>

Result object with success flag and either data or errors

This is the non-throwing version of parseScrollytellingBlock. Instead of throwing errors, it returns a result object that indicates success or failure. Use this when you want to handle errors gracefully without try/catch.

const result = YAMLParser.safeParseScrollytellingBlock(yamlString);
if (result.success) {
console.log('Scrollytelling config:', result.data);
} else {
result.errors.forEach(err => {
console.error(`Error at ${err.path}: ${err.message}`);
});
}

static validate(config): any

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

Validate a JavaScript object against the schema

ParameterTypeDescription
configunknownJavaScript object to validate

any

Validated configuration object

If validation fails

This method bypasses YAML parsing and directly validates a JavaScript object. Useful when you already have a parsed object (e.g., from JSON.parse) and just want to validate and resolve references.

const jsConfig = JSON.parse(jsonString);
const validated = YAMLParser.validate(jsConfig);