Skip to content

Commit

Permalink
FIO-8037: fixed an issue where number component can be sent text thro…
Browse files Browse the repository at this point in the history
…ugh API
  • Loading branch information
KatrinKhilko committed Mar 27, 2024
1 parent 0b2074c commit 01f0e40
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/process/validation/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export const EN_ERRORS = {
captchaTokenNotSpecified: 'ReCAPTCHA: Token is not specified in submission',
captchaFailure: 'ReCaptcha: Response token not found',
time: '{{field}} is not a valid time.',
number: '{{field}} is not a valid number.'
};
27 changes: 27 additions & 0 deletions src/process/validation/rules/__tests__/validateNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from 'chai';

import { FieldError } from 'error';
import { simpleNumberField } from './fixtures/components';
import { generateProcessorContext } from './fixtures/util';
import { validateNumber } from '../validateNumber';

it('Validating a valid number will return null', async () => {
const component = simpleNumberField;
const data = {
component: 45,
};
const context = generateProcessorContext(component, data);
const result = await validateNumber(context);
expect(result).to.equal(null);
});

it('Validating an invalid number will return a FieldError', async () => {
const component = simpleNumberField;
const data = {
component: 'text',
};
const context = generateProcessorContext(component, data);
const result = await validateNumber(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.contain('number');
});
4 changes: 3 additions & 1 deletion src/process/validation/rules/databaseRules.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ValidationRuleInfo } from "types";
import { validateUniqueInfo } from "./validateUnique";
import { validateCaptchaInfo } from "./validateCaptcha";
import { validateNumberInfo } from "./validateNumber";

// These are the validations that require a database connection.
export const databaseRules: ValidationRuleInfo[] = [
validateUniqueInfo,
validateCaptchaInfo
validateCaptchaInfo,
validateNumberInfo
];
42 changes: 42 additions & 0 deletions src/process/validation/rules/validateNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FieldError } from '../../../error/FieldError';
import { NumberComponent, RuleFn, RuleFnSync, ValidationContext } from '../../../types/index';
import { ProcessorInfo } from 'types/process/ProcessorInfo';

const isValidatableNumberComponent = (component: any): component is NumberComponent => {
return component && component.type === 'number';
};

export const shouldValidate = (context: ValidationContext) => {
const { component, value } = context;
if (!value) {
return false;
}
if (!isValidatableNumberComponent(component)) {
return false;
}
return true;
};

export const validateNumber: RuleFn = async (context: ValidationContext) => {
return validateNumberSync(context);
};

export const validateNumberSync: RuleFnSync = (context: ValidationContext) => {
const error = new FieldError('number', context);
const { value } = context;
if (!shouldValidate(context)) {
return null;
}

if (typeof value !== 'number') {
return error;
}
return null;
};

export const validateNumberInfo: ProcessorInfo<ValidationContext, FieldError | null> = {
name: 'validateNumber',
process: validateNumber,
processSync: validateNumberSync,
shouldProcess: shouldValidate,
};

0 comments on commit 01f0e40

Please sign in to comment.