Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8347: Added ability to skip mask validation #109

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
12 changes: 10 additions & 2 deletions src/process/validation/rules/validateMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -90,8 +98,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) || !value || shouldSkipMaskValidation(context)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something of a nit, but wouldn't the check for skipMaskValidation just be better inside of the isValidatableComponent fn?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. the component is not validatable if it has skipMaskValidation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the isValidatableComponent function to improve readability. The previous implementation with two additional conditions made the function difficult to understand at first look. Is it fine?

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
Loading