diff --git a/src/app/modules/form/admin-form/admin-form.service.ts b/src/app/modules/form/admin-form/admin-form.service.ts index e5ebdddb72..a2466c32b3 100644 --- a/src/app/modules/form/admin-form/admin-form.service.ts +++ b/src/app/modules/form/admin-form/admin-form.service.ts @@ -105,6 +105,7 @@ import { checkIsApiSecretKeyName, generateTwilioCredSecretKeyName, getUpdatedFormFields, + insertTableShortTextColumnDefaultValidationOptions, processDuplicateOverrideProps, } from './admin-form.utils' @@ -652,8 +653,10 @@ export const updateFormField = ( fieldId: string, newField: FieldUpdateDto, ): ResultAsync => { + const _newField = insertTableShortTextColumnDefaultValidationOptions(newField) + return ResultAsync.fromPromise( - form.updateFormFieldById(fieldId, newField), + form.updateFormFieldById(fieldId, _newField), (error) => { logger.error({ message: 'Error encountered while updating form field', diff --git a/src/app/modules/form/admin-form/admin-form.utils.ts b/src/app/modules/form/admin-form/admin-form.utils.ts index cb7efd448f..8b6537fd1c 100644 --- a/src/app/modules/form/admin-form/admin-form.utils.ts +++ b/src/app/modules/form/admin-form/admin-form.utils.ts @@ -4,9 +4,12 @@ import { err, ok, Result } from 'neverthrow' import { v4 as uuidv4 } from 'uuid' import { + BasicField, DuplicateFormBodyDto, + FieldUpdateDto, FormResponseMode, FormStatus, + TextValidationOptions, } from '../../../../../shared/types' import { reorder, @@ -586,3 +589,36 @@ export const mapGoGovErrors = (error: AxiosError): GoGovError => { ) } } + +// TODO: remove once we upgrade to Mongoose 7.x +/** + * Manually insert default validation options for short text columns in tables. + * Due to a bug in Mongoose 6.x, default values are not inherited for discriminators. + * + * See https://github.com/Automattic/mongoose/issues/12135 + * + * Fixed in Mongoose 7.x + * @param newField + * @returns + */ + +export const insertTableShortTextColumnDefaultValidationOptions = ( + newField: FieldUpdateDto, +) => { + if (newField.fieldType === BasicField.Table) { + const defaultValidationOptions: TextValidationOptions = { + customVal: null, + selectedValidation: null, + } + newField.columns.map((column) => { + if (column.columnType === BasicField.ShortText) { + column.ValidationOptions = { + ...defaultValidationOptions, + ...column.ValidationOptions, + } + } + return column + }) + } + return newField +}