Skip to content

Commit

Permalink
Merge pull request #109 from formio/FIO-8347-skip-mask-validation
Browse files Browse the repository at this point in the history
FIO-8347: Added ability to skip mask validation
  • Loading branch information
brendanbond authored Aug 6, 2024
2 parents dbd8da3 + 1bb78d3 commit 583d006
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/process/validation/rules/__tests__/validateMask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,33 @@ 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);
});

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);
});
21 changes: 14 additions & 7 deletions src/process/validation/rules/validateMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ 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;

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) {
Expand Down Expand Up @@ -90,8 +97,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 (!isValidatableComponent(component, instance) || !value) {
return false;
}
if (value == null) {
Expand Down
1 change: 1 addition & 0 deletions src/types/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type TextFieldComponent = BaseComponent & {
maxWords?: number | string;
pattern?: string;
patternMessage?: string;
skipMaskValidation?: boolean;
};
};

Expand Down

0 comments on commit 583d006

Please sign in to comment.