Skip to content

Commit 2a841cc

Browse files
committed
fix: Allow empty string as plain scalar representation, for failsafe schema (#616)
1 parent 55c5ef4 commit 2a841cc

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/stringify/stringifyString.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,12 @@ function plainString(
294294
return quotedString(value, ctx)
295295
}
296296
if (
297-
!value ||
298297
/^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(
299298
value
300299
)
301300
) {
302301
// not allowed:
303-
// - empty string, '-' or '?'
302+
// - '-' or '?'
304303
// - start with an indicator character (except [?:-]) or /[?-] /
305304
// - '\n ', ': ' or ' \n' anywhere
306305
// - '#' not preceded by a non-space char

tests/doc/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ aliases:
264264
})
265265
})
266266

267+
describe('failsafe schema', () => {
268+
test('empty scalars (#616)', () => {
269+
const doc = parseDocument('empty:\n', { schema: 'failsafe' })
270+
expect(doc.errors).toMatchObject([])
271+
expect(doc.get('empty', true)).toMatchObject({ value: '' })
272+
expect(doc.toJS()).toMatchObject({ empty: '' })
273+
expect(doc.toString()).toEqual('empty:\n')
274+
})
275+
})
276+
267277
describe('json schema', () => {
268278
test('!!bool', () => {
269279
const src = `"canonical": true

0 commit comments

Comments
 (0)