Skip to content

Commit

Permalink
FIO-8450: Change validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekotikov committed Jun 26, 2024
1 parent 01ba93e commit 00ab6f0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/process/validation/rules/validateUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
5 changes: 1 addition & 4 deletions src/process/validation/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]+)\]$/);
Expand Down

0 comments on commit 00ab6f0

Please sign in to comment.