Skip to content

Commit 9cf06d2

Browse files
committed
fix: Improve warning & avoid TypeError on bad YAML 1.1 nodes (fixes #610)
1 parent 50cf76b commit 9cf06d2

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

src/compose/compose-collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ export function composeCollection(
108108
ctx.schema.tags.push(Object.assign({}, kt, { default: false }))
109109
tag = kt
110110
} else {
111-
if (kt?.collection) {
111+
if (kt) {
112112
onError(
113113
tagToken,
114114
'BAD_COLLECTION_TYPE',
115-
`${kt.tag} used for ${expType} collection, but expects ${kt.collection}`,
115+
`${kt.tag} used for ${expType} collection, but expects ${kt.collection ?? 'scalar'}`,
116116
true
117117
)
118118
} else {

src/schema/yaml-1.1/binary.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const binary: ScalarTag = {
3434
},
3535

3636
stringify({ comment, type, value }, ctx, onComment, onChompKeep) {
37+
if (!value) return ''
3738
const buf = value as Uint8Array // checked earlier by binary.identify()
3839
let str: string
3940
if (typeof Buffer === 'function') {

src/schema/yaml-1.1/timestamp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,5 @@ export const timestamp: ScalarTag & { test: RegExp } = {
116116
},
117117

118118
stringify: ({ value }) =>
119-
(value as Date).toISOString().replace(/(T00:00:00)?\.000Z$/, '')
119+
(value as Date)?.toISOString().replace(/(T00:00:00)?\.000Z$/, '') ?? ''
120120
}

tests/doc/parse.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,24 @@ describe('handling complex keys', () => {
624624
expect(obj).toMatchObject([{ '[ foo ]': 'bar' }])
625625
expect(process.emitWarning).toHaveBeenCalled()
626626
})
627+
628+
test('Error on unresolved !!binary node with mapAsMap: false (#610)', () => {
629+
process.emitWarning = jest.fn()
630+
const doc = YAML.parseDocument('? ? !!binary ? !!binary')
631+
expect(doc.warnings).toMatchObject([{ code: 'BAD_COLLECTION_TYPE' }])
632+
doc.toJS()
633+
expect(process.emitWarning).toHaveBeenCalled()
634+
})
635+
636+
test('Error on unresolved !!timestamp node with mapAsMap: false (#610)', () => {
637+
process.emitWarning = jest.fn()
638+
const doc = YAML.parseDocument(
639+
'? ? !!timestamp ? !!timestamp 2025-03-15T15:35:58.586Z'
640+
)
641+
expect(doc.warnings).toMatchObject([{ code: 'BAD_COLLECTION_TYPE' }])
642+
doc.toJS()
643+
expect(process.emitWarning).toHaveBeenCalled()
644+
})
627645
})
628646

629647
test('Document.toJS({ onAnchor })', () => {

0 commit comments

Comments
 (0)