Skip to content

Commit

Permalink
checkpoint for BE validation
Browse files Browse the repository at this point in the history
  • Loading branch information
scottheng96 committed Dec 11, 2024
1 parent 3ccb9e9 commit dd32a67
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion frontend/src/features/public-form/PublicFormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ const transformFormInputCountryRegionToUpperCase =
.filter((field) => field.fieldType === BasicField.CountryRegion)
.map((field) => field._id),
)

return Object.keys(formInputs).reduce(
(newFormInputs: typeof formInputs, fieldId) => {
const currentInput = formInputs[fieldId]
Expand Down
1 change: 0 additions & 1 deletion frontend/src/features/public-form/PublicFormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ export const submitStorageModeForm = async ({
formInputs,
formLogics,
})

const formData = createClearSubmissionWithVirusScanningFormData(
{
formFields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const PublicFormSubmitButton = ({
const isMobile = useIsMobile()
const { isSubmitting } = useFormState()
const formInputs = useWatch<FormFieldValues>({}) as FormFieldValues
console.log(formFields)
console.log(formInputs)
const {
formId,
isPaymentEnabled,
Expand Down
29 changes: 18 additions & 11 deletions frontend/src/features/public-form/utils/createSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ const createResponsesV3 = (
case BasicField.Uen:
case BasicField.Date:
case BasicField.CountryRegion:
case BasicField.Address: // for now treating as a postalCode single response
case BasicField.YesNo: {
const input = formInputs[ff._id] as
| FormFieldValue<typeof ff.fieldType>
Expand All @@ -282,16 +281,24 @@ const createResponsesV3 = (
} as FieldResponseV3
break
}
// case BasicField.Address:
// const input = formInputs[ff._id] as
// | FormFieldValue<typeof ff.fieldType>
// | undefined
// if (!input) break
// returnedInputs[ff._id] = {
// fieldType: ff.fieldType,
// answer: input,
// } as FieldResponseV3
// break
case BasicField.Address: {
const input = formInputs[ff._id] as
| FormFieldValue<typeof ff.fieldType>
| undefined
if (!input) break
returnedInputs[ff._id] = {
fieldType: ff.fieldType,
answer: {
postalCode: input.postalCode,
blockNumber: input.blockNumber,
streetName: input.streetName,
buildingName: input.buildingName,
levelNumber: input.levelNumber,
unitNumber: input.unitNumber,
},
} as FieldResponseV3
break
}
case BasicField.Email:
case BasicField.Mobile: {
const input = formInputs[ff._id] as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { times } from 'lodash'

import { DATE_PARSE_FORMAT } from '~shared/constants/dates'
import {
AddressFieldResponseV3,
AttachmentFieldResponseV3,
CheckboxFieldResponsesV3,
ChildrenCompoundFieldResponsesV3,
Expand Down Expand Up @@ -222,7 +223,7 @@ const transformToChildOutput = (

const transformToAddressOutput = (
schema: AddressFieldSchema,
input?: AddressFieldValues, // | AddressFieldResponseV3
input?: AddressFieldValues | AddressFieldResponseV3,
): AddressResponse => {
let answer = ''
if (input?.postalCode !== undefined) answer = input.postalCode // TODO: do for all input fields, just using postalCode to test now
Expand Down
12 changes: 6 additions & 6 deletions shared/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export const UenResponse = SingleAnswerResponse.extend({
})
export type UenResponse = z.infer<typeof UenResponse>

// TODO: check fo multivalue response
export const AddressResponse = SingleAnswerResponse.extend({
fieldType: z.literal(BasicField.Address),
})
export type AddressResponse = z.infer<typeof AddressResponse>

export const ChildBirthRecordsResponse = ResponseBase.merge(
MyInfoResponseBase,
).extend({
Expand All @@ -154,12 +160,6 @@ export type SingleChildSubRecordResponse = z.infer<
typeof SingleChildSubRecordResponse
>

// TODO: check fo multivalue response
export const AddressResponse = SingleAnswerResponse.extend({
fieldType: z.literal(BasicField.Address),
})
export type AddressResponse = z.infer<typeof AddressResponse>

export type FieldResponse =
| HeaderResponse
| EmailResponse
Expand Down
6 changes: 3 additions & 3 deletions src/app/utils/field-validation/validators/addressValidator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { chain, left, right } from 'fp-ts/lib/Either'
import { flow } from 'fp-ts/lib/function'
import { AddressResponseV3, BasicField } from 'shared/types'
import { validatePostalCode } from 'shared/utils/address-validation'

import { ProcessedSingleAnswerResponse } from 'src/app/modules/submission/submission.types'
import { IAddressFieldSchema, OmitUnusedValidatorProps } from 'src/types'

import { AddressResponseV3, BasicField } from '../../../../../shared/types'
import { validatePostalCode } from '../../../../../shared/utils/address-validation'
import { ParsedClearFormFieldResponseV3 } from '../../../../types/api'
import {
ResponseValidator,
Expand All @@ -14,7 +14,7 @@ import {

import {
notEmptySingleAnswerResponse,
notEmptySingleAnswerResponseV3,
// notEmptySingleAnswerResponseV3,
} from './common'

type AddressValidator = ResponseValidator<ProcessedSingleAnswerResponse>
Expand Down

0 comments on commit dd32a67

Please sign in to comment.