Skip to content

Commit

Permalink
fix: resolve default for empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 2, 2021
1 parent 8478f0d commit 0257222
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function _resolveSchema (input: InputValue, id: string, ctx: _ResolveCtx): Schem
}

normalizeSchema(schema)
if (ctx.defaults && getValue(ctx.defaults, id) === undefined) {
setValue(ctx.defaults, id, schema.default)
}
return schema
}

Expand All @@ -97,7 +100,7 @@ function normalizeSchema (schema: Partial<Schema>): asserts schema is Schema {
schema.items.type = 'any'
}
}
if (!('default' in schema) && (schema.type === 'object' || schema.type === 'any')) {
if (schema.default === undefined && (schema.type === 'object' || schema.type === 'any')) {
schema.default = {}
}
}
9 changes: 8 additions & 1 deletion test/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('applyDefaults', () => {
it('basic', () => {
const ref = {
name: 'default',
empty: {},
nested: {
val: 1,
list: {
Expand All @@ -17,12 +18,18 @@ describe('applyDefaults', () => {
list: 'b'
}
}
expect(applyDefaults(ref, input)).toMatchObject({

const applied = applyDefaults(ref, input)

expect(applied).toMatchObject({
name: 'custom',
empty: {},
nested: {
val: 1,
list: ['a', 'b']
}
})

expect(applied.empty).toMatchObject({})
})
})

0 comments on commit 0257222

Please sign in to comment.