-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathvalidator.js
More file actions
102 lines (93 loc) · 3.13 KB
/
validator.js
File metadata and controls
102 lines (93 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
class Validator {
constructor(node) {
this.node = node;
}
validate() {
throw new Error('Method \'validate()\' must be implemented.');
}
}
export class FormValidator extends Validator {
validate() {
return new Promise((resolve, reject) => {
try {
this.node.validateForm().then(resolve).catch(reject);
} catch (error) {
reject({
errorFields: [{
errors: [error.message],
name: 'node-error',
}],
});
}
});
}
}
export class NormalNodeConnectorValidator extends Validator {
validate() {
const nextEvents = this.node.getNextRunnableEvents();
const i18n = this.node.graph.i18n;
const isConnectionLimitDisabled = Boolean(this.node.graph?.connectionLimitDisabled);
const isValid = isConnectionLimitDisabled ? nextEvents.length >= 1 : nextEvents.length === 1;
if (!isValid) {
return Promise.reject({
errorFields: [{
errors: [`${i18n?.t('node') ?? 'node'} ${this.node.text} ${i18n?.t('problemWithConnection') ?? 'problemWithConnection'}`],
name: 'link-error',
}],
});
}
return Promise.resolve();
}
}
export class ConditionNodeConnectorValidator extends Validator {
validate() {
const nextEvents = this.node.getNextRunnableEvents();
const i18n = this.node.graph.i18n;
if (nextEvents.length !== this.node.getBranches().filter(b => b.runnable).length) {
return Promise.reject({
errorFields: [{
errors: [`${i18n?.t('node') ?? 'node'} ${this.node.text} ${i18n?.t('problemWithConnection') ?? 'problemWithConnection'}`],
name: 'link-error',
}],
});
}
return Promise.resolve();
}
}
export class EndNodeConnectorValidator extends Validator {
validate() {
const nextEvents = this.node.getNextRunnableEvents();
const i18n = this.node.graph.i18n;
if (nextEvents.length !== 0) {
return Promise.reject({
errorFields: [{
errors: [`${i18n?.t('node') ?? 'node'} ${this.node.text} ${i18n?.t('problemWithConnection') ?? 'problemWithConnection'}`],
name: 'link-error',
}],
});
}
return Promise.resolve();
}
}
export class KnowledgeRetrievalValidator extends Validator {
validate() {
const jadeConfig = this.node.drawer.getLatestJadeConfig();
const i18n = this.node.graph.i18n;
// 校验搜索参数.
const option = jadeConfig.inputParams.find(ip => ip.name === 'option');
if (option.value.length === 0) {
return Promise.reject({
errorFields: [{
errors: [`${this.node.text} ${i18n?.t('noSearchOption') ?? 'noSearchOption'}`],
name: 'search-args-error',
}],
});
}
return Promise.resolve();
}
}