Skip to content

Commit

Permalink
feat: custom validation implementation
Browse files Browse the repository at this point in the history
Related to #1162
  • Loading branch information
Skaiir committed Apr 24, 2024
1 parent 950da0c commit f53c6e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 12 additions & 0 deletions packages/form-js-viewer/src/core/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export class Validator {

errors = [...errors, ...runPresetValidation(field, evaluatedValidation, value)];

const evaluateExpression = (expression) =>
runExpressionEvaluation(this._expressionLanguage, expression, { ...expressionContextInfo, value });

if ('custom' in evaluatedValidation && value && evaluatedValidation.custom.length) {
const { custom } = evaluatedValidation;
custom.forEach(({ condition, message }) => {
if (condition && message && !evaluateExpression(condition)) {
errors = [...errors, evaluateExpression(message)];
}
});
}

return errors;
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/form-js-viewer/src/util/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export function buildExpressionContext(context) {
* If the string is not an expression, it is returned as is.
*
* @param {any} expressionLanguage - The expression language to use.
* @param {string} value - The string to evaluate.
* @param {string} expression - The string expression to evaluate.
* @param {Object} expressionContextInfo - The context information to use.
* @returns {any} - Evaluated value or the original value if not an expression.
*/
export function runExpressionEvaluation(expressionLanguage, value, expressionContextInfo) {
if (expressionLanguage && expressionLanguage.isExpression(value)) {
return expressionLanguage.evaluate(value, buildExpressionContext(expressionContextInfo));
export function runExpressionEvaluation(expressionLanguage, expression, expressionContextInfo) {
if (expressionLanguage && expressionLanguage.isExpression(expression)) {
return expressionLanguage.evaluate(expression, buildExpressionContext(expressionContextInfo));
}
return value;
return expression;
}

0 comments on commit f53c6e9

Please sign in to comment.