From 00ab6f050a3a07b506ae25300021ba37fac2940e Mon Sep 17 00:00:00 2001 From: mikekotikov Date: Wed, 26 Jun 2024 19:34:07 +0300 Subject: [PATCH] FIO-8450: Change validation logic --- src/process/validation/rules/validateUnique.ts | 6 ++++++ src/process/validation/util.ts | 5 +---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/process/validation/rules/validateUnique.ts b/src/process/validation/rules/validateUnique.ts index 7cfe2b4f..7017b3c2 100644 --- a/src/process/validation/rules/validateUnique.ts +++ b/src/process/validation/rules/validateUnique.ts @@ -25,6 +25,12 @@ export const validateUnique: RuleFn = async (context: ValidationContext) => { if (!config || !config.database) { throw new ProcessorError("Can't test for unique value without a database config object", context, 'validate:validateUnique'); } + + // Showing custom unique error message if it is set + if (context?.component?.unique && context?.component?.errors?.unique) { + context.component.validate = {customMessage: context.component.errors!.unique}; + } + try { const isUnique = await config.database?.isUnique(context, value); if (typeof isUnique === 'string') { diff --git a/src/process/validation/util.ts b/src/process/validation/util.ts index 78e665e4..fb6639fb 100644 --- a/src/process/validation/util.ts +++ b/src/process/validation/util.ts @@ -50,9 +50,6 @@ export function isObject(obj: any): obj is Object { return typeof obj != null && (typeof obj === 'object' || typeof obj === 'function'); } -const getCustomErrorMessage = ({ errorKeyOrMessage, context }: FieldError): string => - context.component?.errors?.[errorKeyOrMessage] || ''; - /** * Interpolates @formio/core errors so that they are compatible with the renderer * @param {FieldError[]} errors @@ -63,7 +60,7 @@ export const interpolateErrors = (errors: FieldError[], lang: string = 'en') => return errors.map((error) => { const { errorKeyOrMessage, context } = error; const i18n = VALIDATION_ERRORS[lang] || {}; - const toInterpolate = getCustomErrorMessage(error) || i18n[errorKeyOrMessage] || errorKeyOrMessage; + const toInterpolate = i18n[errorKeyOrMessage] || errorKeyOrMessage; const paths: any = []; context.path.split('.').forEach((part) => { const match = part.match(/\[([0-9]+)\]$/);