From 551a3e700a393c72c3c8ff71081e1cbb9bc95ac8 Mon Sep 17 00:00:00 2001 From: Roman Letsuk Date: Thu, 27 Jun 2024 16:34:05 +0300 Subject: [PATCH 1/3] FIO-8347: added ability to skip mask validation --- .../rules/__tests__/validateMask.test.ts | 14 ++++++++++++++ src/process/validation/rules/validateMask.ts | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/process/validation/rules/__tests__/validateMask.test.ts b/src/process/validation/rules/__tests__/validateMask.test.ts index 3a6ae3ae..5592dc09 100644 --- a/src/process/validation/rules/__tests__/validateMask.test.ts +++ b/src/process/validation/rules/__tests__/validateMask.test.ts @@ -86,3 +86,17 @@ it('Validating a mutil-mask component should return null if the value matches th result = await validateMask(context); expect(result).to.equal(null); }); + +it('Validating a mask component should return null if the instance contains a skipMaskValidation property', async () => { + const component = { ...simpleTextField, inputMask: '999-999-9999' }; + const data = { + component: '1234', + }; + const context = generateProcessorContext(component, data); + let result = await validateMask(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('mask'); + (context as any).instance = { skipMaskValidation: true }; + result = await validateMask(context); + expect(result).to.equal(null); +}); \ No newline at end of file diff --git a/src/process/validation/rules/validateMask.ts b/src/process/validation/rules/validateMask.ts index a30c4ff3..5cfec5eb 100644 --- a/src/process/validation/rules/validateMask.ts +++ b/src/process/validation/rules/validateMask.ts @@ -90,8 +90,8 @@ export function matchInputMask(value: any, inputMask: any) { } export const shouldValidate = (context: ValidationContext) => { - const { component, value } = context; - if (!isValidatableComponent(component) || !value) { + const { component, value, instance } = context; + if ((instance as any)?.skipMaskValidation || !isValidatableComponent(component) || !value) { return false; } if (value == null) { From db399ed55e139ada42937f01406295f733bc6ccd Mon Sep 17 00:00:00 2001 From: Roman Letsuk Date: Wed, 24 Jul 2024 16:13:04 +0300 Subject: [PATCH 2/3] FIO-8347: updated implementation for skipMaskValidation --- .../rules/__tests__/validateMask.test.ts | 18 +++++++++++++++++- src/process/validation/rules/validateMask.ts | 10 +++++++++- src/types/Component.ts | 1 + 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/process/validation/rules/__tests__/validateMask.test.ts b/src/process/validation/rules/__tests__/validateMask.test.ts index 5592dc09..f01d8e19 100644 --- a/src/process/validation/rules/__tests__/validateMask.test.ts +++ b/src/process/validation/rules/__tests__/validateMask.test.ts @@ -99,4 +99,20 @@ it('Validating a mask component should return null if the instance contains a sk (context as any).instance = { skipMaskValidation: true }; result = await validateMask(context); expect(result).to.equal(null); -}); \ No newline at end of file +}); + +it('Validating a mask component should return null if the validate object contains a skipMaskValidation', async () => { + const component = { + ...simpleTextField, + inputMask: '999-999-9999', + validate: { + skipMaskValidation: true, + }, + }; + const data = { + component: '1234', + }; + const context = generateProcessorContext(component, data); + const result = await validateMask(context); + expect(result).to.equal(null); +}); diff --git a/src/process/validation/rules/validateMask.ts b/src/process/validation/rules/validateMask.ts index 5cfec5eb..d4693361 100644 --- a/src/process/validation/rules/validateMask.ts +++ b/src/process/validation/rules/validateMask.ts @@ -21,6 +21,14 @@ const isValidatableComponent = (component: any): component is TextFieldComponent ); }; +// Include instance.skipMaskValidation check to maintain backward compatibility +const shouldSkipMaskValidation = (context: ValidationContext) => { + const { component, instance } = context; + return (component as TextFieldComponent).validate?.skipMaskValidation || + (instance as any)?.skipMaskValidation; +}; + + function getMaskByLabel(component: TextFieldComponent, maskName: string | undefined) { if (maskName) { const inputMask = component.inputMasks?.find((inputMask) => { @@ -91,7 +99,7 @@ export function matchInputMask(value: any, inputMask: any) { export const shouldValidate = (context: ValidationContext) => { const { component, value, instance } = context; - if ((instance as any)?.skipMaskValidation || !isValidatableComponent(component) || !value) { + if (!isValidatableComponent(component) || !value || shouldSkipMaskValidation(context)) { return false; } if (value == null) { diff --git a/src/types/Component.ts b/src/types/Component.ts index 5fec266f..c81139de 100644 --- a/src/types/Component.ts +++ b/src/types/Component.ts @@ -72,6 +72,7 @@ export type TextFieldComponent = BaseComponent & { maxWords?: number | string; pattern?: string; patternMessage?: string; + skipMaskValidation?: boolean; }; }; From 53599f6334c052072fecf8dd54fbb45df649d5cb Mon Sep 17 00:00:00 2001 From: Roman Letsuk Date: Wed, 31 Jul 2024 17:16:54 +0300 Subject: [PATCH 3/3] FIO-8347: moved skip mask validation check to the appropriate method --- src/process/validation/rules/validateMask.ts | 25 ++++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/process/validation/rules/validateMask.ts b/src/process/validation/rules/validateMask.ts index d4693361..c56e11a0 100644 --- a/src/process/validation/rules/validateMask.ts +++ b/src/process/validation/rules/validateMask.ts @@ -13,21 +13,20 @@ const isMaskType = (obj: any): obj is DataObject & { maskName: string; value: st ); }; -const isValidatableComponent = (component: any): component is TextFieldComponent => { +const isValidatableComponent = (component: any, instance: any): component is TextFieldComponent => { + if (!component) return false; + + const { type, inputMask, inputMasks, validate } = component; + // For some reason we skip mask validation for time components - return ((component && component.type && component.type !== 'time') && - (component && component.hasOwnProperty('inputMask') && !!component.inputMask) || - (component && component.hasOwnProperty('inputMasks') && !isEmpty(component.inputMasks)) - ); -}; + if (type === 'time') return false; -// Include instance.skipMaskValidation check to maintain backward compatibility -const shouldSkipMaskValidation = (context: ValidationContext) => { - const { component, instance } = context; - return (component as TextFieldComponent).validate?.skipMaskValidation || - (instance as any)?.skipMaskValidation; -}; + const hasInputMask = inputMask || !isEmpty(inputMasks); + // Include instance.skipMaskValidation check to maintain backward compatibility + const skipMaskValidation = validate?.skipMaskValidation || instance?.skipMaskValidation; + return hasInputMask && !skipMaskValidation; +}; function getMaskByLabel(component: TextFieldComponent, maskName: string | undefined) { if (maskName) { @@ -99,7 +98,7 @@ export function matchInputMask(value: any, inputMask: any) { export const shouldValidate = (context: ValidationContext) => { const { component, value, instance } = context; - if (!isValidatableComponent(component) || !value || shouldSkipMaskValidation(context)) { + if (!isValidatableComponent(component, instance) || !value) { return false; } if (value == null) {