diff --git a/yaml/_type/omap.ts b/yaml/_type/omap.ts index ded6e1e26031..73ab906cb868 100644 --- a/yaml/_type/omap.ts +++ b/yaml/_type/omap.ts @@ -35,12 +35,7 @@ function resolveYamlOmap(data: Any): boolean { return true; } -function constructYamlOmap(data: Any): Any { - return data !== null ? data : []; -} - export const omap = new Type("tag:yaml.org,2002:omap", { - construct: constructYamlOmap, kind: "sequence", resolve: resolveYamlOmap, }); diff --git a/yaml/parse_test.ts b/yaml/parse_test.ts index efc658120afb..d04a9d90f7ca 100644 --- a/yaml/parse_test.ts +++ b/yaml/parse_test.ts @@ -271,3 +271,45 @@ Deno.test({ ]); }, }); + +Deno.test({ + name: "parse() handles omap type", + fn() { + const yaml = `--- !!omap +- Mark McGwire: 65 +- Sammy Sosa: 63 +- Ken Griffey: 58 +`; + assertEquals(parse(yaml), [ + { "Mark McGwire": 65 }, + { "Sammy Sosa": 63 }, + { "Ken Griffey": 58 }, + ]); + + // Invalid omap + // map entry is not an object + assertThrows( + () => parse("--- !!omap\n- 1"), + YAMLError, + "cannot resolve a node with ! explicit tag", + ); + // map entry is empty object + assertThrows( + () => parse("--- !!omap\n- {}"), + YAMLError, + "cannot resolve a node with ! explicit tag", + ); + // map entry is an object with multiple keys + assertThrows( + () => parse("--- !!omap\n- foo: 1\n bar: 2"), + YAMLError, + "cannot resolve a node with ! explicit tag", + ); + // 2 map entries have the same key + assertThrows( + () => parse("--- !!omap\n- foo: 1\n- foo: 2"), + YAMLError, + "cannot resolve a node with ! explicit tag", + ); + }, +});