koishijs / koishi

Cross-platform chatbot framework made with love
https://koishi.chat
MIT License
4.46k stars 244 forks source link

Bug(Schema): Computed with Union Not Showing Branches Correctly #1382

Open MaikoTan opened 7 months ago

MaikoTan commented 7 months ago

Describe the bug

Computed schema with union schema behaves weird currently.

Also some are not visible in production mode.

Steps to reproduce

export interface Config {
  testNormal: 'one' | 'two'
  testComputedNumber: Computed<number>
  testComputed: Computed<'one' | 'two'>
  testComputedUnionConst: Computed<'one' | 'two'>
  testComputedUnionConstWithCustom: Computed<'one' | 'two' | { value: number }>
  testComputedRadio: Computed<'one' | 'two'>
  testComputedUnionConstRadio: Computed<'one' | 'two'>
}

export const Config: Schema<Config> = Schema.object({
  testNormal: Schema.union(['one', 'two']).default('one'),
  testComputedNumber: Schema.computed(Schema.number()).default(1),
  testComputed: Schema.computed(Schema.union(['one', 'two'])).default('one'),
  testComputedUnionConst: Schema.computed(Schema.union([Schema.const('one'), Schema.const('two')])).default('one'),
  testComputedUnionConstWithCustom: Schema.computed(
    Schema.union([Schema.const('one'), Schema.const('two'), Schema.object({ value: Schema.number() })]),
  ).default('one'),
  testComputedRadio: Schema.computed(Schema.union(['one', 'two']).role('radio')).default('one'),
  testComputedUnionConstRadio: Schema.computed(
    Schema.union([Schema.const('one'), Schema.const('two')]).role('radio'),
  ).default('one'),
})

Or see this repo instead

Expected behaviour

Able to resolve options and save config correctly.

Screenshots

Dev Mode

image

Prod Mode

image

Versions

Additional context

No response

shigma commented 7 months ago

computed(union()).role('radio')

It should be computed(union().role('radio')). Actually computed itself is a role.

MaikoTan commented 7 months ago

computed(union()).role('radio')

It should be computed(union().role('radio')). Actually computed itself is a role.

Okay I have updated the examples and:

image