@formvk/schema 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,576 @@
1
+ import type { Field, Form } from '@formvk/core'
2
+ import { describe, expectTypeOf, test } from 'vitest'
3
+
4
+ import { createSchema } from '../create-schema'
5
+ import { createFragment } from '../fragment'
6
+ import type { SchemaCreator } from '../fragment-types'
7
+ import type { ExpressionProps, ISchema, SchemaExpression, SchemaReaction, Stringify, TScope } from '../types'
8
+
9
+ declare module '../types' {
10
+ interface IScopeContext {
11
+ $globalScope?: { locale: string }
12
+ }
13
+ }
14
+
15
+ interface FieldMap {
16
+ Input: { maxLength?: number }
17
+ Select: { options?: Array<{ label: string; value: any }> }
18
+ }
19
+ interface VoidMap {
20
+ FormItem: { label?: string }
21
+ FormLayout: { colon?: boolean }
22
+ }
23
+ interface ArrayMap {
24
+ ArrayItems: { maxItems?: number }
25
+ }
26
+
27
+ const f = createFragment<FieldMap, VoidMap, ArrayMap>()
28
+
29
+ describe('fragment types', () => {
30
+ test('field creator value', () => {
31
+ expectTypeOf(f.Field()).toEqualTypeOf<SchemaCreator<string | undefined, {}>>()
32
+ expectTypeOf(f.Field<number>({ title: '年龄' })).toEqualTypeOf<SchemaCreator<number | undefined, {}>>()
33
+ expectTypeOf(f.Field({ required: true })).toEqualTypeOf<SchemaCreator<string, {}>>()
34
+ expectTypeOf(f.Field<number>({ title: '年龄', required: true })).toEqualTypeOf<SchemaCreator<number, {}>>()
35
+ })
36
+
37
+ test('field component props', () => {
38
+ f.Field({ component: 'Input', componentProps: { maxLength: 10 } })
39
+ f.Field({ component: 'Select', componentProps: { options: [{ label: 'a', value: 1 }] } })
40
+ f.Input({ componentProps: { maxLength: 10 } })
41
+ f.Input({ title: '姓名' })
42
+ // @ts-expect-error componentProps 由分类 props map 推导
43
+ f.Input({ componentProps: { maxLength: '10' } })
44
+ // @ts-expect-error componentProps 由 component 名推导
45
+ f.Field({ component: 'Input', componentProps: { maxLength: '10' } })
46
+ // @ts-expect-error field 的 type 固定为 string
47
+ f.Field({ type: 'number' })
48
+ })
49
+
50
+ test('componentProps / decoratorProps accept expressions', () => {
51
+ // 叶子表达式:仅 {{...}} 模板字符串放行,普通字符串仍由严格 props 拒收
52
+ f.Input({ componentProps: { maxLength: '{{$values.limit}}' } })
53
+ f.Field({ component: 'Input', componentProps: { maxLength: '{{$values.limit}}' } })
54
+ // 嵌套对象/数组内的字符串叶子
55
+ f.Field({
56
+ component: 'Select',
57
+ componentProps: { options: [{ label: '{{$values.optionLabel}}', value: 1 }] },
58
+ })
59
+ // 整个 props 直接传表达式
60
+ f.Input({ componentProps: '{{$values.inputProps}}' })
61
+ // decoratorProps 同理
62
+ f.Field({ decorator: 'FormItem', decoratorProps: { label: '{{$values.title}}' } })
63
+ f.Input({ decorator: 'FormItem', decoratorProps: '{{$values.itemProps}}' })
64
+ // 组件/装饰器引用本身也支持表达式
65
+ f.Field({ component: '{{$values.widget}}' })
66
+ f.Field({ decorator: '{{$values.decorator}}' })
67
+ // void / object / array 通道一致
68
+ f.FormLayout({ componentProps: { colon: '{{$values.colon}}' } })
69
+ f.Object({}, { componentProps: { colon: '{{$values.colon}}' } })
70
+ f.ArrayItems(f.Field(), { componentProps: { maxItems: '{{$values.max}}' } })
71
+ })
72
+
73
+ test('state and validator keys accept expressions', () => {
74
+ f.Field({
75
+ readOnly: '{{x}}',
76
+ writeOnly: '{{x}}',
77
+ multipleOf: '{{x}}',
78
+ maximum: '{{x}}',
79
+ exclusiveMaximum: '{{x}}',
80
+ minimum: '{{x}}',
81
+ exclusiveMinimum: '{{x}}',
82
+ maxLength: '{{x}}',
83
+ minLength: '{{x}}',
84
+ maxItems: '{{x}}',
85
+ minItems: '{{x}}',
86
+ uniqueItems: '{{x}}',
87
+ maxProperties: '{{x}}',
88
+ minProperties: '{{x}}',
89
+ format: '{{x}}',
90
+ })
91
+ // validator:整体表达式、规则对象叶子表达式、数组混合形态
92
+ f.Field({ validator: '{{x}}' })
93
+ f.Field({ validator: { required: '{{x}}' } })
94
+ f.Field({ validator: ['{{x}}', { format: 'email' }] })
95
+ // 普通字符串/错误类型不匹配表达式模板,仍然拒收
96
+ // @ts-expect-error readOnly 只接受 boolean 或 {{...}} 表达式
97
+ f.Field({ readOnly: 'yes' })
98
+ // @ts-expect-error maxLength 只接受 number 或 {{...}} 表达式
99
+ f.Field({ maxLength: '10' })
100
+ // @ts-expect-error format 只接受 string 或 {{...}} 表达式
101
+ f.Field({ format: 1 })
102
+ })
103
+
104
+ test('default decorator option', () => {
105
+ createFragment<FieldMap, VoidMap, ArrayMap>({ defaultDecorator: 'FormItem' })
106
+ // 允许未注册的组件名,运行时从 components 映射解析
107
+ createFragment<FieldMap, VoidMap, ArrayMap>({ defaultDecorator: 'FieldWrapper' })
108
+ // @ts-expect-error defaultDecorator 必须是组件名或组件引用
109
+ createFragment<FieldMap, VoidMap, ArrayMap>({ defaultDecorator: 1 })
110
+ // @ts-expect-error 第五个泛型声明后,运行时取值必须一致
111
+ createFragment<FieldMap, VoidMap, ArrayMap, {}, 'FormItem'>({ defaultDecorator: 'FormLayout' })
112
+ })
113
+
114
+ test('default decorator drives decoratorProps', () => {
115
+ const decorated = createFragment<FieldMap, VoidMap, ArrayMap, {}, 'FormItem'>({ defaultDecorator: 'FormItem' })
116
+
117
+ decorated.Field({ title: '姓名', decoratorProps: { label: '姓名' } })
118
+ decorated.Input({ title: '姓名', decoratorProps: { label: '姓名' } })
119
+ decorated.Object({}, { decoratorProps: { label: '布局' } })
120
+ decorated.Void({}, { decoratorProps: { label: '布局' } })
121
+ decorated.Array(decorated.Field(), { decoratorProps: { label: '列表' } })
122
+ decorated.FormLayout({ decoratorProps: { label: '布局' } })
123
+ decorated.ArrayItems(decorated.Field(), { decoratorProps: { label: '列表' } })
124
+
125
+ // @ts-expect-error decoratorProps 由默认装饰器推导
126
+ decorated.Field({ title: '姓名', decoratorProps: { colon: true } })
127
+ // @ts-expect-error decoratorProps 由默认装饰器推导
128
+ decorated.Input({ title: '姓名', decoratorProps: { colon: true } })
129
+ // @ts-expect-error decoratorProps 由默认装饰器推导
130
+ decorated.Object({}, { decoratorProps: { colon: true } })
131
+ // @ts-expect-error decoratorProps 由默认装饰器推导
132
+ decorated.Void({}, { decoratorProps: { colon: true } })
133
+ // @ts-expect-error decoratorProps 由默认装饰器推导
134
+ decorated.Array(decorated.Field(), { decoratorProps: { colon: true } })
135
+ // @ts-expect-error decoratorProps 由默认装饰器推导
136
+ decorated.FormLayout({ decoratorProps: { colon: true } })
137
+ // @ts-expect-error decoratorProps 由默认装饰器推导
138
+ decorated.ArrayItems(decorated.Field(), { decoratorProps: { colon: true } })
139
+
140
+ // 显式 decorator 仍然覆盖默认值
141
+ decorated.Field({ title: '姓名', decorator: 'FormLayout', decoratorProps: { colon: true } })
142
+ // @ts-expect-error decoratorProps 由显式 decorator 推导
143
+ decorated.Field({ title: '姓名', decorator: 'FormLayout', decoratorProps: { label: '姓名' } })
144
+ })
145
+
146
+ test('default decorator reference drives decoratorProps', () => {
147
+ class StubFormItem {
148
+ $props!: { label?: string }
149
+ }
150
+ const decorated = createFragment<FieldMap, VoidMap, ArrayMap, {}, typeof StubFormItem>({
151
+ defaultDecorator: StubFormItem,
152
+ })
153
+
154
+ decorated.Field({ title: '姓名', decoratorProps: { label: '姓名' } })
155
+ // @ts-expect-error decoratorProps 由默认装饰器组件推导
156
+ decorated.Field({ title: '姓名', decoratorProps: { colon: true } })
157
+ })
158
+
159
+ test('field decorator props', () => {
160
+ f.Field({ component: 'Input', decorator: 'FormItem', decoratorProps: { label: '姓名' } })
161
+ f.Input({ decorator: 'FormItem', decoratorProps: { label: '姓名' } })
162
+ // @ts-expect-error decoratorProps 由 decorator 名推导
163
+ f.Field({ component: 'Input', decorator: 'FormItem', decoratorProps: { colon: true } })
164
+ // @ts-expect-error decoratorProps 由 decorator 名推导
165
+ f.Input({ decorator: 'FormItem', decoratorProps: { colon: true } })
166
+ })
167
+
168
+ test('object / void / array decorator props', () => {
169
+ f.Object({}, { decorator: 'FormItem', decoratorProps: { label: '布局' } })
170
+ f.Void({}, { decorator: 'FormItem', decoratorProps: { label: '布局' } })
171
+ f.Array(f.Field(), { decorator: 'FormItem', decoratorProps: { label: '列表' } })
172
+ f.FormLayout({ decorator: 'FormItem', decoratorProps: { label: '布局' } })
173
+ f.ArrayItems(f.Field(), { decorator: 'FormItem', decoratorProps: { label: '列表' } })
174
+ // @ts-expect-error decoratorProps 由 decorator 名推导
175
+ f.Object({}, { decorator: 'FormLayout', decoratorProps: { label: '布局' } })
176
+ // @ts-expect-error decoratorProps 由 decorator 名推导
177
+ f.Void({}, { decorator: 'FormLayout', decoratorProps: { label: '布局' } })
178
+ // @ts-expect-error decoratorProps 由 decorator 名推导
179
+ f.Array(f.Field(), { decorator: 'FormLayout', decoratorProps: { label: '列表' } })
180
+ // @ts-expect-error decoratorProps 由 decorator 名推导
181
+ f.FormLayout({ decorator: 'FormLayout', decoratorProps: { label: '布局' } })
182
+ // @ts-expect-error decoratorProps 由 decorator 名推导
183
+ f.ArrayItems(f.Field(), { decorator: 'FormLayout', decoratorProps: { label: '列表' } })
184
+ })
185
+
186
+ test('object / void / array decorator references infer props', () => {
187
+ class StubLayout {
188
+ $props!: { colon?: boolean }
189
+ }
190
+ class StubArrayItems {
191
+ $props!: { maxItems?: number }
192
+ }
193
+ class StubFormItem {
194
+ $props!: { label?: string }
195
+ }
196
+ const plain = createFragment()
197
+
198
+ plain.Object({}, { component: StubLayout, decorator: StubFormItem, decoratorProps: { label: '布局' } })
199
+ // @ts-expect-error decoratorProps 由装饰器本身的 $props 推导
200
+ plain.Object({}, { component: StubLayout, decorator: StubFormItem, decoratorProps: { colon: true } })
201
+
202
+ plain.Void({}, { component: StubLayout, decorator: StubFormItem, decoratorProps: { label: '布局' } })
203
+ // @ts-expect-error decoratorProps 由装饰器本身的 $props 推导
204
+ plain.Void({}, { component: StubLayout, decorator: StubFormItem, decoratorProps: { colon: true } })
205
+
206
+ plain.Array(plain.Field(), {
207
+ component: StubArrayItems,
208
+ decorator: StubFormItem,
209
+ decoratorProps: { label: '列表' },
210
+ })
211
+ plain.Array(plain.Field(), {
212
+ component: StubArrayItems,
213
+ decorator: StubFormItem,
214
+ // @ts-expect-error decoratorProps 由装饰器本身的 $props 推导
215
+ decoratorProps: { colon: true },
216
+ })
217
+ })
218
+
219
+ test('object / void / array accept component references', () => {
220
+ class StubLayout {
221
+ $props!: { colon?: boolean }
222
+ }
223
+ class StubArrayItems {
224
+ $props!: { maxItems?: number }
225
+ }
226
+ const plain = createFragment()
227
+
228
+ plain.Object({}, { component: StubLayout, componentProps: { colon: true } })
229
+ // @ts-expect-error componentProps 由组件本身的 $props 推导
230
+ plain.Object({}, { component: StubLayout, componentProps: { colon: 'yes' } })
231
+
232
+ plain.Void({}, { component: StubLayout, componentProps: { colon: true } })
233
+ // @ts-expect-error componentProps 由组件本身的 $props 推导
234
+ plain.Void({}, { component: StubLayout, componentProps: { colon: 'yes' } })
235
+
236
+ plain.Array(plain.Field(), { component: StubArrayItems, componentProps: { maxItems: 2 } })
237
+ // @ts-expect-error componentProps 由组件本身的 $props 推导
238
+ plain.Array(plain.Field(), { component: StubArrayItems, componentProps: { maxItems: '2' } })
239
+ })
240
+
241
+ test('component references infer props without registration', () => {
242
+ class StubInput {
243
+ $props!: { maxLength?: number }
244
+ }
245
+ class StubFormItem {
246
+ $props!: { label?: string }
247
+ }
248
+ class ReactInput {
249
+ props!: { maxLength?: number }
250
+
251
+ constructor(_props: { maxLength?: number }) {}
252
+ }
253
+ const plain = createFragment()
254
+
255
+ plain.Field({ component: StubInput, componentProps: { maxLength: 10 } })
256
+ // @ts-expect-error componentProps 由组件本身的 $props 推导
257
+ plain.Field({ component: StubInput, componentProps: { maxLength: '10' } })
258
+
259
+ plain.Field({ component: StubInput, decorator: StubFormItem, decoratorProps: { label: '姓名' } })
260
+ // @ts-expect-error decoratorProps 由装饰器本身的 $props 推导
261
+ plain.Field({ component: StubInput, decorator: StubFormItem, decoratorProps: { colon: true } })
262
+
263
+ // React 类组件从构造函数首参推导 props
264
+ plain.Field({ component: ReactInput, componentProps: { maxLength: 10 } })
265
+ // @ts-expect-error componentProps 由类组件的构造函数推导
266
+ plain.Field({ component: ReactInput, componentProps: { maxLength: '10' } })
267
+
268
+ // 未注册时字符串组件名仍保持宽松
269
+ plain.Field({ component: 'Anything', componentProps: { whatever: 1 } })
270
+ })
271
+
272
+ test('reactions infer value and record', () => {
273
+ f.Field<number>({
274
+ reactions: field => {
275
+ expectTypeOf(field.value).toEqualTypeOf<number>()
276
+ },
277
+ })
278
+ f.Field({
279
+ reactions: field => {
280
+ expectTypeOf(field.value).toEqualTypeOf<string>()
281
+ },
282
+ })
283
+ f.Input<number>({
284
+ reactions: field => {
285
+ expectTypeOf(field.value).toEqualTypeOf<number>()
286
+ },
287
+ })
288
+ f.Object(
289
+ { name: f.Field({ required: true }) },
290
+ {
291
+ reactions: field => {
292
+ expectTypeOf(field.value).toEqualTypeOf<{ name?: string }>()
293
+ },
294
+ }
295
+ )
296
+ f.Array(f.Object({ name: f.Field({ required: true }) }), {
297
+ reactions: field => {
298
+ expectTypeOf(field.value).toEqualTypeOf<{ name?: string }[]>()
299
+ expectTypeOf(field.record).toEqualTypeOf<{ name?: string }>()
300
+ },
301
+ })
302
+ f.ArrayItems(f.Field<number>({ required: true }), {
303
+ reactions: field => {
304
+ expectTypeOf(field.value).toEqualTypeOf<number[]>()
305
+ },
306
+ })
307
+ })
308
+
309
+ test('reactions infer built-in scope', () => {
310
+ f.Field<number>({
311
+ reactions: (field, scope) => {
312
+ expectTypeOf(scope.$self).toEqualTypeOf(field)
313
+ expectTypeOf(scope.$self.value).toEqualTypeOf<number>()
314
+ expectTypeOf(scope.$index).toEqualTypeOf<number>()
315
+ expectTypeOf(scope.$form).toEqualTypeOf<Form<any>>()
316
+ expectTypeOf(scope.$values).toBeAny()
317
+ expectTypeOf(scope.$record).toBeAny()
318
+ expectTypeOf(scope.$records).toBeArray()
319
+ expectTypeOf(scope.$memo(() => 1)).toEqualTypeOf<number>()
320
+ expectTypeOf(scope.$observable({ count: 1 })).toEqualTypeOf<{ count: number }>()
321
+ expectTypeOf(scope.$effect).toBeFunction()
322
+ expectTypeOf(scope.$props).toBeFunction()
323
+ },
324
+ })
325
+
326
+ f.Input({
327
+ reactions: (field, scope) => {
328
+ scope.$props({ maxLength: 10 })
329
+ // @ts-expect-error $props 使用组件 props 类型
330
+ scope.$props({ maxLength: '10' })
331
+ },
332
+ })
333
+ })
334
+
335
+ test('TScope composes built-in, global and custom scope', () => {
336
+ interface MyScope {
337
+ $moment: (value: string) => string
338
+ }
339
+ type Values = { list: { name: string }[] }
340
+ interface RowRecord {
341
+ name: string
342
+ }
343
+
344
+ const scope = Object.create(null) as TScope<MyScope, Field<string, any, any, RowRecord>, RowRecord, Values>
345
+
346
+ expectTypeOf(scope.$self).toEqualTypeOf<Field<string, any, any, RowRecord>>()
347
+ expectTypeOf(scope.$record).toEqualTypeOf<RowRecord & { $lookup: any; $index: number }>()
348
+ expectTypeOf(scope.$records).toEqualTypeOf<RowRecord[]>()
349
+ expectTypeOf(scope.$values).toEqualTypeOf<Values>()
350
+ expectTypeOf(scope.$form).toEqualTypeOf<Form<Values>>()
351
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
352
+ // 全局扩展
353
+ expectTypeOf(scope.$globalScope).toEqualTypeOf<{ locale: string } | undefined>()
354
+ })
355
+
356
+ test('reactions scope generic', () => {
357
+ interface MyScope {
358
+ $moment: (value: string) => string
359
+ }
360
+ const scoped = createFragment<FieldMap, VoidMap, ArrayMap, MyScope>()
361
+
362
+ scoped.Field<number>({
363
+ reactions: (field, scope) => {
364
+ expectTypeOf(field.value).toEqualTypeOf<number>()
365
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
366
+ // @ts-expect-error scope 未声明的字段
367
+ void scope.$unknown
368
+ },
369
+ })
370
+
371
+ scoped.Field({
372
+ required: true,
373
+ reactions: (field, scope) => {
374
+ expectTypeOf(field.value).toEqualTypeOf<string>()
375
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
376
+ },
377
+ })
378
+
379
+ scoped.Array(scoped.Object({ name: scoped.Field({ required: true }) }), {
380
+ reactions: (field, scope) => {
381
+ expectTypeOf(field.value).toEqualTypeOf<{ name?: string }[]>()
382
+ expectTypeOf(field.record).toEqualTypeOf<{ name?: string }>()
383
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
384
+ },
385
+ })
386
+
387
+ scoped.FormLayout({
388
+ reactions: (field, scope) => {
389
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
390
+ },
391
+ })
392
+ })
393
+
394
+ test('parent declared record and scope flow into children', () => {
395
+ interface MyScope {
396
+ $moment: (value: string) => string
397
+ }
398
+ type Values = { list: Array<{ name: string }> }
399
+ const scoped = createFragment<FieldMap, VoidMap, ArrayMap, MyScope>()
400
+
401
+ scoped.Object<{ name: string; age: number }, MyScope>({
402
+ name: scoped.Field({
403
+ required: true,
404
+ reactions: (field, scope) => {
405
+ expectTypeOf(field.value).toEqualTypeOf<string>()
406
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
407
+ expectTypeOf(scope.$record).toEqualTypeOf<{ name: string; age: number } & { $lookup: any; $index: number }>()
408
+ },
409
+ }),
410
+ // 不写 Field 泛型时,值类型由父级 Record 的 `age: number` 收敛
411
+ age: scoped.Field({
412
+ required: true,
413
+ reactions: (field, scope) => {
414
+ expectTypeOf(field.value).toEqualTypeOf<number>()
415
+ expectTypeOf(scope.$record).toEqualTypeOf<{ name: string; age: number } & { $lookup: any; $index: number }>()
416
+ },
417
+ }),
418
+ // 未在父级 Record 中声明的 key 只允许 void 子节点
419
+ layout: scoped.FormLayout(),
420
+ // @ts-expect-error 未声明的 key 不允许产生值的字段
421
+ extra: scoped.Field(),
422
+ })
423
+
424
+ // 嵌套对象显式声明自身值类型即可,Scope 与父级一致时无需重复声明
425
+ scoped.Object<{ address: { street: string } }>({
426
+ address: scoped.Object<{ street: string }>({
427
+ street: scoped.Field({
428
+ required: true,
429
+ reactions: (field, scope) => {
430
+ expectTypeOf(field.value).toEqualTypeOf<string>()
431
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
432
+ expectTypeOf(scope.$record).toEqualTypeOf<{ street: string } & { $lookup: any; $index: number }>()
433
+ },
434
+ }),
435
+ }),
436
+ })
437
+
438
+ scoped.Void<{ name: string }, MyScope>({
439
+ name: scoped.Field({
440
+ required: true,
441
+ reactions: (field, scope) => {
442
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
443
+ expectTypeOf(scope.$record).toEqualTypeOf<{ name: string } & { $lookup: any; $index: number }>()
444
+ },
445
+ }),
446
+ layout: scoped.FormLayout(),
447
+ })
448
+
449
+ scoped.Array<{ name: string }, MyScope>(
450
+ scoped.Object<{ name: string }>({
451
+ name: scoped.Field({
452
+ required: true,
453
+ reactions: (field, scope) => {
454
+ expectTypeOf(scope.$record).toEqualTypeOf<{ name: string } & { $lookup: any; $index: number }>()
455
+ },
456
+ }),
457
+ }),
458
+ {
459
+ reactions: (field, scope) => {
460
+ expectTypeOf(field.value).toEqualTypeOf<{ name: string }[]>()
461
+ expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
462
+ },
463
+ }
464
+ )
465
+
466
+ // 父级声明后子级仍可显式覆盖 record 与 values
467
+ scoped.Object<{ name: string }, MyScope, { name: string }>({
468
+ name: scoped.Field<string, 'Input', 'FormItem', MyScope, { name: string }, Values>({
469
+ required: true,
470
+ reactions: (field, scope) => {
471
+ expectTypeOf(field.value).toEqualTypeOf<string>()
472
+ expectTypeOf(scope.$values).toEqualTypeOf<Values>()
473
+ },
474
+ }),
475
+ })
476
+ })
477
+
478
+ test('SchemaReaction accepts explicit scope generic', () => {
479
+ type MyScope = { $locale: string }
480
+ const reaction: SchemaReaction<Field<string>, MyScope> = (field, scope) => {
481
+ expectTypeOf(field.value).toEqualTypeOf<string>()
482
+ expectTypeOf(scope.$locale).toEqualTypeOf<string>()
483
+ }
484
+ void reaction
485
+ })
486
+
487
+ test('IScopeContext can be extended globally', () => {
488
+ f.Field({
489
+ reactions: (field, scope) => {
490
+ expectTypeOf(scope.$globalScope).toEqualTypeOf<{ locale: string } | undefined>()
491
+ },
492
+ })
493
+ })
494
+
495
+ test('dynamic creators require a declared component', () => {
496
+ const plain = createFragment()
497
+ // @ts-expect-error 未在组件映射中声明 Input 时不存在 f.Input
498
+ plain.Input()
499
+ })
500
+
501
+ test('object creator value', () => {
502
+ const result = f.Object({
503
+ name: f.Field({ required: true }),
504
+ age: f.Field<number>(),
505
+ })
506
+ expectTypeOf<(typeof result._$types$_)[0]>().toEqualTypeOf<{
507
+ name?: string
508
+ age?: number | undefined
509
+ }>()
510
+ })
511
+
512
+ test('void creator', () => {
513
+ const result = f.Void({
514
+ name: f.Field({ required: true }),
515
+ })
516
+ expectTypeOf<(typeof result._$types$_)[0]>().toEqualTypeOf<{
517
+ name?: string
518
+ }>()
519
+ f.FormLayout({ componentProps: { colon: true } })
520
+ // @ts-expect-error componentProps 由分类 props map 推导
521
+ f.FormLayout({ componentProps: { colon: 'true' } })
522
+ })
523
+
524
+ test('array creator value', () => {
525
+ const array = f.Array(f.Field({ required: true }))
526
+ expectTypeOf<(typeof array._$types$_)[0]>().toEqualTypeOf<string[]>()
527
+ const arrayItems = f.ArrayItems(f.Field({ required: true }))
528
+ expectTypeOf<(typeof arrayItems._$types$_)[0]>().toEqualTypeOf<string[]>()
529
+ f.ArrayItems(f.Field(), { componentProps: { maxItems: 2 } })
530
+ // @ts-expect-error componentProps 由分类 props map 推导
531
+ f.ArrayItems(f.Field(), { componentProps: { maxItems: '2' } })
532
+ })
533
+
534
+ test('void component item accepts schema directly', () => {
535
+ f.FormItem({ title: '姓名' })
536
+ f.FormItem({ componentProps: { label: '姓名' } })
537
+ })
538
+
539
+ test('createSchema values', () => {
540
+ const schema = createSchema({
541
+ name: f.Field({ required: true }),
542
+ age: f.Field<number>(),
543
+ })
544
+ expectTypeOf<(typeof schema._$types$_)[0]>().toEqualTypeOf<{
545
+ name?: string
546
+ age?: number | undefined
547
+ }>()
548
+ })
549
+
550
+ test('ISchema can be extended', () => {
551
+ interface MySchema extends ISchema {
552
+ fieldWidget?: string
553
+ }
554
+
555
+ expectTypeOf<MySchema>().toMatchTypeOf<ISchema>()
556
+ expectTypeOf<MySchema['visible']>().toEqualTypeOf<boolean | SchemaExpression | undefined>()
557
+ expectTypeOf<MySchema['fieldWidget']>().toEqualTypeOf<string | undefined>()
558
+ })
559
+
560
+ test('Stringify', () => {
561
+ expectTypeOf<Stringify<string>>().toEqualTypeOf<string | SchemaExpression>()
562
+ expectTypeOf<Stringify<boolean>>().toEqualTypeOf<boolean | SchemaExpression>()
563
+ expectTypeOf<Stringify<string[]>>().toEqualTypeOf<Array<string | SchemaExpression> | SchemaExpression>()
564
+ expectTypeOf<Stringify<{ visible: boolean }>>().toEqualTypeOf<{
565
+ visible?: boolean | SchemaExpression
566
+ }>()
567
+ })
568
+
569
+ test('ExpressionProps', () => {
570
+ // 保留原 key 的必填/可选修饰符,叶子与整体均允许表达式
571
+ expectTypeOf<ExpressionProps<{ a: string; b?: number }>>().toEqualTypeOf<
572
+ SchemaExpression | { a: string | SchemaExpression; b?: number | SchemaExpression }
573
+ >()
574
+ expectTypeOf<ExpressionProps<number>>().toEqualTypeOf<number | SchemaExpression>()
575
+ })
576
+ })