Skip to content

Commit 963024e

Browse files
committed
fix(cloud): cap onboarding survey free-text length
1 parent 06334d8 commit 963024e

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/locales/en/main.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,8 @@
31303130
"errors": {
31313131
"chooseAnOption": "Please choose an option.",
31323132
"selectAtLeastOne": "Please select at least one option.",
3133-
"describeAnswer": "Please describe your answer."
3133+
"describeAnswer": "Please describe your answer.",
3134+
"answerTooLong": "Please keep your answer under {max} characters."
31343135
},
31353136
"options": {
31363137
"intent": {

src/platform/cloud/onboarding/survey/DynamicSurveyField.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
v-if="field.allowOther && field.otherFieldId && isOtherSelected"
6262
:model-value="(otherValue as string) ?? ''"
6363
:class="inputClass"
64+
:maxlength="OTHER_TEXT_MAX_LENGTH"
6465
:placeholder="
6566
$t(
6667
`cloudOnboarding.survey.options.${field.id}.otherPlaceholder`,
@@ -103,6 +104,8 @@ import type {
103104
OnboardingSurveyOption
104105
} from '@/platform/remoteConfig/types'
105106
107+
import { OTHER_TEXT_MAX_LENGTH } from './surveySchema'
108+
106109
const {
107110
field,
108111
modelValue,

src/platform/cloud/onboarding/survey/surveySchema.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { OnboardingSurvey } from '@/platform/remoteConfig/types'
44

55
import { defaultOnboardingSurvey } from './defaultSurveySchema'
66
import {
7+
OTHER_TEXT_MAX_LENGTH,
78
buildInitialValues,
89
buildSubmissionPayload,
910
buildZodSchema,
@@ -404,4 +405,23 @@ describe('other free-text validation', () => {
404405
schema.safeParse({ source: 'other', sourceOther: ' ' }).success
405406
).toBe(false)
406407
})
408+
409+
it('rejects an "other" answer longer than the max length', () => {
410+
const schema = buildZodSchema(otherSurvey, {
411+
source: 'other',
412+
sourceOther: 'x'.repeat(OTHER_TEXT_MAX_LENGTH + 1)
413+
})
414+
expect(
415+
schema.safeParse({
416+
source: 'other',
417+
sourceOther: 'x'.repeat(OTHER_TEXT_MAX_LENGTH + 1)
418+
}).success
419+
).toBe(false)
420+
expect(
421+
schema.safeParse({
422+
source: 'other',
423+
sourceOther: 'x'.repeat(OTHER_TEXT_MAX_LENGTH)
424+
}).success
425+
).toBe(true)
426+
})
407427
})

src/platform/cloud/onboarding/survey/surveySchema.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type {
99

1010
export type SurveyValues = Record<string, string | string[] | undefined>
1111

12+
export const OTHER_TEXT_MAX_LENGTH = 200
13+
1214
export const hasNonEmptyValue = (
1315
current: string | string[] | undefined
1416
): boolean => {
@@ -59,7 +61,7 @@ export const prepareSurvey = (survey: OnboardingSurvey): OnboardingSurvey => ({
5961
fields: survey.fields.map(randomizeOptions)
6062
})
6163

62-
type Translator = (key: string) => string
64+
type Translator = (key: string, named?: Record<string, unknown>) => string
6365

6466
const identityTranslator: Translator = (key) => key
6567

@@ -100,6 +102,11 @@ export const buildZodSchema = (
100102
.min(1, {
101103
message: t('cloudOnboarding.survey.errors.describeAnswer')
102104
})
105+
.max(OTHER_TEXT_MAX_LENGTH, {
106+
message: t('cloudOnboarding.survey.errors.answerTooLong', {
107+
max: OTHER_TEXT_MAX_LENGTH
108+
})
109+
})
103110
} else if (field.otherFieldId) {
104111
shape[field.otherFieldId] = z.string().optional()
105112
}

0 commit comments

Comments
 (0)