Add Multiselect for forms (#9092)
- Add new FormMultiSelectField component - Factorize existing display / input into new ui components - Update the variable resolver to handle arrays properly <img width="526" alt="Capture d’écran 2024-12-17 à 11 46 38" src="https://github.com/user-attachments/assets/6d37b513-8caa-43d0-a27e-ab55dac21f6d" />
This commit is contained in:
@ -8,5 +8,4 @@ export class WorkflowExecutorException extends CustomException {
|
||||
|
||||
export enum WorkflowExecutorExceptionCode {
|
||||
WORKFLOW_FAILED = 'WORKFLOW_FAILED',
|
||||
VARIABLE_EVALUATION_FAILED = 'VARIABLE_EVALUATION_FAILED',
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ describe('resolveInput', () => {
|
||||
});
|
||||
|
||||
it('should handle non-existent variables', () => {
|
||||
expect(resolveInput('{{user.email}}', context)).toBe('');
|
||||
expect(resolveInput('{{user.email}}', context)).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should resolve variables in an array', () => {
|
||||
@ -67,15 +67,11 @@ describe('resolveInput', () => {
|
||||
const expected = {
|
||||
user: {
|
||||
displayName: 'John Doe',
|
||||
preferences: ['dark', 'true'],
|
||||
preferences: ['dark', true],
|
||||
},
|
||||
staticData: [1, 2, 3],
|
||||
};
|
||||
|
||||
expect(resolveInput(input, context)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should throw an error for invalid expressions', () => {
|
||||
expect(() => resolveInput('{{invalidFunction()}}', context)).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@ -2,11 +2,6 @@ import { isNil, isString } from '@nestjs/common/utils/shared.utils';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
import {
|
||||
WorkflowExecutorException,
|
||||
WorkflowExecutorExceptionCode,
|
||||
} from 'src/modules/workflow/workflow-executor/exceptions/workflow-executor.exception';
|
||||
|
||||
const VARIABLE_PATTERN = RegExp('\\{\\{(.*?)\\}\\}', 'g');
|
||||
|
||||
export const resolveInput = (
|
||||
@ -81,18 +76,22 @@ const resolveString = (
|
||||
});
|
||||
};
|
||||
|
||||
const evalFromContext = (
|
||||
input: string,
|
||||
context: Record<string, unknown>,
|
||||
): string => {
|
||||
const evalFromContext = (input: string, context: Record<string, unknown>) => {
|
||||
try {
|
||||
const inferredInput = Handlebars.compile(input)(context);
|
||||
Handlebars.registerHelper('json', (input: string) => JSON.stringify(input));
|
||||
|
||||
return inferredInput ?? '';
|
||||
const inputWithHelper = input
|
||||
.replace('{{', '{{{ json ')
|
||||
.replace('}}', ' }}}');
|
||||
|
||||
const inferredInput = Handlebars.compile(inputWithHelper)(context, {
|
||||
helpers: {
|
||||
json: (input: string) => JSON.stringify(input),
|
||||
},
|
||||
});
|
||||
|
||||
return JSON.parse(inferredInput) ?? '';
|
||||
} catch (exception) {
|
||||
throw new WorkflowExecutorException(
|
||||
`Failed to evaluate variable ${input}: ${exception}`,
|
||||
WorkflowExecutorExceptionCode.VARIABLE_EVALUATION_FAILED,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user