-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIO-8037: fixed an issue where number component can be sent text thro…
…ugh API
- Loading branch information
1 parent
0b2074c
commit 01f0e40
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/process/validation/rules/__tests__/validateNumber.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |