@zerodep-dev/flow-ts-model 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.
- package/LICENSE +21 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/definition/define.d.ts +32 -0
- package/dist/definition/define.d.ts.map +1 -0
- package/dist/definition/define.js +3 -0
- package/dist/definition/define.js.map +1 -0
- package/dist/definition/definition.d.ts +408 -0
- package/dist/definition/definition.d.ts.map +1 -0
- package/dist/definition/definition.js +32 -0
- package/dist/definition/definition.js.map +1 -0
- package/dist/definition/diff.d.ts +43 -0
- package/dist/definition/diff.d.ts.map +1 -0
- package/dist/definition/diff.js +70 -0
- package/dist/definition/diff.js.map +1 -0
- package/dist/definition/edit.d.ts +56 -0
- package/dist/definition/edit.d.ts.map +1 -0
- package/dist/definition/edit.js +290 -0
- package/dist/definition/edit.js.map +1 -0
- package/dist/definition/gateway-pairs.d.ts +24 -0
- package/dist/definition/gateway-pairs.d.ts.map +1 -0
- package/dist/definition/gateway-pairs.js +133 -0
- package/dist/definition/gateway-pairs.js.map +1 -0
- package/dist/definition/graph.d.ts +24 -0
- package/dist/definition/graph.d.ts.map +1 -0
- package/dist/definition/graph.js +32 -0
- package/dist/definition/graph.js.map +1 -0
- package/dist/definition/schema.d.ts +21 -0
- package/dist/definition/schema.d.ts.map +1 -0
- package/dist/definition/schema.js +33 -0
- package/dist/definition/schema.js.map +1 -0
- package/dist/definition/validate.d.ts +33 -0
- package/dist/definition/validate.d.ts.map +1 -0
- package/dist/definition/validate.js +577 -0
- package/dist/definition/validate.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +62 -0
- package/dist/errors.js.map +1 -0
- package/dist/form/access.d.ts +68 -0
- package/dist/form/access.d.ts.map +1 -0
- package/dist/form/access.js +125 -0
- package/dist/form/access.js.map +1 -0
- package/dist/form/field-values.d.ts +42 -0
- package/dist/form/field-values.d.ts.map +1 -0
- package/dist/form/field-values.js +165 -0
- package/dist/form/field-values.js.map +1 -0
- package/dist/form/form-source.d.ts +8 -0
- package/dist/form/form-source.d.ts.map +1 -0
- package/dist/form/form-source.js +40 -0
- package/dist/form/form-source.js.map +1 -0
- package/dist/form/manifest.d.ts +58 -0
- package/dist/form/manifest.d.ts.map +1 -0
- package/dist/form/manifest.js +22 -0
- package/dist/form/manifest.js.map +1 -0
- package/dist/form/validate-manifest.d.ts +14 -0
- package/dist/form/validate-manifest.d.ts.map +1 -0
- package/dist/form/validate-manifest.js +209 -0
- package/dist/form/validate-manifest.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/project.d.ts +85 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +14 -0
- package/dist/project.js.map +1 -0
- package/dist/routing/condition.d.ts +37 -0
- package/dist/routing/condition.d.ts.map +1 -0
- package/dist/routing/condition.js +136 -0
- package/dist/routing/condition.js.map +1 -0
- package/dist/routing/routing.d.ts +34 -0
- package/dist/routing/routing.d.ts.map +1 -0
- package/dist/routing/routing.js +56 -0
- package/dist/routing/routing.js.map +1 -0
- package/dist/routing/values.d.ts +11 -0
- package/dist/routing/values.d.ts.map +1 -0
- package/dist/routing/values.js +110 -0
- package/dist/routing/values.js.map +1 -0
- package/package.json +50 -3
- package/src/definition/define.ts +38 -0
- package/src/definition/definition.ts +455 -0
- package/src/definition/diff.ts +118 -0
- package/src/definition/edit.ts +369 -0
- package/src/definition/gateway-pairs.ts +159 -0
- package/src/definition/graph.ts +50 -0
- package/src/definition/schema.ts +49 -0
- package/src/definition/validate.ts +684 -0
- package/src/errors.ts +107 -0
- package/src/form/access.ts +204 -0
- package/src/form/field-values.ts +221 -0
- package/src/form/form-source.ts +50 -0
- package/src/form/manifest.ts +88 -0
- package/src/form/validate-manifest.ts +245 -0
- package/src/index.ts +29 -0
- package/src/project.ts +88 -0
- package/src/routing/condition.ts +162 -0
- package/src/routing/routing.ts +73 -0
- package/src/routing/values.ts +132 -0
- package/README.md +0 -3
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字段清单自身的校验(docs/decisions/0004 的 1.2、1.3):表单保存、发布时跑,与 Java 的 `ManifestValidator` 同一套规则,
|
|
3
|
+
* 两边跑 spec/manifests 的样例。问题的形状同流程定义的校验(validate.ts 的 Problem),位置写在 `field` 上。
|
|
4
|
+
*
|
|
5
|
+
* validateManifest(manifest);
|
|
6
|
+
* // → [{ code: 'field-name-invalid', severity: 'error', field: 'Amount' }]
|
|
7
|
+
*/
|
|
8
|
+
import { conditionFields, isConditionComplete, type Condition } from '../routing/condition.ts';
|
|
9
|
+
import { checkFieldValue, typedValue } from './field-values.ts';
|
|
10
|
+
import { manifestField, type FieldDefinition, type FormManifest } from './manifest.ts';
|
|
11
|
+
import type { Problem } from '../definition/validate.ts';
|
|
12
|
+
import { compareValues } from '../routing/values.ts';
|
|
13
|
+
|
|
14
|
+
/** 内置的字段类型 */
|
|
15
|
+
export const BUILTIN_FIELD_TYPES: ReadonlySet<string> = new Set([
|
|
16
|
+
'text',
|
|
17
|
+
'number',
|
|
18
|
+
'decimal',
|
|
19
|
+
'boolean',
|
|
20
|
+
'date',
|
|
21
|
+
'datetime',
|
|
22
|
+
'select',
|
|
23
|
+
'people',
|
|
24
|
+
'file',
|
|
25
|
+
'list',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
/** 字段名的写法:小写字母开头,只有字母和数字,最长 64 */
|
|
29
|
+
const FIELD_NAME = /^[a-z][A-Za-z0-9]{0,63}$/;
|
|
30
|
+
|
|
31
|
+
/** 校验字段清单时能用到的外部信息 */
|
|
32
|
+
export interface ManifestContext {
|
|
33
|
+
/** 接入方注册的字段类型;不给时不认内置以外的类型 */
|
|
34
|
+
readonly isFieldType?: (type: string) => boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 表单条件里能用的 `sys.` 名字(表单的条件在前端算,流程变量不发给浏览器,0004 的 5.5) */
|
|
38
|
+
const SYSTEM_NAMES: ReadonlySet<string> = new Set([
|
|
39
|
+
'initiator',
|
|
40
|
+
'startedAt',
|
|
41
|
+
'instanceId',
|
|
42
|
+
'businessKey',
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
/** 必须是非负整数的属性 */
|
|
46
|
+
const COUNTS = [
|
|
47
|
+
'minLength',
|
|
48
|
+
'maxLength',
|
|
49
|
+
'scale',
|
|
50
|
+
'maxCount',
|
|
51
|
+
'maxSize',
|
|
52
|
+
'minItems',
|
|
53
|
+
'maxItems',
|
|
54
|
+
] as const;
|
|
55
|
+
|
|
56
|
+
/** 校验字段清单 */
|
|
57
|
+
export function validateManifest(manifest: FormManifest, context: ManifestContext = {}): Problem[] {
|
|
58
|
+
const problems: Problem[] = [];
|
|
59
|
+
const report = (code: string, field: string, params?: Record<string, string>) =>
|
|
60
|
+
problems.push({ code, severity: 'error', field, ...(params ? { params } : {}) });
|
|
61
|
+
const visit = (fields: FormManifest, prefix: string, inList: boolean) => {
|
|
62
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
63
|
+
const path = prefix + name;
|
|
64
|
+
checkField(manifest, field, path, name, context, report);
|
|
65
|
+
if (field.type === 'list') {
|
|
66
|
+
if (inList) report('list-nested', path);
|
|
67
|
+
else if (!field.fields || Object.keys(field.fields).length === 0)
|
|
68
|
+
report('list-no-columns', path);
|
|
69
|
+
else visit(field.fields, `${path}.`, true);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
visit(manifest, '', false);
|
|
74
|
+
return problems;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Report = (code: string, field: string, params?: Record<string, string>) => void;
|
|
78
|
+
|
|
79
|
+
function checkField(
|
|
80
|
+
manifest: FormManifest,
|
|
81
|
+
field: FieldDefinition,
|
|
82
|
+
path: string,
|
|
83
|
+
name: string,
|
|
84
|
+
context: ManifestContext,
|
|
85
|
+
report: Report,
|
|
86
|
+
): void {
|
|
87
|
+
if (!FIELD_NAME.test(name)) report('field-name-invalid', path);
|
|
88
|
+
if (typeof field.label !== 'string' || field.label.trim() === '')
|
|
89
|
+
report('field-label-missing', path);
|
|
90
|
+
if (!BUILTIN_FIELD_TYPES.has(field.type) && context.isFieldType?.(field.type) !== true)
|
|
91
|
+
report('field-type-unknown', path, { type: field.type });
|
|
92
|
+
|
|
93
|
+
for (const attribute of COUNTS) {
|
|
94
|
+
const value = field[attribute];
|
|
95
|
+
if (
|
|
96
|
+
value !== undefined &&
|
|
97
|
+
!(typeof value === 'number' && Number.isInteger(value) && value >= 0)
|
|
98
|
+
)
|
|
99
|
+
report('attribute-invalid', path, { attribute });
|
|
100
|
+
}
|
|
101
|
+
if (
|
|
102
|
+
field.type === 'people' &&
|
|
103
|
+
field.max !== undefined &&
|
|
104
|
+
!(typeof field.max === 'number' && Number.isInteger(field.max) && field.max >= 1)
|
|
105
|
+
) {
|
|
106
|
+
report('attribute-invalid', path, { attribute: 'max' });
|
|
107
|
+
}
|
|
108
|
+
if (field.pattern !== undefined && !compiles(field.pattern)) report('pattern-invalid', path);
|
|
109
|
+
checkRanges(field, path, report);
|
|
110
|
+
|
|
111
|
+
if (field.type === 'select' && Array.isArray(field.options)) {
|
|
112
|
+
if (field.options.length === 0) report('options-empty', path);
|
|
113
|
+
const values = field.options.map((o) => o.value);
|
|
114
|
+
for (const value of new Set(values.filter((v, i) => values.indexOf(v) !== i)))
|
|
115
|
+
report('option-duplicate', path, { value: String(value) });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (const which of ['visibleWhen', 'requiredWhen'] as const) {
|
|
119
|
+
const condition = field[which];
|
|
120
|
+
if (condition) checkCondition(manifest, condition, path, which, report);
|
|
121
|
+
}
|
|
122
|
+
if (
|
|
123
|
+
field.default !== undefined &&
|
|
124
|
+
field.default !== null &&
|
|
125
|
+
checkFieldValue(field, typedValue(field, field.default)).length > 0
|
|
126
|
+
) {
|
|
127
|
+
report('default-invalid', path);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function compiles(pattern: string): boolean {
|
|
132
|
+
try {
|
|
133
|
+
new RegExp(pattern, 'u');
|
|
134
|
+
return true;
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 范围的属性:类型对、下限不大于上限 */
|
|
141
|
+
function checkRanges(field: FieldDefinition, path: string, report: Report): void {
|
|
142
|
+
if (
|
|
143
|
+
field.minLength !== undefined &&
|
|
144
|
+
field.maxLength !== undefined &&
|
|
145
|
+
field.minLength > field.maxLength
|
|
146
|
+
)
|
|
147
|
+
report('range-invalid', path, { attribute: 'length' });
|
|
148
|
+
if (
|
|
149
|
+
field.minItems !== undefined &&
|
|
150
|
+
field.maxItems !== undefined &&
|
|
151
|
+
field.minItems > field.maxItems
|
|
152
|
+
)
|
|
153
|
+
report('range-invalid', path, { attribute: 'items' });
|
|
154
|
+
if (!['number', 'decimal', 'date', 'datetime'].includes(field.type)) return;
|
|
155
|
+
// 范围本身要是这种类型的合法值(decimal 的上下限是文字,date 的是日期)
|
|
156
|
+
const plain: FieldDefinition = { label: field.label, type: field.type };
|
|
157
|
+
for (const attribute of ['min', 'max'] as const) {
|
|
158
|
+
const bound = field[attribute];
|
|
159
|
+
if (bound !== undefined && checkFieldValue(plain, typedValue(plain, bound)).length > 0)
|
|
160
|
+
report('attribute-invalid', path, { attribute });
|
|
161
|
+
}
|
|
162
|
+
if (
|
|
163
|
+
field.min !== undefined &&
|
|
164
|
+
field.max !== undefined &&
|
|
165
|
+
compareValues(typedValue(field, field.min), typedValue(field, field.max)) > 0
|
|
166
|
+
) {
|
|
167
|
+
report('range-invalid', path, { attribute: 'value' });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 表单条件:写完整、不用 fn.、只引用表单字段与 sys. 名字 */
|
|
172
|
+
function checkCondition(
|
|
173
|
+
manifest: FormManifest,
|
|
174
|
+
condition: Condition,
|
|
175
|
+
path: string,
|
|
176
|
+
which: string,
|
|
177
|
+
report: Report,
|
|
178
|
+
): void {
|
|
179
|
+
if (!isConditionComplete(condition)) report('condition-incomplete', path, { condition: which });
|
|
180
|
+
for (const ref of conditionFields(condition)) {
|
|
181
|
+
if (ref.startsWith('fn.')) report('condition-fn-in-form', path, { condition: which, ref });
|
|
182
|
+
else if (ref.startsWith('sys.')) {
|
|
183
|
+
if (!SYSTEM_NAMES.has(ref.slice(4))) report('ref-unknown', path, { condition: which, ref });
|
|
184
|
+
} else if (!ref.startsWith('form.') || !manifestField(manifest, ref.slice(5))) {
|
|
185
|
+
report('ref-unknown', path, { condition: which, ref });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const ATTRIBUTE_TEXT: Readonly<Record<string, string>> = {
|
|
191
|
+
minLength: '最少字数',
|
|
192
|
+
maxLength: '最多字数',
|
|
193
|
+
min: '最小值',
|
|
194
|
+
max: '最大值',
|
|
195
|
+
scale: '小数位',
|
|
196
|
+
maxCount: '最多几个',
|
|
197
|
+
maxSize: '文件大小上限',
|
|
198
|
+
minItems: '最少几行',
|
|
199
|
+
maxItems: '最多几行',
|
|
200
|
+
length: '字数范围',
|
|
201
|
+
items: '行数范围',
|
|
202
|
+
value: '取值范围',
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/** 清单问题的业务说法(表单设计器列出来);没有专门说法的给出代号 */
|
|
206
|
+
export function manifestProblemMessage(problem: Problem, manifest?: FormManifest): string {
|
|
207
|
+
const path = problem.field ?? '';
|
|
208
|
+
const label = manifest ? manifestField(manifest, path)?.label : undefined;
|
|
209
|
+
const field = `“${label || path}”`;
|
|
210
|
+
const params = problem.params ?? {};
|
|
211
|
+
const attribute = ATTRIBUTE_TEXT[params.attribute ?? ''] ?? params.attribute ?? '';
|
|
212
|
+
const condition = params.condition === 'requiredWhen' ? '必填条件' : '显示条件';
|
|
213
|
+
switch (problem.code) {
|
|
214
|
+
case 'field-name-invalid':
|
|
215
|
+
return `${field}的字段名要小写字母开头,只用字母和数字`;
|
|
216
|
+
case 'field-label-missing':
|
|
217
|
+
return `${field}还没有名称`;
|
|
218
|
+
case 'field-type-unknown':
|
|
219
|
+
return `${field}的类型“${params.type ?? ''}”不认识`;
|
|
220
|
+
case 'attribute-invalid':
|
|
221
|
+
return `${field}的${attribute}写得不对`;
|
|
222
|
+
case 'range-invalid':
|
|
223
|
+
return `${field}的${attribute}下限比上限大`;
|
|
224
|
+
case 'pattern-invalid':
|
|
225
|
+
return `${field}的格式(正则表达式)写错了`;
|
|
226
|
+
case 'options-empty':
|
|
227
|
+
return `${field}还没有选项`;
|
|
228
|
+
case 'option-duplicate':
|
|
229
|
+
return `${field}有两个选项的值都是“${params.value ?? ''}”`;
|
|
230
|
+
case 'default-invalid':
|
|
231
|
+
return `${field}的默认值不合这个字段`;
|
|
232
|
+
case 'list-nested':
|
|
233
|
+
return `${field}在明细表里,明细表里不能再放明细表`;
|
|
234
|
+
case 'list-no-columns':
|
|
235
|
+
return `明细表${field}还没有列`;
|
|
236
|
+
case 'condition-incomplete':
|
|
237
|
+
return `${field}的${condition}没写完整`;
|
|
238
|
+
case 'condition-fn-in-form':
|
|
239
|
+
return `${field}的${condition}用了函数(${params.ref ?? ''}),表单里只能比较字段`;
|
|
240
|
+
case 'ref-unknown':
|
|
241
|
+
return `${field}的${condition}引用了不存在的“${params.ref ?? ''}”`;
|
|
242
|
+
default:
|
|
243
|
+
return `${field}:${problem.code}`;
|
|
244
|
+
}
|
|
245
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 流程引擎的模型包:定义的类型、条件、值、图、选边、校验、表单权限、差异,前端(设计器、审批面板)与引擎共用,不依赖 vue 与 easy-query。
|
|
3
|
+
* 设计见 docs/decisions/0001-design.md。按职责分在三个文件夹:
|
|
4
|
+
*
|
|
5
|
+
* - definition/:流程定义本身——类型(definition)、格式版本(schema)、在代码里写(define)、图(graph)、分叉与汇合(gateway-pairs)、
|
|
6
|
+
* 校验(validate)、设计器的编辑函数(edit)、两个版本的差异(diff)
|
|
7
|
+
* - routing/:推进时怎么取值、比较、选边——值(values)、条件树(condition)、选边与连线的副作用(routing)
|
|
8
|
+
* - form/:单据——字段清单(manifest)、值的检查(field-values)、清单的校验(validate-manifest)、字段在节点上的权限与收提交(access)、写回 form.ts(form-source)
|
|
9
|
+
*
|
|
10
|
+
* 根目录:业务错误码(errors)、流程即 TS 工程(project)。
|
|
11
|
+
*/
|
|
12
|
+
export * from './definition/definition.ts';
|
|
13
|
+
export * from './definition/schema.ts';
|
|
14
|
+
export * from './definition/define.ts';
|
|
15
|
+
export * from './definition/graph.ts';
|
|
16
|
+
export * from './definition/gateway-pairs.ts';
|
|
17
|
+
export * from './definition/validate.ts';
|
|
18
|
+
export * from './definition/edit.ts';
|
|
19
|
+
export * from './definition/diff.ts';
|
|
20
|
+
export * from './routing/values.ts';
|
|
21
|
+
export * from './routing/condition.ts';
|
|
22
|
+
export * from './routing/routing.ts';
|
|
23
|
+
export * from './form/manifest.ts';
|
|
24
|
+
export * from './form/field-values.ts';
|
|
25
|
+
export * from './form/validate-manifest.ts';
|
|
26
|
+
export * from './form/access.ts';
|
|
27
|
+
export * from './form/form-source.ts';
|
|
28
|
+
export * from './errors.ts';
|
|
29
|
+
export * from './project.ts';
|
package/src/project.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 流程即一个小 TS 工程(docs/decisions/0002-projects.md):在线编辑、仓库里写的都是这几份文件——
|
|
3
|
+
*
|
|
4
|
+
* process.json 图:节点、连线、泳道、布局(设计器画的)
|
|
5
|
+
* form.ts 单据:`export default defineForm({ 字段: { label, type, … } })`,就是字段清单(manifest.ts,0004-forms.md),
|
|
6
|
+
* 推出单据的类型;发布时内联进定义的 form.manifest
|
|
7
|
+
* logic.ts 逻辑:`export default defineLogic(form, { resolvers, guards, actions, functions })`,
|
|
8
|
+
* 定义里写 `logic.名字` 引用
|
|
9
|
+
* views/*.tsx 需要时才写,在页面里渲染(引擎不加载):start.tsx 是发起页的分区,print.tsx 是打印页,别的是审批单的分区;
|
|
10
|
+
* 默认导出组件(属性是 FlowViewProps),`export const title` 是分区的标题。只 import 单据、别的视图与页面登记的模块
|
|
11
|
+
* test.ts 场景测试
|
|
12
|
+
*
|
|
13
|
+
* 文件之间相对路径 import;宿主登记的模块(组件库、zod、业务服务……)按名字 import。
|
|
14
|
+
*/
|
|
15
|
+
import type { OrgId } from './definition/definition.ts';
|
|
16
|
+
import type { FormManifest } from './form/manifest.ts';
|
|
17
|
+
|
|
18
|
+
/** 一个流程工程:路径 → 源码;`process.json` 必须有 */
|
|
19
|
+
export interface FlowProject {
|
|
20
|
+
readonly files: Readonly<Record<string, string>>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 约定的文件 */
|
|
24
|
+
export const PROJECT_FILES = {
|
|
25
|
+
process: 'process.json',
|
|
26
|
+
form: 'form.ts',
|
|
27
|
+
logic: 'logic.ts',
|
|
28
|
+
test: 'test.ts',
|
|
29
|
+
/** 视图的目录 */
|
|
30
|
+
views: 'views/',
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
/** 视图文件(views/ 下的):在页面里渲染,引擎不加载 */
|
|
34
|
+
export const isViewFile = (path: string): boolean => path.startsWith(PROJECT_FILES.views);
|
|
35
|
+
|
|
36
|
+
/** 视图(views/*.tsx 默认导出的组件)拿到的属性 */
|
|
37
|
+
export interface FlowViewProps<V extends object = Record<string, unknown>> {
|
|
38
|
+
/** 单据的值(发起页上是正在填的) */
|
|
39
|
+
readonly variables: Readonly<Partial<V>>;
|
|
40
|
+
/** 这张单;发起页上为空 */
|
|
41
|
+
readonly instance: {
|
|
42
|
+
readonly id: number;
|
|
43
|
+
readonly title: string | null;
|
|
44
|
+
readonly status: string;
|
|
45
|
+
/** 正在处理的节点 */
|
|
46
|
+
readonly current: readonly string[];
|
|
47
|
+
} | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 写单据:原样返回字段清单的字面量,字面量类型留着(推出 FormValues);表单设计器读写的就是它 */
|
|
51
|
+
export const defineForm = <const M extends FormManifest>(manifest: M): M => manifest;
|
|
52
|
+
|
|
53
|
+
/** 字段类型 → 值(小数、日期、时刻是文字,同线上的写法;人员是编号) */
|
|
54
|
+
export interface FieldValues {
|
|
55
|
+
text: string;
|
|
56
|
+
number: number;
|
|
57
|
+
decimal: string;
|
|
58
|
+
boolean: boolean;
|
|
59
|
+
date: string;
|
|
60
|
+
datetime: string;
|
|
61
|
+
select: string;
|
|
62
|
+
people: OrgId[];
|
|
63
|
+
file: { readonly name: string; readonly url: string; readonly size?: number }[];
|
|
64
|
+
list: unknown[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 一个字段的值:多选是数组,明细表是行(每行按列推出) */
|
|
68
|
+
type FieldValue<F> = F extends {
|
|
69
|
+
readonly type: 'list';
|
|
70
|
+
readonly fields: infer C extends FormManifest;
|
|
71
|
+
}
|
|
72
|
+
? FormValues<C>[]
|
|
73
|
+
: F extends { readonly type: 'select'; readonly multiple: true }
|
|
74
|
+
? string[]
|
|
75
|
+
: F extends { readonly type: infer T extends keyof FieldValues }
|
|
76
|
+
? FieldValues[T]
|
|
77
|
+
: unknown;
|
|
78
|
+
|
|
79
|
+
/** 单据的值:必填的字段一定有,其余可以没有 */
|
|
80
|
+
export type FormValues<M extends FormManifest> = {
|
|
81
|
+
-readonly [K in keyof M as M[K] extends { readonly required: true } ? K : never]: FieldValue<
|
|
82
|
+
M[K]
|
|
83
|
+
>;
|
|
84
|
+
} & {
|
|
85
|
+
-readonly [K in keyof M as M[K] extends { readonly required: true } ? never : K]?: FieldValue<
|
|
86
|
+
M[K]
|
|
87
|
+
>;
|
|
88
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 条件树:比较一个字段,或用 all / any / not 组合。与组件库的 `Condition` 同形(`{ field, op, value }`),连线条件、
|
|
3
|
+
* 表单的显示与必填条件(`visibleWhen`、`requiredWhen`)都用它(0005 的 6.2、0004 的 1.3)。值怎样比见 values.ts。
|
|
4
|
+
*
|
|
5
|
+
* evaluateCondition({ all: [{ field: 'form.amount', op: 'gt', value: '10000' }] }, (field) => scope.read(field));
|
|
6
|
+
*
|
|
7
|
+
* 字段的值是数组时(多选、人员字段的编号列表),比较对数组里的每一项做:等于、是其中之一、包含、大小这些是"有一项满足",
|
|
8
|
+
* 不等于、不在其中、不包含是"没有一项满足";有值、没有值看数组是不是空的。
|
|
9
|
+
*/
|
|
10
|
+
import { compareValues, isBlank, sameValue, toText } from './values.ts';
|
|
11
|
+
|
|
12
|
+
/** 比较的方式,同组件库的 14 种 */
|
|
13
|
+
export const CONDITION_OPS = [
|
|
14
|
+
'eq',
|
|
15
|
+
'ne',
|
|
16
|
+
'gt',
|
|
17
|
+
'gte',
|
|
18
|
+
'lt',
|
|
19
|
+
'lte',
|
|
20
|
+
'between',
|
|
21
|
+
'in',
|
|
22
|
+
'notIn',
|
|
23
|
+
'contains',
|
|
24
|
+
'notContains',
|
|
25
|
+
'startsWith',
|
|
26
|
+
'exists',
|
|
27
|
+
'empty',
|
|
28
|
+
] as const;
|
|
29
|
+
|
|
30
|
+
/** 比较的方式 */
|
|
31
|
+
export type ConditionOp = (typeof CONDITION_OPS)[number];
|
|
32
|
+
|
|
33
|
+
/** 比较一个字段 */
|
|
34
|
+
export interface ConditionRule {
|
|
35
|
+
/** 字段名:`form.字段`、`sys.名字`、流程变量名、`fn.名字`(0005 的 1.1) */
|
|
36
|
+
readonly field: string;
|
|
37
|
+
/** 比较的方式 */
|
|
38
|
+
readonly op: ConditionOp;
|
|
39
|
+
/** 比较的值;`between` 是 `[下限, 上限]`,`in` / `notIn` 是数组,`exists` / `empty` 没有 */
|
|
40
|
+
readonly value?: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 条件:一条比较,或 all(全部满足)/ any(任一满足)/ not(不满足)组合 */
|
|
44
|
+
export type Condition =
|
|
45
|
+
| ConditionRule
|
|
46
|
+
| { readonly all: readonly Condition[] }
|
|
47
|
+
| { readonly any: readonly Condition[] }
|
|
48
|
+
| { readonly not: Condition };
|
|
49
|
+
|
|
50
|
+
/** 按字段名取值 */
|
|
51
|
+
export type ConditionReader = (field: string) => unknown;
|
|
52
|
+
|
|
53
|
+
/** 这种比较要不要值 */
|
|
54
|
+
export const needsValue = (op: ConditionOp): boolean => op !== 'exists' && op !== 'empty';
|
|
55
|
+
|
|
56
|
+
/** 一条比较已经写完整:要值的有值,`between` 至少一端有值 */
|
|
57
|
+
export function isRuleComplete(rule: ConditionRule): boolean {
|
|
58
|
+
if (!needsValue(rule.op)) return true;
|
|
59
|
+
if (rule.op === 'between')
|
|
60
|
+
return Array.isArray(rule.value) && rule.value.some((end) => !isBlank(end));
|
|
61
|
+
return !isBlank(rule.value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 整棵树都写完整:没有没写完的比较、没有空的组(流程里发布时要求这样,0005 的 6.2) */
|
|
65
|
+
export function isConditionComplete(condition: Condition): boolean {
|
|
66
|
+
if ('not' in condition) return isConditionComplete(condition.not);
|
|
67
|
+
if ('all' in condition || 'any' in condition) {
|
|
68
|
+
const items = 'all' in condition ? condition.all : condition.any;
|
|
69
|
+
return items.length > 0 && items.every(isConditionComplete);
|
|
70
|
+
}
|
|
71
|
+
return isRuleComplete(condition);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 条件里用到的字段名,按出现的先后、不重复 */
|
|
75
|
+
export function conditionFields(condition: Condition): string[] {
|
|
76
|
+
const fields = new Set<string>();
|
|
77
|
+
const visit = (node: Condition): void => {
|
|
78
|
+
if ('not' in node) visit(node.not);
|
|
79
|
+
else if ('all' in node) node.all.forEach(visit);
|
|
80
|
+
else if ('any' in node) node.any.forEach(visit);
|
|
81
|
+
else fields.add(node.field);
|
|
82
|
+
};
|
|
83
|
+
visit(condition);
|
|
84
|
+
return [...fields];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 条件成不成立。没写完的比较、空的组不加条件(同组件库与 query 的 conditionBy),整棵树都不加条件时成立;
|
|
89
|
+
* 流程定义在发布时就拒绝这样的条件,所以这条规则只在筛选这类场合起作用。
|
|
90
|
+
*/
|
|
91
|
+
export function evaluateCondition(condition: Condition, read: ConditionReader): boolean {
|
|
92
|
+
return evaluate(condition, read) ?? true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function evaluate(condition: Condition, read: ConditionReader): boolean | undefined {
|
|
96
|
+
if ('not' in condition) {
|
|
97
|
+
const inner = evaluate(condition.not, read);
|
|
98
|
+
return inner === undefined ? undefined : !inner;
|
|
99
|
+
}
|
|
100
|
+
if ('all' in condition || 'any' in condition) {
|
|
101
|
+
const results = ('all' in condition ? condition.all : condition.any)
|
|
102
|
+
.map((item) => evaluate(item, read))
|
|
103
|
+
.filter((result): result is boolean => result !== undefined);
|
|
104
|
+
if (results.length === 0) return undefined;
|
|
105
|
+
return 'all' in condition ? results.every(Boolean) : results.some(Boolean);
|
|
106
|
+
}
|
|
107
|
+
if (!isRuleComplete(condition)) return undefined;
|
|
108
|
+
return matchesRule(condition, read(condition.field));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const lower = (value: unknown) => toText(value).toLowerCase();
|
|
112
|
+
|
|
113
|
+
/** 对一个(不是数组的)值,这条比较成不成立 */
|
|
114
|
+
function matchesOne(op: ConditionOp, actual: unknown, value: unknown): boolean {
|
|
115
|
+
switch (op) {
|
|
116
|
+
case 'eq':
|
|
117
|
+
return sameValue(actual, value);
|
|
118
|
+
case 'ne':
|
|
119
|
+
return !sameValue(actual, value);
|
|
120
|
+
case 'gt':
|
|
121
|
+
return actual != null && compareValues(actual, value) > 0;
|
|
122
|
+
case 'gte':
|
|
123
|
+
return actual != null && compareValues(actual, value) >= 0;
|
|
124
|
+
case 'lt':
|
|
125
|
+
return actual != null && compareValues(actual, value) < 0;
|
|
126
|
+
case 'lte':
|
|
127
|
+
return actual != null && compareValues(actual, value) <= 0;
|
|
128
|
+
case 'between': {
|
|
129
|
+
const [low, high] = Array.isArray(value) ? value : [];
|
|
130
|
+
if (actual == null) return false;
|
|
131
|
+
return (
|
|
132
|
+
(isBlank(low) || compareValues(actual, low) >= 0) &&
|
|
133
|
+
(isBlank(high) || compareValues(actual, high) <= 0)
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
case 'in':
|
|
137
|
+
return Array.isArray(value) && value.some((item) => sameValue(actual, item));
|
|
138
|
+
case 'notIn':
|
|
139
|
+
return !Array.isArray(value) || !value.some((item) => sameValue(actual, item));
|
|
140
|
+
case 'contains':
|
|
141
|
+
return lower(actual).includes(lower(value));
|
|
142
|
+
case 'notContains':
|
|
143
|
+
return !lower(actual).includes(lower(value));
|
|
144
|
+
case 'startsWith':
|
|
145
|
+
return lower(actual).startsWith(lower(value));
|
|
146
|
+
case 'exists':
|
|
147
|
+
return !isBlank(actual);
|
|
148
|
+
case 'empty':
|
|
149
|
+
return isBlank(actual);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** "没有一项满足"的比较:数组时要求每一项都满足对应的肯定比较不成立 */
|
|
154
|
+
const NEGATIVE: ReadonlySet<ConditionOp> = new Set(['ne', 'notIn', 'notContains']);
|
|
155
|
+
|
|
156
|
+
function matchesRule(rule: ConditionRule, actual: unknown): boolean {
|
|
157
|
+
if (rule.op === 'exists' || rule.op === 'empty' || !Array.isArray(actual))
|
|
158
|
+
return matchesOne(rule.op, actual, rule.value);
|
|
159
|
+
return NEGATIVE.has(rule.op)
|
|
160
|
+
? actual.every((item) => matchesOne(rule.op, item, rule.value))
|
|
161
|
+
: actual.some((item) => matchesOne(rule.op, item, rule.value));
|
|
162
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 选出边与连线的副作用(docs/decisions/0005 第 6 节):引擎推进时用,前端预览"会走到哪些节点"时也用(0005 的 5.6)。
|
|
3
|
+
* 与 Java 的 `Routing` 同一套规则,两边跑 spec/routing 的样例。
|
|
4
|
+
*
|
|
5
|
+
* selectEdges(graph, 'review', { outcome: 'pass', read }); // → 走哪几条线
|
|
6
|
+
* applyEffects(edge.effects ?? [], variables, read); // → 走过这条线后的流程变量
|
|
7
|
+
*/
|
|
8
|
+
import { evaluateCondition, type ConditionReader } from './condition.ts';
|
|
9
|
+
import { isRef, type Effect, type ProcessEdge } from '../definition/definition.ts';
|
|
10
|
+
import type { ProcessGraph } from '../definition/graph.ts';
|
|
11
|
+
import { decimalText } from './values.ts';
|
|
12
|
+
|
|
13
|
+
/** 选边时的输入 */
|
|
14
|
+
export interface RouteInput {
|
|
15
|
+
/** 节点给出的结果:人工节点的处理结果、自动节点与子流程返回的、外部事件带回的;网关没有 */
|
|
16
|
+
readonly outcome?: string | null;
|
|
17
|
+
/** 条件里按字段名取值 */
|
|
18
|
+
readonly read: ConditionReader;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 从一个节点走哪几条线。
|
|
23
|
+
* - 并行网关:全部出边(条件不起作用,校验会提示);
|
|
24
|
+
* - 包容网关:条件成立的都走,都不成立时走默认边;
|
|
25
|
+
* - 其余:条件成立的边里,先取写明了这个结果的,再取没写结果的,都没有时走默认边——最多一条(沿用旧契约的 nextProcessEdge)。
|
|
26
|
+
* 一条也选不出时返回空数组:定义有漏洞(校验会报出来),或者结果没有对应的出边。
|
|
27
|
+
*/
|
|
28
|
+
export function selectEdges(graph: ProcessGraph, nodeId: string, input: RouteInput): ProcessEdge[] {
|
|
29
|
+
const edges = graph.outgoing(nodeId);
|
|
30
|
+
const type = graph.node(nodeId)?.type;
|
|
31
|
+
if (type === 'parallel') return [...edges];
|
|
32
|
+
const outcome = input.outcome ?? null;
|
|
33
|
+
const fits = (edge: ProcessEdge) => edge.outcome === undefined || edge.outcome === outcome;
|
|
34
|
+
const holds = (edge: ProcessEdge) =>
|
|
35
|
+
edge.when === undefined || evaluateCondition(edge.when, input.read);
|
|
36
|
+
const candidates = edges.filter((edge) => fits(edge) && !edge.default && holds(edge));
|
|
37
|
+
const fallback = edges.filter((edge) => edge.default && fits(edge));
|
|
38
|
+
if (type === 'inclusive') return candidates.length > 0 ? candidates : fallback.slice(0, 1);
|
|
39
|
+
const chosen =
|
|
40
|
+
candidates.find((edge) => edge.outcome !== undefined && edge.outcome === outcome) ??
|
|
41
|
+
candidates[0] ??
|
|
42
|
+
fallback[0];
|
|
43
|
+
return chosen ? [chosen] : [];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 流程变量 */
|
|
47
|
+
export type Variables = Readonly<Record<string, unknown>>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 按顺序应用连线的副作用,返回新的流程变量(不改原对象)。
|
|
51
|
+
* - `set`:值是引用时用 read 取;
|
|
52
|
+
* - `increment`:没有、或不是数字的当作 0;按精确的小数加(`0.1 + 0.2` 是 `0.3`),结果是整数时写成数字,否则写成小数文字。
|
|
53
|
+
*/
|
|
54
|
+
export function applyEffects(
|
|
55
|
+
effects: readonly Effect[],
|
|
56
|
+
variables: Variables,
|
|
57
|
+
read: ConditionReader,
|
|
58
|
+
): Record<string, unknown> {
|
|
59
|
+
const next: Record<string, unknown> = { ...variables };
|
|
60
|
+
for (const effect of effects) {
|
|
61
|
+
if ('set' in effect)
|
|
62
|
+
next[effect.set] = isRef(effect.value) ? read(effect.value.ref) : effect.value;
|
|
63
|
+
else next[effect.increment] = addNumbers(next[effect.increment], effect.by ?? 1);
|
|
64
|
+
}
|
|
65
|
+
return next;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 两个数精确相加:不是数字的当作 0;结果放得进安全整数时是 number,否则是小数文字 */
|
|
69
|
+
function addNumbers(current: unknown, by: number): number | string {
|
|
70
|
+
const sum = decimalText(current, by);
|
|
71
|
+
const asNumber = Number(sum);
|
|
72
|
+
return /^-?\d+$/.test(sum) && Number.isSafeInteger(asNumber) ? asNumber : sum;
|
|
73
|
+
}
|