Skip to content

Commit 5e1e3fb

Browse files
committed
wip
1 parent 5435dff commit 5e1e3fb

File tree

6 files changed

+49
-73
lines changed

6 files changed

+49
-73
lines changed

packages/form-js-editor/src/features/properties-panel/PropertiesProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class PropertiesProvider {
6767
SerializationGroup(field, editField),
6868
ConstraintsGroup(field, editField),
6969
ValidationGroup(field, editField),
70-
CustomValidationsGroup(field, editField),
70+
CustomValidationsGroup(field, editField, getService),
7171
CustomPropertiesGroup(field, editField),
7272
].filter((group) => group != null);
7373

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import {
2-
get,
3-
set
4-
} from 'min-dash';
1+
import { get, set } from 'min-dash';
52

63
import { useService } from '../hooks';
74

85
import { TextFieldEntry, FeelEntry } from '@bpmn-io/properties-panel';
96
import { useCallback } from 'preact/hooks';
107

11-
128
export function CustomValidationEntry(props) {
13-
const {
14-
editField,
15-
field,
16-
idPrefix,
17-
index
18-
} = props;
9+
const { editField, field, idPrefix, index } = props;
1910

2011
const entries = [
2112
{
@@ -24,45 +15,39 @@ export function CustomValidationEntry(props) {
2415
field,
2516
id: idPrefix + '-condition',
2617
idPrefix,
27-
index
18+
index,
2819
},
2920
{
3021
component: Message,
3122
editField,
3223
field,
3324
id: idPrefix + '-message',
3425
idPrefix,
35-
index
36-
}
26+
index,
27+
},
3728
];
3829

3930
return entries;
4031
}
4132

4233
function Condition(props) {
43-
const {
44-
editField,
45-
field,
46-
id,
47-
index
48-
} = props;
34+
const { editField, field, id, index } = props;
4935

5036
const debounce = useService('debounce');
5137

52-
5338
const setValue = (value, error) => {
5439
if (error) {
5540
return;
5641
}
5742

58-
const validate = get(field, [ 'validate' ]);
59-
const newValidate = set(validate, [ 'custom', index, 'condition' ], value);
43+
const validate = get(field, ['validate']);
44+
const newValidate = set(validate, ['custom', index, 'condition'], value);
6045

6146
return editField(field, 'validate', newValidate);
6247
};
6348

6449
const getValue = () => {
65-
return get(field, [ 'validate', 'custom', index, 'condition' ]);
50+
return get(field, ['validate', 'custom', index, 'condition']);
6651
};
6752

6853
const conditionEntryValidate = useCallback((value) => {
@@ -79,17 +64,12 @@ function Condition(props) {
7964
id,
8065
label: 'Condition',
8166
setValue,
82-
validate: conditionEntryValidate
67+
validate: conditionEntryValidate,
8368
});
8469
}
8570

8671
function Message(props) {
87-
const {
88-
editField,
89-
field,
90-
id,
91-
index
92-
} = props;
72+
const { editField, field, id, index } = props;
9373

9474
const debounce = useService('debounce');
9575

@@ -98,14 +78,14 @@ function Message(props) {
9878
return;
9979
}
10080

101-
const validate = get(field, [ 'validate' ]);
102-
const newValidate = set(validate, [ 'custom', index, 'message' ], value);
81+
const validate = get(field, ['validate']);
82+
const newValidate = set(validate, ['custom', index, 'message'], value);
10383

10484
return editField(field, 'validate', newValidate);
10585
};
10686

10787
const getValue = () => {
108-
return get(field, [ 'validate', 'custom', index, 'message' ]);
88+
return get(field, ['validate', 'custom', index, 'message']);
10989
};
11090

11191
const messageEntryValidate = useCallback((value) => {
@@ -121,6 +101,6 @@ function Message(props) {
121101
id,
122102
label: 'Message',
123103
setValue,
124-
validate: messageEntryValidate
104+
validate: messageEntryValidate,
125105
});
126106
}
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import {
2-
get,
3-
set,
4-
without
5-
} from 'min-dash';
1+
import { get, set, without } from 'min-dash';
62

73
import { arrayAdd } from '../Util';
84

9-
import {
10-
ListGroup
11-
} from '@bpmn-io/properties-panel';
5+
import { ListGroup } from '@bpmn-io/properties-panel';
126

137
import { VALIDATION_TYPE_OPTIONS } from './ValidationGroup';
148
import { CustomValidationEntry } from '../entries/CustomValidationEntry';
159

16-
export function CustomValidationsGroup(field, editField) {
10+
export function CustomValidationsGroup(field, editField, getService) {
11+
const validate = get(field, ['validate'], {});
12+
const { validatable, keyed } = getService('formFields').get(field.type).config;
1713

18-
const validate = get(field, [ 'validate' ], {});
19-
const shouldRender = [ undefined, VALIDATION_TYPE_OPTIONS.custom.value ].includes(validate.validationType);
14+
const isValidatable = validatable !== undefined ? validatable : keyed;
15+
const isCustomValidation = [undefined, VALIDATION_TYPE_OPTIONS.custom.value].includes(validate.validationType);
16+
const shouldRender = isValidatable && isCustomValidation;
2017

2118
if (!shouldRender) {
2219
return;
@@ -26,44 +23,38 @@ export function CustomValidationsGroup(field, editField) {
2623
id: 'custom-validation',
2724
label: 'Custom Validations',
2825
component: ListGroup,
29-
...CustomValidationsEntry({ editField, field, id: 'custom-validation-list' })
26+
...CustomValidationsEntry({ editField, field, id: 'custom-validation-list' }),
3027
};
3128
}
3229

33-
3430
export function CustomValidationsEntry(props) {
35-
const {
36-
editField,
37-
field,
38-
id: idPrefix
39-
} = props;
31+
const { editField, field, id: idPrefix } = props;
4032

4133
const addEntry = (e) => {
42-
4334
e.stopPropagation();
4435

45-
const customValidations = get(field, [ 'validate', 'custom' ], []);
36+
const customValidations = get(field, ['validate', 'custom'], []);
4637
const newIndex = customValidations.length + 1;
4738
const newValue = {
4839
condition: 'false',
49-
message: 'Error'
40+
message: 'Error',
5041
};
5142

5243
const newArray = arrayAdd(customValidations, newIndex, newValue);
53-
const newValidate = set(field.validate, [ 'custom' ], newArray);
44+
const newValidate = set(field.validate || {}, ['custom'], newArray);
5445

55-
editField(field, [ 'validate' ], newValidate);
46+
editField(field, ['validate'], newValidate);
5647
};
5748

5849
const removeEntry = (entry) => {
59-
const customValidations = get(field, [ 'validate', 'custom' ], []);
50+
const customValidations = get(field, ['validate', 'custom'], []);
6051
const newArray = without(customValidations, entry);
61-
const newValidate = set(field.validate, [ 'custom' ], newArray);
52+
const newValidate = set(field.validate, ['custom'], newArray);
6253

63-
editField(field, [ 'validate' ], newValidate);
54+
editField(field, ['validate'], newValidate);
6455
};
6556

66-
const items = get(field, [ 'validate', 'custom' ], []).map((entry, index) => {
57+
const items = get(field, ['validate', 'custom'], []).map((entry, index) => {
6758
const id = idPrefix + '-' + index;
6859

6960
return {
@@ -72,15 +63,16 @@ export function CustomValidationsEntry(props) {
7263
editField,
7364
field,
7465
idPrefix,
75-
index
66+
index,
7667
}),
77-
remove: () => removeEntry(entry)
68+
label: 'Rule ' + (index + 1),
69+
remove: () => removeEntry(entry),
7870
};
7971
});
8072

8173
return {
8274
items,
8375
add: addEntry,
84-
shouldSort: false
76+
shouldSort: false,
8577
};
86-
}
78+
}

packages/form-js-viewer/src/core/Validator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ export class Validator {
7979
if ('custom' in evaluatedValidation && value && evaluatedValidation.custom.length) {
8080
const { custom } = evaluatedValidation;
8181
custom.forEach(({ condition, message }) => {
82-
if (condition && !runExpressionEvaluation(this._expressionLanguage, value, expressionContextInfo)) {
82+
if (
83+
condition &&
84+
!runExpressionEvaluation(this._expressionLanguage, condition, { ...expressionContextInfo, value })
85+
) {
8386
errors = [...errors, message];
8487
}
8588
});

packages/form-js-viewer/src/render/components/form-fields/ExpressionField.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ExpressionField.config = {
4545
label: 'Expression',
4646
group: 'basic-input',
4747
keyed: true,
48+
validatable: false,
4849
emptyValue: null,
4950
escapeGridRender: true,
5051
create: (options = {}) => ({

packages/form-js-viewer/src/util/expressions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export function buildExpressionContext(context) {
2222
* If the string is not an expression, it is returned as is.
2323
*
2424
* @param {any} expressionLanguage - The expression language to use.
25-
* @param {string} value - The string to evaluate.
25+
* @param {string} expression - The string expression to evaluate.
2626
* @param {Object} expressionContextInfo - The context information to use.
2727
* @returns {any} - Evaluated value or the original value if not an expression.
2828
*/
29-
export function runExpressionEvaluation(expressionLanguage, value, expressionContextInfo) {
30-
if (expressionLanguage && expressionLanguage.isExpression(value)) {
31-
return expressionLanguage.evaluate(value, buildExpressionContext(expressionContextInfo));
29+
export function runExpressionEvaluation(expressionLanguage, expression, expressionContextInfo) {
30+
if (expressionLanguage && expressionLanguage.isExpression(expression)) {
31+
return expressionLanguage.evaluate(expression, buildExpressionContext(expressionContextInfo));
3232
}
33-
return value;
33+
return expression;
3434
}

0 commit comments

Comments
 (0)