Skip to content

Commit

Permalink
fix: fix simplify behavior on object / dict, fix koishijs/webui#323
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 24, 2024
1 parent f61fafd commit 1416b55
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Expand Up @@ -282,7 +282,7 @@ Schema.prototype.pattern = function pattern(regexp) {
}

Schema.prototype.simplify = function simplify(this: Schema, value) {
if (deepEqual(value, this.meta.default)) return null
if (deepEqual(value, this.meta.default, this.type === 'dict')) return null
if (isNullable(value)) return value
if (this.type === 'object' || this.type === 'dict') {
const result: Dict = {}
Expand All @@ -291,6 +291,7 @@ Schema.prototype.simplify = function simplify(this: Schema, value) {
const item = schema?.simplify(value[key])
if (this.type === 'dict' || !isNullable(item)) result[key] = item
}
if (deepEqual(result, this.meta.default, this.type === 'dict')) return null
return result
} else if (this.type === 'array' || this.type === 'tuple') {
const result: any[] = []
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/simplify.spec.ts
Expand Up @@ -19,7 +19,7 @@ describe('Simplify', () => {

expect(schema.simplify(null)).to.deep.equal(null)
expect(schema.simplify({ a: 'a', b: 1 })).to.deep.equal({ b: 1 })
expect(schema.simplify({ c: {} })).to.deep.equal({})
expect(schema.simplify({ c: { d: true } })).to.deep.equal({})
expect(schema.simplify({ c: { d: false } })).to.deep.equal({ c: { d: false } })
})
})

0 comments on commit 1416b55

Please sign in to comment.