@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,135 +0,0 @@
1
- import type { Form } from '@formvk/core'
2
- import { createForm } from '@formvk/core'
3
- import { expect, test } from 'vitest'
4
-
5
- import { Schema } from '../schema'
6
- import type { ISchema, SchemaKey } from '../types'
7
-
8
- // 这是 schema
9
- const schemaJson: ISchema = {
10
- type: 'object',
11
- title: 'xxx配置',
12
- properties: {
13
- string: {
14
- type: 'string',
15
- title: 'string',
16
- maxLength: 5,
17
- required: true,
18
- },
19
- number: {
20
- type: 'number',
21
- title: 'number',
22
- required: true,
23
- },
24
- url: {
25
- type: 'string',
26
- title: 'url',
27
- format: 'url',
28
- },
29
- arr: {
30
- type: 'array',
31
- title: 'array',
32
- maxItems: 2,
33
- required: true,
34
- items: {
35
- type: 'object',
36
- properties: {
37
- string: {
38
- type: 'string',
39
- title: 'string',
40
- required: true,
41
- },
42
- },
43
- },
44
- },
45
- },
46
- }
47
-
48
- // 这是需要校验的数据
49
- const schemaData = {
50
- string: '123456', // 超过5个字
51
- // number 字段不存在
52
- url: 'xxxxx', // 不合法的url
53
- arr: [
54
- {
55
- string: '1',
56
- },
57
- {
58
- string: '2',
59
- },
60
- {
61
- // 数组超出2项
62
- string: '', // 没有填
63
- },
64
- ],
65
- }
66
-
67
- function recursiveField(form: Form, schema: ISchema, basePath?: string, name?: SchemaKey) {
68
- const fieldSchema = new Schema(schema)
69
- const fieldProps = fieldSchema.toFieldProps()
70
-
71
- function recursiveProperties(propBasePath?: string) {
72
- fieldSchema.mapProperties((propSchema, propName) => {
73
- recursiveField(form, propSchema, propBasePath, propName)
74
- })
75
- }
76
-
77
- if (name === undefined || name === null) {
78
- recursiveProperties(basePath)
79
- return
80
- }
81
-
82
- if (schema.type === 'object') {
83
- const field = form.createObjectField({
84
- ...fieldProps,
85
- name: name as any,
86
- basePath,
87
- })!
88
-
89
- recursiveProperties(field.address.toString())
90
- } else if (schema.type === 'array') {
91
- const field = form.createArrayField({
92
- ...fieldProps,
93
- name: name as any,
94
- basePath,
95
- })!
96
-
97
- const fieldAddress = field.address.toString()
98
- const fieldValues = form.getValuesIn(fieldAddress)
99
- fieldValues.forEach((_value: any, index: number) => {
100
- if (schema.items) {
101
- const itemsSchema = Array.isArray(schema.items) ? schema.items[index] || schema.items[0] : schema.items
102
-
103
- recursiveField(form, itemsSchema as ISchema, fieldAddress, index)
104
- }
105
- })
106
- } else if (schema.type === 'void') {
107
- const field = form.createVoidField({
108
- ...fieldProps,
109
- name: name as any,
110
- basePath,
111
- })!
112
-
113
- recursiveProperties(field.address.toString())
114
- } else {
115
- form.createField({
116
- ...fieldProps,
117
- name: name as any,
118
- basePath,
119
- })
120
- }
121
- }
122
-
123
- test('server validate', async () => {
124
- const form = createForm({
125
- values: schemaData,
126
- })
127
- recursiveField(form, schemaJson)
128
- let errors: any
129
- try {
130
- await form.validate()
131
- } catch (e) {
132
- errors = e
133
- }
134
- expect(errors).not.toBeUndefined()
135
- })
@@ -1,28 +0,0 @@
1
- import { observable } from '@formvk/reactive'
2
- import { expect, test } from 'vitest'
3
-
4
- import { Schema } from '../schema'
5
- import { createDataSource, isExtensionKey, isNoNeedCompileObject } from '../shared'
6
-
7
- test('isNoNeedCompileObject', () => {
8
- expect(isNoNeedCompileObject({})).toBeFalsy()
9
- expect(isNoNeedCompileObject({ $$typeof: null, _owner: null })).toBeTruthy()
10
- expect(isNoNeedCompileObject({ _isAMomentObject: true })).toBeTruthy()
11
- expect(isNoNeedCompileObject({ [Symbol.for('__REVA_ACTIONS')]: true })).toBeTruthy()
12
- expect(isNoNeedCompileObject({ toJSON: () => {} })).toBeTruthy()
13
- expect(isNoNeedCompileObject({ toJS: () => {} })).toBeTruthy()
14
- expect(isNoNeedCompileObject(observable({}))).toBeTruthy()
15
- expect(isNoNeedCompileObject(new Schema({}))).toBeTruthy()
16
- })
17
-
18
- test('createDataSource', () => {
19
- expect(createDataSource(['111'])).toEqual([{ label: '111', value: '111' }])
20
- expect(createDataSource([{ label: '111', value: '111' }])).toEqual([{ label: '111', value: '111' }])
21
- })
22
-
23
- test('isExtensionKey', () => {
24
- expect(isExtensionKey('xCustomThing')).toBeTruthy()
25
- expect(isExtensionKey('component')).toBeFalsy()
26
- expect(isExtensionKey('x')).toBeFalsy()
27
- expect(isExtensionKey('xyz')).toBeFalsy()
28
- })
@@ -1,531 +0,0 @@
1
- import type { IFieldFactoryProps } from '@formvk/core'
2
- import { createForm } from '@formvk/core'
3
- import { isObservable } from '@formvk/reactive'
4
- import { expect, test, vi } from 'vitest'
5
-
6
- import { Schema } from '../schema'
7
- import type { ISchema, ISchemaTransformerOptions } from '../types'
8
-
9
- function attach<T extends { onMount: () => void }>(target: T | undefined): T {
10
- target?.onMount()
11
- return target as T
12
- }
13
-
14
- function getFormAndFields(
15
- field1SchemaProps: Omit<ISchema, 'name'> = {},
16
- field2SchemaProps: Omit<ISchema, 'name'> = {},
17
- options: ISchemaTransformerOptions = {}
18
- ) {
19
- const filed1Schema: IFieldFactoryProps<any, any> = new Schema({
20
- name: 'field1',
21
- ...field1SchemaProps,
22
- } as any).toFieldProps(options)
23
-
24
- const filed2Schema: IFieldFactoryProps<any, any> = new Schema({
25
- name: 'field2',
26
- ...field2SchemaProps,
27
- } as any).toFieldProps(options)
28
-
29
- const form = createForm()
30
- const field1 = form.createField(filed1Schema)!
31
- const field2 = form.createField(filed2Schema)!
32
-
33
- return {
34
- form,
35
- field1,
36
- field2,
37
- }
38
- }
39
-
40
- test('baseReaction', () => {
41
- const { field1, field2 } = getFormAndFields(
42
- {
43
- title: 'field1Title',
44
- },
45
- {
46
- title: 'field2Title',
47
- }
48
- )
49
-
50
- expect(field1.title).toBe('field1Title')
51
- expect(field2.title).toBe('field2Title')
52
- })
53
-
54
- test('baseReaction with scopes', () => {
55
- const scopeTitle = 'fieldTitle'
56
- const scopeDescription = 'fieldDescription'
57
-
58
- const { field1, field2 } = getFormAndFields(
59
- {
60
- title: '{{scopeTitle}}',
61
- },
62
- {
63
- description: '{{scopeDescription}}',
64
- },
65
- {
66
- scope: {
67
- scopeTitle,
68
- scopeDescription,
69
- },
70
- }
71
- )
72
-
73
- expect(field1.title).toBe(scopeTitle)
74
- expect(field2.description).toBe(scopeDescription)
75
- })
76
-
77
- test('baseReaction maps camelCase extension keys to field state', () => {
78
- const { field1 } = getFormAndFields({
79
- component: 'Input',
80
- componentProps: { placeholder: 'hello' },
81
- decorator: 'FormItem',
82
- decoratorProps: { labelCol: 3 },
83
- display: 'visible',
84
- pattern: 'readPretty',
85
- data: { hello: 'world' },
86
- content: 'content',
87
- })
88
-
89
- expect(field1.componentType).toBe('Input')
90
- expect(field1.componentProps).toEqual({ placeholder: 'hello' })
91
- expect(field1.decoratorType).toBe('FormItem')
92
- expect(field1.decoratorProps).toEqual({ labelCol: 3 })
93
- expect(field1.display).toBe('visible')
94
- expect(field1.pattern).toBe('readPretty')
95
- expect(field1.readPretty).toBe(true)
96
- expect(field1.data).toEqual({ hello: 'world' })
97
- expect(field1.content).toBe('content')
98
- })
99
-
100
- test('standard readOnly maps to field readOnly', () => {
101
- const { field1 } = getFormAndFields({
102
- readOnly: true,
103
- })
104
- expect(field1.readOnly).toBe(true)
105
- })
106
-
107
- test('baseReaction compiles expressions in componentProps / decoratorProps / validator keys', () => {
108
- const { field1 } = getFormAndFields(
109
- {
110
- component: 'Input',
111
- componentProps: {
112
- placeholder: '{{placeholder}}',
113
- nested: { deep: '{{deepValue}}' },
114
- },
115
- decorator: 'FormItem',
116
- decoratorProps: { label: '{{label}}' },
117
- maxLength: '{{maxLength}}',
118
- },
119
- {},
120
- {
121
- scope: {
122
- placeholder: '请输入',
123
- deepValue: 'deep',
124
- label: '姓名',
125
- maxLength: 6,
126
- },
127
- }
128
- )
129
-
130
- expect(field1.componentProps).toEqual({
131
- placeholder: '请输入',
132
- nested: { deep: 'deep' },
133
- })
134
- expect(field1.decoratorProps).toEqual({ label: '姓名' })
135
- expect(field1.validator).toContainEqual({ maxLength: 6 })
136
- })
137
-
138
- test('baseReaction maps index to field order', () => {
139
- const schema = new Schema({
140
- type: 'object',
141
- properties: {
142
- aa: {
143
- name: 'aa',
144
- index: 1,
145
- },
146
- bb: {
147
- name: 'bb',
148
- index: 0,
149
- },
150
- },
151
- })
152
- expect(schema.mapProperties((_schema, key) => key)).toEqual(['bb', 'aa'])
153
- })
154
-
155
- test('userReactions with target(state)', () => {
156
- const field2Title = 'field2Title'
157
- const { field2 } = getFormAndFields({
158
- reactions: {
159
- target: 'field2',
160
- fulfill: {
161
- state: {
162
- title: field2Title,
163
- },
164
- },
165
- },
166
- })
167
-
168
- expect(field2.title).toBe(field2Title)
169
- })
170
-
171
- test('userReactions with target(schema)', () => {
172
- const field2Data = 'fieldData'
173
- const { field2 } = getFormAndFields({
174
- reactions: {
175
- target: 'field2',
176
- fulfill: {
177
- schema: {
178
- data: field2Data,
179
- },
180
- },
181
- },
182
- })
183
-
184
- expect(field2.data).toBe(field2Data)
185
- })
186
-
187
- test('userReactions with target(runner)', () => {
188
- const mockFn = vi.fn()
189
- const field2Title = 'field2Title'
190
- const { field2 } = getFormAndFields(
191
- {
192
- reactions: {
193
- target: 'field2',
194
- fulfill: {
195
- run: `$target.title='${field2Title}';fn()`,
196
- },
197
- },
198
- },
199
- {},
200
- {
201
- scope: {
202
- fn: mockFn,
203
- },
204
- }
205
- )
206
-
207
- expect(mockFn).toBeCalledTimes(1)
208
- expect(field2.title).toBe(field2Title)
209
- })
210
-
211
- test('userReactions without target(state)', () => {
212
- const field1Title = 'field1Title'
213
- const { field1 } = getFormAndFields({
214
- reactions: {
215
- fulfill: {
216
- state: {
217
- title: field1Title,
218
- },
219
- },
220
- },
221
- })
222
-
223
- expect(field1.title).toBe(field1Title)
224
- })
225
-
226
- test('userReactions without target(schema)', () => {
227
- const field1Data = 'fieldData'
228
- const { field1 } = getFormAndFields({
229
- reactions: {
230
- fulfill: {
231
- schema: {
232
- data: field1Data,
233
- },
234
- },
235
- },
236
- })
237
-
238
- expect(field1.data).toBe(field1Data)
239
- })
240
-
241
- test('userReactions without target(runner)', () => {
242
- const mockFn = vi.fn()
243
- const { field1 } = getFormAndFields(
244
- {
245
- reactions: {
246
- fulfill: {
247
- run: `$self.__target__=$target;fn()`,
248
- },
249
- },
250
- },
251
- {},
252
- {
253
- scope: {
254
- fn: mockFn,
255
- },
256
- }
257
- )
258
-
259
- expect(mockFn).toBeCalledTimes(1)
260
- expect((field1 as any).__target__).toBe(null)
261
- })
262
-
263
- test('userReactions with condition', () => {
264
- const mockFn = vi.fn()
265
- const { field1 } = getFormAndFields(
266
- {
267
- value: true,
268
- reactions: {
269
- when: '$self.value===true',
270
- fulfill: {
271
- run: 'mockFn($self.value)',
272
- },
273
- otherwise: {
274
- run: 'mockFn($self.value)',
275
- },
276
- },
277
- },
278
- {},
279
- {
280
- scope: {
281
- mockFn,
282
- },
283
- }
284
- )
285
-
286
- expect(mockFn).nthCalledWith(1, true)
287
-
288
- field1.value = false
289
-
290
- expect(mockFn).nthCalledWith(2, false)
291
- })
292
-
293
- test('userReactions with condition(wrong type)', () => {
294
- const field1Value = 'field1Value'
295
- const mockFn = vi.fn()
296
- getFormAndFields(
297
- {
298
- value: field1Value,
299
- reactions: {
300
- dependencies: 'value' as any,
301
- fulfill: {
302
- run: 'mockFn($deps, $dependencies)',
303
- },
304
- },
305
- },
306
- {},
307
- {
308
- scope: {
309
- mockFn,
310
- },
311
- }
312
- )
313
-
314
- expect(mockFn).nthCalledWith(1, [], [])
315
- })
316
-
317
- test('userReactions with condition(array)', () => {
318
- const field1Value = 'field1Value'
319
- const field2Value = 'field2Value'
320
- const field1Title = 'field1Title'
321
- const field1Description = 'field1Description'
322
- const mockFn = vi.fn()
323
-
324
- getFormAndFields(
325
- {
326
- title: field1Title,
327
- description: field1Description,
328
- value: field1Value,
329
- },
330
- {
331
- value: field2Value,
332
- reactions: {
333
- dependencies: [
334
- 'field2',
335
- {
336
- name: 1 as any,
337
- source: 'field1',
338
- },
339
- {
340
- name: '2',
341
- source: 'field1#title',
342
- },
343
- {
344
- name: '3',
345
- source: 'field1',
346
- property: 'description',
347
- },
348
- ],
349
- fulfill: {
350
- run: `mockFn($deps)`,
351
- },
352
- },
353
- },
354
- {
355
- scope: {
356
- mockFn,
357
- },
358
- }
359
- )
360
-
361
- expect(mockFn).nthCalledWith(1, [field2Value, field1Value, field1Title, field1Description])
362
- })
363
-
364
- test('userReactions with condition(object)', () => {
365
- const field2Value = 'field2Value'
366
- const field1Title = 'field1Title'
367
- const mockFn = vi.fn()
368
-
369
- getFormAndFields(
370
- {
371
- title: field1Title,
372
- },
373
- {
374
- value: field2Value,
375
- reactions: {
376
- dependencies: {
377
- key1: 'field1#title',
378
- key2: 'field2',
379
- },
380
- fulfill: {
381
- run: `mockFn($deps)`,
382
- },
383
- },
384
- },
385
- {
386
- scope: {
387
- mockFn,
388
- },
389
- }
390
- )
391
-
392
- expect(mockFn).nthCalledWith(1, {
393
- key1: field1Title,
394
- key2: field2Value,
395
- })
396
- })
397
-
398
- test('userReactions with user-defined effects', () => {
399
- const field2Value = 'field2Value'
400
- const field1Title = 'field1Title'
401
- const mockFn = vi.fn()
402
-
403
- const { field2 } = getFormAndFields(
404
- {
405
- title: field1Title,
406
- reactions: {
407
- target: 'field2',
408
- fulfill: {
409
- run: `mockFn($target.value)`,
410
- },
411
- effects: ['onFieldInit'],
412
- },
413
- },
414
- {
415
- value: field2Value,
416
- },
417
- {
418
- scope: {
419
- mockFn,
420
- },
421
- }
422
- )
423
-
424
- expect(mockFn).toBeCalledTimes(1)
425
- expect(mockFn).nthCalledWith(1, field2Value)
426
-
427
- field2.value = field1Title
428
- expect(mockFn).toBeCalledTimes(1)
429
- })
430
-
431
- test('userReactions with function type', () => {
432
- const componentProps = {
433
- prop: 1,
434
- }
435
- let observableValue: any = {}
436
- const { field1 } = getFormAndFields({
437
- reactions: (field, baseScope) => {
438
- baseScope.$props(componentProps)
439
- observableValue = baseScope.$observable({})
440
- },
441
- })
442
-
443
- expect(field1.componentProps).toMatchObject(componentProps)
444
- expect(isObservable(observableValue)).toBe(true)
445
- })
446
-
447
- test('userReactions with $lookup $record $records $index', () => {
448
- const initialValues = {
449
- array: [
450
- { a: 1, b: 2 },
451
- { a: 3, b: 4 },
452
- ],
453
- }
454
- const form = attach(
455
- createForm({
456
- initialValues,
457
- })
458
- )
459
-
460
- form.createArrayField({
461
- name: 'array',
462
- })
463
- form.createObjectField({
464
- name: '0',
465
- basePath: 'array',
466
- })
467
- form.createObjectField({
468
- name: '1',
469
- basePath: 'array',
470
- })
471
-
472
- const field0aSchema = new Schema({
473
- name: 'array.0.a',
474
- reactions: `{{$self.title = $record.b}}`,
475
- } as any).toFieldProps({})
476
-
477
- const field0bSchema = new Schema({
478
- name: 'array.0.b',
479
- reactions: '{{$self.title = $lookup.array[0].a}}',
480
- } as any).toFieldProps({})
481
-
482
- const field1aSchema = new Schema({
483
- name: 'array.1.a',
484
- reactions: '{{$self.title = $records[$index].b}}',
485
- } as any).toFieldProps({})
486
-
487
- const field1bSchema = new Schema({
488
- name: 'array.1.b',
489
- reactions: `{{$self.title = $record.$lookup.array[$record.$index].a}}`,
490
- } as any).toFieldProps({})
491
-
492
- const field0a = attach(form.createField(field0aSchema))
493
- const field0b = attach(form.createField(field0bSchema))
494
- const field1a = attach(form.createField(field1aSchema))
495
- const field1b = attach(form.createField(field1bSchema))
496
-
497
- expect(field0a.title).toEqual(2)
498
- expect(field0b.title).toEqual(1)
499
- expect(field1a.title).toEqual(4)
500
- expect(field1b.title).toEqual(3)
501
- })
502
-
503
- test('userReactions with primary type record', () => {
504
- const initialValues = {
505
- array: [1, 2, 3],
506
- }
507
-
508
- const form = attach(
509
- createForm({
510
- initialValues,
511
- })
512
- )
513
-
514
- const field0Schema = new Schema({
515
- name: 'array.0',
516
- reactions: `{{$self.title = $record}}`,
517
- } as any).toFieldProps({})
518
-
519
- const field1Schema = new Schema({
520
- name: 'array.1',
521
- reactions: '{{$self.title = $record}}',
522
- } as any).toFieldProps({})
523
-
524
- form.createArrayField({
525
- name: 'array',
526
- })
527
- const field0 = attach(form.createField(field0Schema))
528
- const field1 = attach(form.createField(field1Schema))
529
- expect(field0.title).toEqual(1)
530
- expect(field1.title).toEqual(2)
531
- })