-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use field instance validation in formfields
Closes #1147
- Loading branch information
Showing
6 changed files
with
90 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...es/form-js-viewer/src/features/viewerCommands/cmd/UpdateFieldInstanceValidationHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { set } from 'min-dash'; | ||
import { clone } from '../../../util'; | ||
|
||
export class UpdateFieldInstanceValidationHandler { | ||
constructor(form, validator) { | ||
this._form = form; | ||
this._validator = validator; | ||
} | ||
|
||
execute(context) { | ||
const { fieldInstance, value } = context; | ||
const { id, indexes } = fieldInstance; | ||
const { errors } = this._form._getState(); | ||
|
||
context.oldErrors = clone(errors); | ||
|
||
const fieldErrors = this._validator.validateFieldInstance(fieldInstance, value); | ||
const updatedErrors = set( | ||
errors, | ||
[id, ...Object.values(indexes || {})], | ||
fieldErrors.length ? fieldErrors : undefined, | ||
); | ||
this._form._setState({ errors: updatedErrors }); | ||
} | ||
|
||
revert(context) { | ||
this._form._setState({ errors: context.oldErrors }); | ||
} | ||
} | ||
|
||
UpdateFieldInstanceValidationHandler.$inject = ['form', 'validator']; |
3 changes: 3 additions & 0 deletions
3
packages/form-js-viewer/src/features/viewerCommands/cmd/UpdateFieldValidationHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
import { wrapObjectKeysWithUnderscores } from './simple'; | ||
|
||
|
||
/** | ||
* Transform a LocalExpressionContext object into a usable FEEL context. | ||
* | ||
* @param {Object} context - The LocalExpressionContext object. | ||
* @returns {Object} The usable FEEL context. | ||
*/ | ||
|
||
export function buildExpressionContext(context) { | ||
const { | ||
data, | ||
...specialContextKeys | ||
} = context; | ||
|
||
return { | ||
...specialContextKeys, | ||
...data, | ||
...wrapObjectKeysWithUnderscores(specialContextKeys) | ||
}; | ||
} | ||
|
||
/** | ||
* Evaluate a string based on the expressionLanguage and context information. | ||
* 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 {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)); | ||
} | ||
return value; | ||
} | ||
import { wrapObjectKeysWithUnderscores } from './simple'; | ||
|
||
/** | ||
* Transform a LocalExpressionContext object into a usable FEEL context. | ||
* | ||
* @param {Object} context - The LocalExpressionContext object. | ||
* @returns {Object} The usable FEEL context. | ||
*/ | ||
|
||
export function buildExpressionContext(context) { | ||
const { data, ...specialContextKeys } = context; | ||
|
||
return { | ||
...specialContextKeys, | ||
...data, | ||
...wrapObjectKeysWithUnderscores(specialContextKeys), | ||
}; | ||
} | ||
|
||
/** | ||
* Evaluate a string based on the expressionLanguage and context information. | ||
* 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 {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)); | ||
} | ||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters