Skip to content

Commit b71ab81

Browse files
Preprocess whitespace so CharacterCount reports the correct string length
1 parent 3d5c6f5 commit b71ab81

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/server/plugins/engine/components/MultilineTextField.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,51 @@ describe('MultilineTextField', () => {
627627
output: { value: getFormData('') }
628628
}
629629
]
630+
},
631+
{
632+
description: 'Schema max with whitespace replacement',
633+
component: {
634+
title: 'Example textarea',
635+
name: 'myComponent',
636+
type: ComponentType.MultilineTextField,
637+
options: {},
638+
schema: {
639+
max: 10
640+
}
641+
} satisfies MultilineTextFieldComponent,
642+
assertions: [
643+
{
644+
input: getFormData(`h\r\ne\r\nl\r\nl\r\no`),
645+
output: {
646+
value: getFormData(`h\ne\nl\nl\no`)
647+
}
648+
}
649+
]
650+
},
651+
{
652+
description: 'Schema max with whitespace replacement',
653+
component: {
654+
title: 'Example textarea',
655+
name: 'myComponent',
656+
type: ComponentType.MultilineTextField,
657+
options: {},
658+
schema: {
659+
max: 8
660+
}
661+
} satisfies MultilineTextFieldComponent,
662+
assertions: [
663+
{
664+
input: getFormData(`h\r\ne\r\nl\r\nl\r\no`),
665+
output: {
666+
value: getFormData(`h\ne\nl\nl\no`),
667+
errors: [
668+
expect.objectContaining({
669+
text: 'Example textarea must be 8 characters or less'
670+
})
671+
]
672+
}
673+
}
674+
]
630675
}
631676
])('$description', ({ component: def, assertions }) => {
632677
let collection: ComponentCollection

src/server/plugins/engine/components/MultilineTextField.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ export class MultilineTextField extends FormComponent {
2626

2727
const { schema, options } = def
2828

29-
let formSchema = Joi.string().trim().label(this.label).required()
29+
let formSchema = Joi.string()
30+
.replace(/\r\n/g, '\n')
31+
.trim()
32+
.label(this.label)
33+
.required()
3034

3135
if (options.required === false) {
3236
formSchema = formSchema.allow('')

0 commit comments

Comments
 (0)