@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.
package/src/types.ts DELETED
@@ -1,295 +0,0 @@
1
- import type { FieldValidator, Form, FormPathPattern, GeneralField, IGeneralFieldState } from '@formvk/core'
2
- import type { autorun } from '@formvk/reactive'
3
-
4
- export type SchemaEnum = Array<
5
- | string
6
- | number
7
- | boolean
8
- | { label?: string; value?: any; [key: string]: any }
9
- | { key?: any; title?: string; [key: string]: any }
10
- >
11
-
12
- export type SchemaTypes =
13
- | 'string'
14
- | 'object'
15
- | 'array'
16
- | 'number'
17
- | 'boolean'
18
- | 'void'
19
- | 'date'
20
- | 'datetime'
21
- | (string & {})
22
-
23
- export type SchemaProperties = Record<string, ISchema>
24
-
25
- export type SchemaPatch = (schema: ISchema) => ISchema
26
-
27
- export type SchemaKey = string | number
28
-
29
- export type SchemaEffectTypes =
30
- | 'onFieldInit'
31
- | 'onFieldMount'
32
- | 'onFieldUnmount'
33
- | 'onFieldValueChange'
34
- | 'onFieldInputValueChange'
35
- | 'onFieldInitialValueChange'
36
- | 'onFieldValidateStart'
37
- | 'onFieldValidateEnd'
38
- | 'onFieldValidateFailed'
39
- | 'onFieldValidateSuccess'
40
-
41
- export type SchemaReaction<
42
- Field extends GeneralField = GeneralField,
43
- Scope extends object = {},
44
- IRecord extends object = any,
45
- IValues extends object = any,
46
- > =
47
- | {
48
- dependencies?:
49
- | Array<
50
- | string
51
- | {
52
- name?: string
53
- type?: string
54
- source?: string
55
- property?: string
56
- }
57
- >
58
- | Record<string, string>
59
- when?: string | boolean
60
- target?: string
61
- effects?: (SchemaEffectTypes | (string & {}))[]
62
- fulfill?: {
63
- state?: Stringify<IGeneralFieldState>
64
- schema?: ISchema
65
- run?: string
66
- }
67
- otherwise?: {
68
- state?: Stringify<IGeneralFieldState>
69
- schema?: ISchema
70
- run?: string
71
- }
72
- [key: string]: any
73
- }
74
- | ((field: Field, scope: TScope<Scope, Field, IRecord, IValues>) => void)
75
-
76
- export type SchemaReactions<
77
- Field extends GeneralField = GeneralField,
78
- Scope extends object = {},
79
- IRecord extends object = any,
80
- IValues extends object = any,
81
- > = SchemaReaction<Field, Scope, IRecord, IValues> | SchemaReaction<Field, Scope, IRecord, IValues>[]
82
-
83
- export type SchemaItems = ISchema | ISchema[]
84
-
85
- export type SchemaComponents = Record<string, any>
86
-
87
- export interface ISchemaFieldUpdateRequest {
88
- state?: Stringify<IGeneralFieldState>
89
- schema?: ISchema
90
- run?: string
91
- }
92
-
93
- /**
94
- * reactions / expressions 中内置的动态能力
95
- */
96
- export interface IScopeDefault {
97
- /** 等同 autorun,追踪回调中的响应式依赖 */
98
- $effect: typeof autorun
99
- /** 立即执行回调并返回结果 */
100
- $memo: <T>(callback: () => T) => T
101
- /** 将对象转换为 observable */
102
- $observable: <T extends object>(target: T) => T
103
- }
104
-
105
- /**
106
- * reactions / expressions 中内置的字段上下文
107
- */
108
- export interface IScopeBase<
109
- Field extends GeneralField = GeneralField,
110
- IRecord extends object = any,
111
- IValues extends object = any,
112
- > extends IScopeDefault {
113
- /** 当前字段 */
114
- $self: Field
115
- /** 当前表单 */
116
- $form: Form<IValues>
117
- /** 表单值 */
118
- $values: IValues
119
- /** 当前字段在数组中的下标 */
120
- $index: number
121
- /** 父级记录 */
122
- $record: IRecord & {
123
- $lookup: any
124
- $index: number
125
- }
126
- /** 父级记录数组 */
127
- $records: IRecord[]
128
- /** 设置当前字段的组件属性 */
129
- $props: Field['setComponentProps']
130
- }
131
-
132
- /**
133
- * 全局 scope 扩展点。
134
- * 可通过声明合并全局扩展,所有未显式传入 Scope 泛型的位置都会继承:
135
- *
136
- * declare module '@formvk/schema' {
137
- * interface IScopeContext {
138
- * $myApi: MyApi
139
- * }
140
- * }
141
- */
142
- export interface IScopeContext {}
143
-
144
- /**
145
- * 组合内置 scope、全局扩展与自定义 scope
146
- */
147
- export type TScope<
148
- Scope extends object = {},
149
- Field extends GeneralField = GeneralField,
150
- IRecord extends object = any,
151
- IValues extends object = any,
152
- > = IScopeBase<Field, IRecord, IValues> & IScopeContext & Scope
153
-
154
- export interface IFieldStateSetterOptions {
155
- field: GeneralField
156
- target?: FormPathPattern
157
- request: ISchemaFieldUpdateRequest
158
- runner?: string
159
- scope?: Record<string, any>
160
- }
161
-
162
- /**
163
- * scope 输入:运行时允许传入任意自定义字段。
164
- * 需要类型推断时请使用 createFragment 的 Scope 泛型或 TScope。
165
- */
166
- export interface ISchemaTransformerOptions<Scope extends object = Record<string, any>> {
167
- scope?: Scope
168
- }
169
-
170
- export type Slot = {
171
- target: string
172
- isRenderProp?: boolean
173
- }
174
-
175
- export type SchemaExpression = `{{${string}}}`
176
-
177
- /**
178
- * 递归地把类型 P 的叶子扩展为「原类型 | 表达式」。
179
- * 参考 formily-pro TStringify:
180
- * - 函数:SchemaExpression | P
181
- * - 数组:Stringify<Item>[] | SchemaExpression
182
- * - 对象:逐 key 递归
183
- * - 标量:SchemaExpression | P
184
- */
185
- export type Stringify<P> = P extends (...args: any[]) => any
186
- ? SchemaExpression | P
187
- : P extends readonly any[]
188
- ? Stringify<P[number]>[] | SchemaExpression
189
- : P extends object
190
- ? { [K in keyof P]?: Stringify<P[K]> }
191
- : SchemaExpression | P
192
-
193
- /**
194
- * 组件/装饰器 props 的表达式类型:递归地把每个叶子扩展为「原类型 | 表达式」,整体也允许直接传表达式。
195
- * 运行时 compiler 会编译 componentProps / decoratorProps 内任意层级的 `{{...}}` 字符串叶子。
196
- *
197
- * 与 Stringify 的区别:对象分支采用同态映射,保留原 key 的可选/必填修饰符
198
- * (Stringify 会把所有 key 变可选),避免含必填 props 的组件接口被意外放宽。
199
- */
200
- export type ExpressionProps<P> = P extends (...args: any[]) => any
201
- ? SchemaExpression | P
202
- : P extends object
203
- ? SchemaExpression | { [K in keyof P]: ExpressionProps<P[K]> }
204
- : SchemaExpression | P
205
-
206
- /**
207
- * 所有 schema 节点共享的基础字段。
208
- * 业务可通过 `interface MySchema extends ISchemaBase` 或声明合并扩展字段。
209
- */
210
- export interface ISchemaBase {
211
- title?: Stringify<string>
212
- description?: Stringify<string>
213
- editable?: Stringify<boolean>
214
- disabled?: Stringify<boolean>
215
- readPretty?: Stringify<boolean>
216
- visible?: Stringify<boolean>
217
- hidden?: Stringify<boolean>
218
- display?: Stringify<'visible' | 'hidden' | 'none'>
219
- data?: any
220
- index?: number
221
- content?: any
222
-
223
- /** 第三方自定义扩展字段,使用 xKey 形式 */
224
- [key: `x${string}`]: any
225
- }
226
-
227
- /**
228
- * 标准 JSON Schema 字段保持小驼峰;formily 的 `x-*` 扩展字段统一去掉 x 前缀并使用小驼峰,
229
- * 例如 `x-component` -> `component`、`x-component-props` -> `componentProps`。
230
- * 第三方自定义扩展字段使用 `xKey` 形式,例如 `xCustomThing`。
231
- *
232
- * 注意:`pattern` 表示字段交互模式(editable / readOnly / disabled / readPretty),
233
- * 不再承担标准 JSON Schema 中正则校验的语义,正则校验请使用 `validator`。
234
- */
235
- export interface ISchema extends ISchemaBase {
236
- version?: string
237
- name?: SchemaKey
238
- default?: any
239
- readOnly?: Stringify<boolean>
240
- writeOnly?: Stringify<boolean>
241
- type?: SchemaTypes
242
- enum?: Stringify<SchemaEnum>
243
- const?: any
244
- multipleOf?: Stringify<number>
245
- maximum?: Stringify<number>
246
- exclusiveMaximum?: Stringify<number>
247
- minimum?: Stringify<number>
248
- exclusiveMinimum?: Stringify<number>
249
- maxLength?: Stringify<number>
250
- minLength?: Stringify<number>
251
- pattern?: Stringify<string>
252
- maxItems?: Stringify<number>
253
- minItems?: Stringify<number>
254
- uniqueItems?: Stringify<boolean>
255
- maxProperties?: Stringify<number>
256
- minProperties?: Stringify<number>
257
- required?: Stringify<string[] | boolean | string>
258
- format?: Stringify<string>
259
- $ref?: string
260
- $namespace?: string
261
-
262
- /** 嵌套 JSON Schema 描述 */
263
- definitions?: SchemaProperties
264
- properties?: SchemaProperties
265
- items?: SchemaItems
266
- additionalItems?: ISchema
267
- patternProperties?: SchemaProperties
268
- additionalProperties?: ISchema
269
-
270
- /** 字段值 */
271
- value?: any
272
- /** 校验器,叶子规则同样支持 `{{...}}` 表达式 */
273
- validator?: Stringify<FieldValidator>
274
- /**
275
- * 装饰器引用,支持组件名字符串 / 组件引用 / `{{...}}` 表达式(求值结果为组件名或组件引用)。
276
- * 结构化创建请使用 createFragment 获得按组件 map 收窄的严格类型。
277
- */
278
- decorator?: any
279
- /** 装饰器属性,任意层级的字符串叶子支持 `{{...}}` 表达式 */
280
- decoratorProps?: any
281
- /**
282
- * 组件引用,支持组件名字符串 / 组件引用 / `{{...}}` 表达式(求值结果为组件名或组件引用)。
283
- * 结构化创建请使用 createFragment 获得按组件 map 收窄的严格类型。
284
- */
285
- component?: any
286
- /** 组件属性,任意层级的字符串叶子支持 `{{...}}` 表达式 */
287
- componentProps?: any
288
- /** 组件响应器 */
289
- reactions?: SchemaReactions
290
- /** 指定不做表达式编译的字段 */
291
- compileOmitted?: string[]
292
- slotNode?: Slot
293
- /** 渲染到父组件的具名插槽 */
294
- slot?: string
295
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "paths": {},
5
- "types": []
6
- },
7
- "include": ["./src/**/*.ts"],
8
- "exclude": ["./src/**/__tests__/**", "./__tests__/**"]
9
- }
package/tsconfig.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": ["./src/**/*.ts", "./test/**/*.ts"]
4
- }
package/vite.config.ts DELETED
@@ -1,19 +0,0 @@
1
- import { defineConfig } from 'vite'
2
- import dts from 'vite-plugin-dts'
3
- export default defineConfig({
4
- plugins: [dts({ tsconfigPath: './tsconfig.build.json', entryRoot: 'src', bundleTypes: true })],
5
- build: {
6
- lib: {
7
- entry: 'src/index.ts',
8
- name: 'Formvk.Schema',
9
- fileName: 'index',
10
- formats: ['es'],
11
- },
12
- rollupOptions: {
13
- external: ['@formvk/core', '@formvk/reactive', '@formvk/shared'],
14
- },
15
- sourcemap: true,
16
- minify: false,
17
- target: 'esnext',
18
- },
19
- })