@formvk/schema 0.1.0 → 1.0.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.
@@ -1,380 +0,0 @@
1
- import { describe, expect, test, vi } from 'vitest'
2
-
3
- import { createFragment } from '../fragment'
4
- import type { ISchema } from '../types'
5
-
6
- type LooseMap = Record<string, any>
7
-
8
- describe('createFragment', () => {
9
- test('field', () => {
10
- const f = createFragment()
11
- expect(f.Field()()).toEqual({ type: 'string' })
12
- expect(f.Field({ title: '姓名', component: 'Input' })()).toEqual({
13
- type: 'string',
14
- title: '姓名',
15
- component: 'Input',
16
- })
17
- })
18
-
19
- test('does not add any default patch', () => {
20
- const f = createFragment()
21
- expect(f.Field({ title: '姓名', component: 'Input' })()).toEqual({
22
- type: 'string',
23
- title: '姓名',
24
- component: 'Input',
25
- })
26
- expect(f.Field({ title: '姓名', component: 'Input' })()).not.toHaveProperty('decorator')
27
- })
28
-
29
- test('accepts component references', () => {
30
- const f = createFragment()
31
- const Input = { name: 'Input' }
32
- const FormItem = { name: 'FormItem' }
33
- const ArrayItems = { name: 'ArrayItems' }
34
- const schema = f.Field({ component: Input, decorator: FormItem })()
35
- expect(schema.component).toBe(Input)
36
- expect(schema.decorator).toBe(FormItem)
37
- expect(f.Object({}, { component: Input, decorator: FormItem })().decorator).toBe(FormItem)
38
- expect(f.Void({}, { component: Input, decorator: FormItem })().decorator).toBe(FormItem)
39
- expect(f.Array(f.Field(), { component: ArrayItems, decorator: FormItem })().decorator).toBe(FormItem)
40
- })
41
-
42
- test('defaultDecorator', () => {
43
- const f = createFragment<LooseMap, LooseMap, LooseMap>({ defaultDecorator: 'FormItem' })
44
-
45
- expect(f.Input({ title: '姓名' })()).toEqual({
46
- type: 'string',
47
- title: '姓名',
48
- component: 'Input',
49
- decorator: 'FormItem',
50
- })
51
- // 无 title 的字段不补
52
- expect(f.Field({ component: 'Input' })()).toEqual({
53
- type: 'string',
54
- component: 'Input',
55
- })
56
- // 显式 decorator 优先
57
- expect(f.Field({ title: '姓名', decorator: 'FieldWrapper' })()).toEqual({
58
- type: 'string',
59
- title: '姓名',
60
- decorator: 'FieldWrapper',
61
- })
62
- // 非 string 节点不补
63
- expect(f.Object({ name: f.Field({ title: '姓名' }) }, { title: '布局' })()).toEqual({
64
- type: 'object',
65
- title: '布局',
66
- properties: {
67
- name: { type: 'string', title: '姓名', decorator: 'FormItem' },
68
- },
69
- })
70
- expect(f.Void({}, { title: '布局' })()).not.toHaveProperty('decorator')
71
- expect(f.Array(f.Field(), { title: '列表' })()).not.toHaveProperty('decorator')
72
- })
73
-
74
- test('defaultDecorator can be overridden by patches', () => {
75
- const f = createFragment({
76
- defaultDecorator: 'FormItem',
77
- patches: schema => ({ ...schema, decorator: 'FieldWrapper' }),
78
- })
79
-
80
- expect(f.Field({ title: '姓名' })()).toEqual({
81
- type: 'string',
82
- title: '姓名',
83
- decorator: 'FieldWrapper',
84
- })
85
- })
86
-
87
- test('object', () => {
88
- const f = createFragment()
89
- expect(f.Object({ name: f.Field({ component: 'Input' }) })()).toEqual({
90
- type: 'object',
91
- properties: {
92
- name: {
93
- type: 'string',
94
- component: 'Input',
95
- },
96
- },
97
- })
98
- })
99
-
100
- test('object accepts plain schema and skips nil', () => {
101
- const f = createFragment()
102
- const schema = f.Object({
103
- name: f.Field(),
104
- plain: { type: 'number' },
105
- empty: null,
106
- missing: undefined,
107
- } as any)()
108
- expect(schema.properties).toEqual({
109
- name: { type: 'string' },
110
- plain: { type: 'number' },
111
- })
112
- })
113
-
114
- test('void', () => {
115
- const f = createFragment()
116
- expect(f.Void({ submit: f.Field({ component: 'Submit', content: '提交' }) })()).toEqual({
117
- type: 'void',
118
- properties: {
119
- submit: {
120
- type: 'string',
121
- component: 'Submit',
122
- content: '提交',
123
- },
124
- },
125
- })
126
- })
127
-
128
- test('array', () => {
129
- const f = createFragment()
130
- expect(f.Array(f.Object({ name: f.Field() }))()).toEqual({
131
- type: 'array',
132
- items: {
133
- type: 'object',
134
- properties: {
135
- name: { type: 'string' },
136
- },
137
- },
138
- })
139
- expect(f.Array(f.Field())()).toEqual({
140
- type: 'array',
141
- items: { type: 'string' },
142
- })
143
- })
144
-
145
- test('transform', () => {
146
- const f = createFragment()
147
- expect(
148
- f.transform({
149
- type: 'object',
150
- properties: {
151
- name: { type: 'string', component: 'Input' },
152
- list: {
153
- type: 'array',
154
- items: {
155
- type: 'object',
156
- properties: {
157
- age: { type: 'number', component: 'NumberPicker' },
158
- },
159
- },
160
- },
161
- layout: {
162
- type: 'void',
163
- properties: {
164
- submit: { type: 'string', component: 'Submit' },
165
- },
166
- },
167
- },
168
- })
169
- ).toEqual({
170
- type: 'object',
171
- properties: {
172
- name: { type: 'string', component: 'Input' },
173
- list: {
174
- type: 'array',
175
- items: {
176
- type: 'object',
177
- properties: {
178
- age: { type: 'number', component: 'NumberPicker' },
179
- },
180
- },
181
- },
182
- layout: {
183
- type: 'void',
184
- properties: {
185
- submit: { type: 'string', component: 'Submit' },
186
- },
187
- },
188
- },
189
- })
190
- expect(f.transform()).toBeUndefined()
191
- })
192
-
193
- test('dynamic components', () => {
194
- const f = createFragment<LooseMap, LooseMap, LooseMap>()
195
- expect(f.Input({ title: '姓名' })()).toEqual({
196
- type: 'string',
197
- title: '姓名',
198
- component: 'Input',
199
- })
200
- })
201
-
202
- test('dynamic void and array components', () => {
203
- const f = createFragment<LooseMap, LooseMap, LooseMap>({
204
- voidComponentNames: ['FormLayout', 'Submit'],
205
- arrayComponentNames: ['ArrayItems'],
206
- })
207
- expect(f.FormLayout({ title: '布局' })()).toEqual({
208
- type: 'void',
209
- title: '布局',
210
- component: 'FormLayout',
211
- })
212
- expect(f.Submit({ content: '提交' })()).toEqual({
213
- type: 'void',
214
- content: '提交',
215
- component: 'Submit',
216
- })
217
- expect(f.ArrayItems(f.Field(), { title: '列表' })()).toEqual({
218
- type: 'array',
219
- title: '列表',
220
- component: 'ArrayItems',
221
- items: { type: 'string' },
222
- })
223
- })
224
-
225
- test('patches', () => {
226
- const f = createFragment({
227
- patches: [
228
- schema => ({
229
- ...schema,
230
- data: { from: 'options' },
231
- }),
232
- ],
233
- })
234
-
235
- expect(f.Field({ title: '姓名' })()).toEqual({
236
- type: 'string',
237
- title: '姓名',
238
- data: { from: 'options' },
239
- })
240
-
241
- expect(
242
- f.Field({ title: '姓名' })([
243
- schema => ({
244
- ...schema,
245
- data: { from: 'call' },
246
- }),
247
- ])
248
- ).toEqual({
249
- type: 'string',
250
- title: '姓名',
251
- data: { from: 'call' },
252
- })
253
- })
254
-
255
- test('pre patches run first', () => {
256
- const calls: string[] = []
257
- const f = createFragment({
258
- patches: [
259
- schema => {
260
- calls.push('options')
261
- return schema
262
- },
263
- ],
264
- })
265
-
266
- f.Field()([
267
- schema => {
268
- calls.push('call')
269
- return schema
270
- },
271
- {
272
- pre: true,
273
- patch: schema => {
274
- calls.push('pre')
275
- return schema
276
- },
277
- },
278
- ])
279
-
280
- expect(calls).toEqual(['pre', 'options', 'call'])
281
- })
282
-
283
- test('patches are forwarded to nested creators', () => {
284
- const f = createFragment()
285
- const schema = f.Object({
286
- name: f.Field(),
287
- })(schema => ({
288
- ...schema,
289
- index: 1,
290
- }))
291
- expect(schema.properties?.name).toEqual({
292
- type: 'string',
293
- index: 1,
294
- })
295
- })
296
-
297
- test('proxy helpers', () => {
298
- const f = createFragment()
299
- expect(JSON.stringify(f)).toBe('{}')
300
- expect((f as any).then).toBeUndefined()
301
- expect(typeof f.Field).toBe('function')
302
- })
303
-
304
- test('transform keeps patched properties expanded', () => {
305
- const f = createFragment()
306
- const schema: ISchema = f.transform({
307
- type: 'object',
308
- properties: {
309
- name: { type: 'string' },
310
- },
311
- })
312
- expect(schema.properties?.name).toEqual({ type: 'string' })
313
- })
314
-
315
- test('proxy dispatches every dynamic name through the classification matrix', () => {
316
- const f = createFragment<LooseMap, LooseMap, LooseMap>({
317
- voidComponentNames: ['Card'],
318
- arrayComponentNames: ['ArrayItems'],
319
- })
320
-
321
- // 未登记的动态名默认走 field 通道
322
- expect(f.UnknownWidget({ title: 'x' })()).toEqual({
323
- type: 'string',
324
- title: 'x',
325
- component: 'UnknownWidget',
326
- })
327
- // 登记的 void / array 名走对应通道
328
- expect(f.Card({ title: '布局' })()).toEqual({ type: 'void', title: '布局', component: 'Card' })
329
- expect(f.ArrayItems(f.Field())()).toEqual({
330
- type: 'array',
331
- component: 'ArrayItems',
332
- items: { type: 'string' },
333
- })
334
- // base 构造器不走动态分发
335
- expect(f.Object({})()).toEqual({ type: 'object', properties: {} })
336
- // 非字符串 key 走 Reflect,不产生动态构造器
337
- expect((f as any)[Symbol.iterator]).toBeUndefined()
338
- })
339
-
340
- test('dev warning: same name in void and array lists, array wins', () => {
341
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
342
- createFragment({
343
- voidComponentNames: ['Shared', 'Card'],
344
- arrayComponentNames: ['Shared', 'ArrayItems'],
345
- })
346
-
347
- expect(warn).toHaveBeenCalledTimes(1)
348
- expect(warn.mock.calls[0][0]).toContain('Shared')
349
- expect(warn.mock.calls[0][0]).not.toContain('Card')
350
- warn.mockRestore()
351
- })
352
-
353
- test('no dev warning when void and array lists do not overlap', () => {
354
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
355
- createFragment({ voidComponentNames: ['Card'], arrayComponentNames: ['ArrayItems'] })
356
-
357
- expect(warn).not.toHaveBeenCalled()
358
- warn.mockRestore()
359
- })
360
-
361
- test('dev warning: unregistered dynamic component called with array-style items argument', () => {
362
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
363
- const f = createFragment<LooseMap, LooseMap, LooseMap>()
364
-
365
- // 行为不变:仍然静默产出 field 节点,items creator 被吞掉
366
- const schema = f.ForgottenArray(f.Field())()
367
- expect(schema).toEqual({ type: 'string', component: 'ForgottenArray' })
368
- expect(warn).toHaveBeenCalledTimes(1)
369
- expect(warn.mock.calls[0][0]).toContain('ForgottenArray')
370
-
371
- // 同名再次误用只告警一次
372
- f.ForgottenArray(f.Field())
373
- expect(warn).toHaveBeenCalledTimes(1)
374
-
375
- // 对象入参是正常 field 用法,不告警
376
- f.RegularInput({ title: 'x' })
377
- expect(warn).toHaveBeenCalledTimes(1)
378
- warn.mockRestore()
379
- })
380
- })
@@ -1,325 +0,0 @@
1
- import { isFn } from '@formvk/shared'
2
- import { expect, test } from 'vitest'
3
-
4
- import { Schema } from '..'
5
-
6
- test('has methods', () => {
7
- const schema = new Schema({
8
- type: 'object',
9
- properties: {
10
- aa: {
11
- type: 'string',
12
- },
13
- },
14
- })
15
- expect(isFn(schema.setAdditionalItems)).toBeTruthy()
16
- expect(isFn(schema.setAdditionalProperties)).toBeTruthy()
17
- expect(isFn(schema.setItems)).toBeTruthy()
18
- expect(isFn(schema.setPatternProperties)).toBeTruthy()
19
- expect(isFn(schema.setProperties)).toBeTruthy()
20
- expect(isFn(schema.addPatternProperty)).toBeTruthy()
21
- expect(isFn(schema.addProperty)).toBeTruthy()
22
- expect(isFn(schema.fromJSON)).toBeTruthy()
23
- expect(isFn(schema.toJSON)).toBeTruthy()
24
- expect(isFn(schema.reducePatternProperties)).toBeTruthy()
25
- expect(isFn(schema.reduceProperties)).toBeTruthy()
26
- expect(isFn(schema.removeProperty)).toBeTruthy()
27
- expect(isFn(schema.removePatternProperty)).toBeTruthy()
28
- expect(isFn(schema.mapPatternProperties)).toBeTruthy()
29
- expect(isFn(schema.mapProperties)).toBeTruthy()
30
- expect(isFn(schema.toFieldProps)).toBeTruthy()
31
-
32
- expect(isFn(Schema.isSchemaInstance)).toBeTruthy()
33
- expect(isFn(Schema.registerCompiler)).toBeTruthy()
34
- expect(isFn(Schema.registerPatches)).toBeTruthy()
35
- expect(isFn(Schema.shallowCompile)).toBeTruthy()
36
- expect(isFn(Schema.compile)).toBeTruthy()
37
- expect(isFn(Schema.getOrderProperties)).toBeTruthy()
38
- })
39
-
40
- test('all props with camelCase extension keys', () => {
41
- const schema = new Schema({
42
- type: 'object',
43
- title: 'title',
44
- description: 'description',
45
- patternProperties: {
46
- '^[a-zA-Z0-9]*$': {
47
- properties: {
48
- model: { type: 'string' },
49
- made: { type: 'string' },
50
- year: { type: 'string' },
51
- },
52
- },
53
- },
54
- additionalProperties: {
55
- type: 'string',
56
- },
57
- properties: {
58
- string: {
59
- type: 'string',
60
- default: 'default',
61
- required: true,
62
- component: 'Input',
63
- componentProps: {
64
- placeholder: 'placeholder',
65
- },
66
- decorator: 'FormItem',
67
- decoratorProps: {
68
- labelCol: 3,
69
- },
70
- disabled: true,
71
- display: 'visible',
72
- editable: false,
73
- hidden: false,
74
- pattern: 'readPretty',
75
- readOnly: true,
76
- validator: ['phone'],
77
- reactions: [
78
- {
79
- target: 'xxx',
80
- when: '{{aa > bb}}',
81
- },
82
- ],
83
- },
84
- boolean: {
85
- type: 'boolean',
86
- default: false,
87
- },
88
- number: {
89
- type: 'number',
90
- default: 100,
91
- },
92
- date: {
93
- type: 'date',
94
- default: '2020-12-23',
95
- },
96
- datetime: {
97
- type: 'datetime',
98
- default: '2020-12-23 23:00:00',
99
- },
100
- array: {
101
- type: 'array',
102
- items: {
103
- type: 'string',
104
- },
105
- additionalItems: {
106
- type: 'number',
107
- },
108
- },
109
- array2: {
110
- type: 'array',
111
- items: [
112
- {
113
- type: 'string',
114
- },
115
- {
116
- type: 'object',
117
- },
118
- ],
119
- },
120
- void: {
121
- type: 'void',
122
- },
123
- },
124
- })
125
-
126
- expect(schema.properties?.string.component).toEqual('Input')
127
- expect(schema.properties?.string.componentProps).toEqual({ placeholder: 'placeholder' })
128
- expect(schema.properties?.string.decorator).toEqual('FormItem')
129
- expect(schema.properties?.string.decoratorProps).toEqual({ labelCol: 3 })
130
- expect(schema.properties?.string.pattern).toEqual('readPretty')
131
- expect(schema.properties?.string.validator).toEqual(['phone'])
132
- expect(schema.properties?.string.reactions).toEqual([{ target: 'xxx', when: '{{aa > bb}}' }])
133
- expect(schema.properties?.array.items).toBeInstanceOf(Schema)
134
- expect(schema.properties?.array2.items instanceof Array).toBeTruthy()
135
- expect(schema.properties?.array2.items).toHaveLength(2)
136
- })
137
-
138
- test('all methods', () => {
139
- const schema = new Schema({
140
- type: 'object',
141
- reactions: null as any,
142
- })
143
- const schema2 = new Schema({
144
- type: 'object',
145
- fn: () => {},
146
- } as any)
147
- const schema3 = new Schema({
148
- type: 'object',
149
- additionalItems: null as any,
150
- additionalProperties: null as any,
151
- properties: null as any,
152
- })
153
- schema3.additionalItems = null as any
154
- schema3.additionalProperties = null as any
155
- schema3.properties = {
156
- xxx: null,
157
- } as any
158
- schema3.items = [null] as any
159
- const schema4 = new Schema({
160
- type: 'object',
161
- additionalItems: {},
162
- additionalProperties: {},
163
- properties: {},
164
- })
165
- schema4.additionalItems = {} as any
166
- schema4.additionalProperties = {} as any
167
- schema4.properties = {
168
- xxx: {} as any,
169
- }
170
- schema4.items = [{}] as any
171
- const schema5 = new Schema({
172
- type: 'array',
173
- })
174
- schema5.items = null as any
175
- const schema6 = new Schema({
176
- type: 'array',
177
- })
178
- schema6.items = {} as any
179
- const schema7 = new Schema({
180
- type: 'array',
181
- items: {
182
- type: 'string',
183
- },
184
- })
185
- const string = schema.addProperty('string', {
186
- type: 'string',
187
- title: 'string',
188
- description: null as any,
189
- reactions: [
190
- {
191
- target: 'xxx',
192
- when: true,
193
- fulfill: {
194
- schema: {},
195
- },
196
- },
197
- ],
198
- })
199
-
200
- const array = schema.addProperty('array', {
201
- type: 'string',
202
- title: 'string',
203
- items: [{ type: 'integer' }, { type: 'integer' }],
204
- })
205
- const pattern = schema.addPatternProperty('^[a-zA-Z0-9]*$', {
206
- properties: {
207
- model: { type: 'string', index: 2 },
208
- made: { type: 'string', index: 1 },
209
- year: { type: 'string', index: 0 },
210
- },
211
- })
212
- schema.addPatternProperty('xxx', null as any)
213
- schema.setAdditionalProperties({
214
- type: 'string',
215
- })
216
- schema.setAdditionalProperties(null as any)
217
- array.setItems(null as any)
218
- array.setAdditionalItems({
219
- type: 'string',
220
- })
221
- array.setAdditionalItems(null as any)
222
- schema.setPatternProperties(null as any)
223
- schema.fromJSON(null as any)
224
- expect((schema2 as any)['fn']).toBeUndefined()
225
- expect(schema.properties?.string).not.toBeUndefined()
226
- expect(schema.patternProperties?.['^[a-zA-Z0-9]*$']).not.toBeUndefined()
227
- expect(schema.toJSON()).toBeTruthy()
228
- expect(pattern?.mapProperties((schema, key) => key)).toEqual(['year', 'made', 'model'])
229
- expect(pattern?.reduceProperties((buf, schema, key) => buf.concat('_' + key), [] as string[])).toEqual([
230
- '_year',
231
- '_made',
232
- '_model',
233
- ])
234
- expect(schema.mapPatternProperties((schema, key) => key)).toEqual(['^[a-zA-Z0-9]*$'])
235
- expect(schema.reducePatternProperties((buf, schema, key) => buf.concat('_' + key), [] as string[])).toEqual([
236
- '_^[a-zA-Z0-9]*$',
237
- ])
238
- schema5.toJSON()
239
- schema6.toJSON()
240
- schema7.toJSON()
241
- schema.removeProperty('string')
242
- expect(schema.properties?.string).toBeUndefined()
243
- schema.removePatternProperty('^[a-zA-Z0-9]*$')
244
- expect(schema.patternProperties?.['^[a-zA-Z0-9]*$']).toBeUndefined()
245
- expect(schema.compile()).toBeInstanceOf(Schema)
246
- expect(string?.compile()).toBeInstanceOf(Schema)
247
- expect(schema3.toJSON()).toBeTruthy()
248
- expect(schema4.toJSON()).toBeTruthy()
249
- })
250
-
251
- test('all static methods', () => {
252
- expect(Schema.compile({ aa: '{{123}}' })).toEqual({ aa: 123 })
253
- expect(Schema.shallowCompile('{{123}}')).toEqual(123)
254
- expect(Schema.getOrderProperties()).toEqual([])
255
- Schema.registerPatches(null as any)
256
- })
257
-
258
- test('single function reactions', () => {
259
- function reactions() {
260
- return console.info('reactions')
261
- }
262
- const schema = new Schema({
263
- type: 'string',
264
- reactions,
265
- })
266
- expect(schema.compile()['reactions']).toEqual(reactions)
267
- })
268
-
269
- test('custom extension keys keep xKey camelCase form', () => {
270
- function fn() {
271
- return 'x'
272
- }
273
- const schema = new Schema({
274
- type: 'string',
275
- xCustomThing: {
276
- hello: '{{123}}',
277
- },
278
- xCustomFn: fn,
279
- } as any)
280
- expect(schema.toJSON()['xCustomThing']).toEqual({ hello: '{{123}}' })
281
- expect(schema.toJSON()['xCustomFn']).toEqual(fn)
282
- expect(schema.compile()['xCustomThing']).toEqual({ hello: 123 })
283
- expect(schema.compile()['xCustomFn']).toEqual(fn)
284
- })
285
-
286
- test('definitions and $ref', () => {
287
- const schema = new Schema({
288
- definitions: {
289
- address: {
290
- type: 'object',
291
- properties: {
292
- street_address: {
293
- type: 'string',
294
- },
295
- city: {
296
- type: 'string',
297
- },
298
- state: {
299
- type: 'string',
300
- },
301
- },
302
- required: ['street_address', 'city', 'state'],
303
- },
304
- },
305
- type: 'object',
306
- properties: {
307
- billing_address: {
308
- title: 'Billing address',
309
- $ref: '#/definitions/address',
310
- },
311
- shipping_address: {
312
- title: 'Shipping address',
313
- $ref: '#/definitions/address',
314
- },
315
- },
316
- })
317
- expect(schema.properties?.billing_address.required).toEqual(['street_address', 'city', 'state'])
318
- })
319
-
320
- test('immutable projection of a Schema instance', () => {
321
- const schema = new Schema({ type: 'string' })
322
- expect(new Schema(schema as any)).toBe(schema)
323
- expect(Schema.isSchemaInstance(schema)).toBeTruthy()
324
- expect(Schema.isSchemaInstance({})).toBeFalsy()
325
- })