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

refactor: migrate rating validator to ts #724

Merged
merged 2 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions src/app/utils/field-validation/FieldValidatorFactory.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
DecimalValidator,
AttachmentValidator,
DateValidator,
RatingValidator,
} = require('./validators')

const myInfoTypes = require('../../../shared/resources/myinfo').types
Expand Down Expand Up @@ -41,6 +40,8 @@ class FieldValidatorFactory {
case 'textarea': // long text
case 'nric':
case 'homeno':
case 'rating':
throw new Error(`${fieldType} has been migrated to TypeScript`)
case 'radiobutton':
throw new Error(`${fieldType} has been migrated to TypeScript`)
case 'dropdown':
Expand All @@ -53,8 +54,6 @@ class FieldValidatorFactory {
return new TableValidator(...arguments)
case 'number':
return new NumberValidator(...arguments)
case 'rating':
return new RatingValidator(...arguments)
case 'yes_no':
return new YesNoValidator(...arguments)
case 'decimal':
Expand Down
1 change: 0 additions & 1 deletion src/app/utils/field-validation/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const ALLOWED_VALIDATORS = [
'DecimalValidator',
'NumberValidator',
'MobileValidator',
'RatingValidator',
'TextValidator',
'DateValidator',
'TableValidator',
Expand Down
3 changes: 2 additions & 1 deletion src/app/utils/field-validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export const validateField = (
case BasicField.LongText:
case BasicField.Nric:
case BasicField.HomeNo:
case BasicField.Radio: {
case BasicField.Radio:
case BasicField.Rating: {
const validator = constructSingleAnswerValidator(formField)
const validEither = validator(response)
if (isLeft(validEither)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isLongTextField,
isNricField,
isRadioButtonField,
isRatingField,
isSectionField,
isShortTextField,
} from '../../../types/field/utils/guards'
Expand All @@ -15,6 +16,7 @@ import { ProcessedSingleAnswerResponse } from '../../modules/submission/submissi
import { constructHomeNoValidator } from './validators/homeNoValidator'
import { constructNricValidator } from './validators/nricValidator'
import { constructRadioButtonValidator } from './validators/radioButtonValidator'
import { constructRatingValidator } from './validators/ratingValidator'
import { constructSectionValidator } from './validators/sectionValidator'
import constructTextValidator from './validators/textValidator'

Expand All @@ -35,6 +37,8 @@ export const constructSingleAnswerValidator = (
return constructHomeNoValidator(formField)
} else if (isRadioButtonField(formField)) {
return constructRadioButtonValidator(formField)
} else if (isRatingField(formField)) {
return constructRatingValidator(formField)
}
return () => left('Unsupported field type')
}
27 changes: 0 additions & 27 deletions src/app/utils/field-validation/validators/RatingValidator.class.js

This file was deleted.

1 change: 0 additions & 1 deletion src/app/utils/field-validation/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ module.exports.TableValidator = require('./TableValidator.class')
module.exports.TextValidator = require('./TextValidator.class')
module.exports.YesNoValidator = require('./YesNoValidator.class')
module.exports.DateValidator = require('./DateValidator.class')
module.exports.RatingValidator = require('./RatingValidator.class')
module.exports.AttachmentValidator = require('./AttachmentValidator.class')
37 changes: 37 additions & 0 deletions src/app/utils/field-validation/validators/ratingValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { chain, left, right } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
import isInt from 'validator/lib/isInt'

import { IRatingField } from 'src/types/field'
import { ResponseValidator } from 'src/types/field/utils/validation'
import { ISingleAnswerResponse } from 'src/types/response'

import { notEmptySingleAnswerResponse } from './common'

type RatingValidator = ResponseValidator<ISingleAnswerResponse>
type RatingValidatorConstructor = (ratingField: IRatingField) => RatingValidator

const ratingValidator: RatingValidatorConstructor = (ratingField) => (
response,
) => {
const { answer } = response
const { steps } = ratingField.ratingOptions

const isValid = isInt(answer, {
min: 1,
max: steps,
allow_leading_zeroes: false,
})

return isValid
? right(response)
: left(`RatingValidator:\t answer is not a valid rating`)
}

export const constructRatingValidator: RatingValidatorConstructor = (
ratingField,
) => (response) =>
pipe(
notEmptySingleAnswerResponse(response),
chain(ratingValidator(ratingField)),
)
5 changes: 5 additions & 0 deletions src/types/field/utils/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ILongTextField,
INricField,
IRadioField,
IRatingField,
ISectionFieldSchema,
IShortTextField,
} from 'src/types/field'
Expand Down Expand Up @@ -44,3 +45,7 @@ export const isRadioButtonField = (
): formField is IRadioField => {
return formField.fieldType === BasicField.Radio
}

export const isRatingField = (formField: IField): formField is IRatingField => {
return formField.fieldType === BasicField.Rating
}