Skip to content

Commit

Permalink
fix: add patch to feed default ValidationOptions on table shorttext (#…
Browse files Browse the repository at this point in the history
…7029)

* fix: add patch to feed default ValidationOptions on table shorttext

* fix: add TextValidationOptions type guard to replacer
  • Loading branch information
KenLSM authored Jan 23, 2024
1 parent a93e29d commit 9cb3126
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/app/modules/form/admin-form/admin-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import {
checkIsApiSecretKeyName,
generateTwilioCredSecretKeyName,
getUpdatedFormFields,
insertTableShortTextColumnDefaultValidationOptions,
processDuplicateOverrideProps,
} from './admin-form.utils'

Expand Down Expand Up @@ -652,8 +653,10 @@ export const updateFormField = (
fieldId: string,
newField: FieldUpdateDto,
): ResultAsync<FormFieldSchema, PossibleDatabaseError | FieldNotFoundError> => {
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',
Expand Down
36 changes: 36 additions & 0 deletions src/app/modules/form/admin-form/admin-form.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

0 comments on commit 9cb3126

Please sign in to comment.