diff --git a/src/stringify/stringifyPair.ts b/src/stringify/stringifyPair.ts index ad9fabcd..4ef6710f 100644 --- a/src/stringify/stringifyPair.ts +++ b/src/stringify/stringifyPair.ts @@ -22,7 +22,7 @@ export function stringifyPair( if (keyComment) { throw new Error('With simple keys, key nodes cannot have comments') } - if (isCollection(key)) { + if (isCollection(key) || (!isNode(key) && typeof key === 'object')) { const msg = 'With simple keys, collection cannot be used as a key value' throw new Error(msg) } diff --git a/tests/doc/stringify.ts b/tests/doc/stringify.ts index 0ff20d58..7a150e63 100644 --- a/tests/doc/stringify.ts +++ b/tests/doc/stringify.ts @@ -723,6 +723,24 @@ describe('simple keys', () => { ) }) + test('key with JS object value', () => { + const doc = YAML.parseDocument('[foo]: bar') + doc.contents.items[0].key = { foo: 42 } + expect(doc.toString()).toBe('? foo: 42\n: bar\n') + expect(() => doc.toString({ simpleKeys: true })).toThrow( + /With simple keys, collection cannot be used as a key value/ + ) + }) + + test('key with JS null value', () => { + const doc = YAML.parseDocument('[foo]: bar') + doc.contents.items[0].key = null + expect(doc.toString()).toBe('? null\n: bar\n') + expect(() => doc.toString({ simpleKeys: true })).toThrow( + /With simple keys, collection cannot be used as a key value/ + ) + }) + test('key value lingth > 1024', () => { const str = ` ? ${new Array(1026).join('a')}