From 366e6d6f04fa46dc657cff42d512d7478c5e5f1f Mon Sep 17 00:00:00 2001 From: Philip Bruvoll Date: Tue, 17 Nov 2026 11:18:08 +0100 Subject: [PATCH 1/3] fix: keep invalid dates in date/date-time fields out of the save request Typing an impossible date like 32.13.2026 into a Datepicker bound to a DateTime field sent the raw text in the PATCH. The backend could not deserialize it and the frontend crashed with "Ukjent feil". convertData now rejects values that are not ISO dates (strictParseISO) for string fields with format date or date-time, so they are kept as invalid data and never sent. The Datepicker reads the invalid value to show its own "invalid date format" message, and filters out the generic invalid-data message to avoid a duplicate. Refs #4180 --- src/features/formData/convertData.test.ts | 27 +++++++++++++++++++ src/features/formData/convertData.ts | 9 +++++-- .../Datepicker/DatepickerComponent.test.tsx | 19 +++++++++++++ src/layout/Datepicker/index.tsx | 12 ++++++++- .../Datepicker/useDatepickerValidation.ts | 4 ++- 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/features/formData/convertData.test.ts diff --git a/src/features/formData/convertData.test.ts b/src/features/formData/convertData.test.ts new file mode 100644 index 0000000000..8ba2b8b945 --- /dev/null +++ b/src/features/formData/convertData.test.ts @@ -0,0 +1,27 @@ +import type { JSONSchema7 } from 'json-schema'; + +import { convertData } from 'src/features/formData/convertData'; + +describe('convertData', () => { + describe.each(['date', 'date-time'])('string with format %s', (format) => { + const schema: JSONSchema7 = { type: 'string', format }; + + it.each(['2026-01-01', '2026-01-01T12:34:56.123+01:00'])('accepts %s', (value) => { + expect(convertData(value, schema)).toEqual({ newValue: value, error: false }); + }); + + it.each(['32.13.2026', '2026-13-32', '12.34.____'])('rejects %s', (value) => { + expect(convertData(value, schema)).toEqual({ newValue: undefined, error: true }); + }); + }); + + it('accepts any value for a string without a date format', () => { + expect(convertData('32.13.2026', { type: 'string' })).toEqual({ newValue: '32.13.2026', error: false }); + }); + + it('validates dates in an anyOf sub-schema', () => { + const schema: JSONSchema7 = { anyOf: [{ type: 'string', format: 'date-time' }, { type: 'null' }] }; + expect(convertData('2026-01-01', schema)).toEqual({ newValue: '2026-01-01', error: false }); + expect(convertData('32.13.2026', schema)).toEqual({ newValue: undefined, error: true }); + }); +}); diff --git a/src/features/formData/convertData.ts b/src/features/formData/convertData.ts index bcdcae87de..f9f783c8a4 100644 --- a/src/features/formData/convertData.ts +++ b/src/features/formData/convertData.ts @@ -1,5 +1,7 @@ import type { JSONSchema7, JSONSchema7Definition } from 'json-schema'; +import { strictParseISO } from 'src/app-components/Datepicker/utils/dateHelpers'; + interface ReturnType { // eslint-disable-next-line @typescript-eslint/no-explicit-any newValue: any; @@ -55,15 +57,18 @@ function convertToType(value: Value, schema: JSONSchema7 | JSONSchema7Definition // eslint-disable-next-line @typescript-eslint/no-explicit-any AllValidTypes.includes(schema.type as any) ) { - return convertToScalar(value, schema.type as ValidTypes, schema['@xsdType']); + return convertToScalar(value, schema.type as ValidTypes, schema['@xsdType'], schema.format); } throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`); } -function convertToScalar(value: Value, targetType: ValidTypes, xsdType?: string): ReturnType { +function convertToScalar(value: Value, targetType: ValidTypes, xsdType?: string, format?: string): ReturnType { const sVal = String(value); if (targetType === 'string') { + if ((format === 'date-time' || format === 'date') && !strictParseISO(sVal)) { + return { newValue: undefined, error: true }; + } return { newValue: sVal, error: false }; } diff --git a/src/layout/Datepicker/DatepickerComponent.test.tsx b/src/layout/Datepicker/DatepickerComponent.test.tsx index 480f9c5861..4edd6f2203 100644 --- a/src/layout/Datepicker/DatepickerComponent.test.tsx +++ b/src/layout/Datepicker/DatepickerComponent.test.tsx @@ -205,6 +205,25 @@ describe('DatepickerComponent', () => { }); }); + it('should show its own validation message and not save when an invalid date is typed into a date-time field', async () => { + const { mutations } = await render({ + component: { showValidations: ['AllExceptRequired'] }, + queries: { + fetchDataModelSchema: async () => ({ + type: 'object', + properties: { myDate: { type: 'string', format: 'date-time' } }, + }), + }, + }); + + await userEvent.type(screen.getByRole('textbox'), '32.13.2026'); + await userEvent.tab(); + + expect(await screen.findByText('Ugyldig datoformat. Bruk formatet DD.MM.YYYY.')).toBeInTheDocument(); + expect(screen.queryByText('Feil format eller verdi')).not.toBeInTheDocument(); + expect(mutations.doPatchFormData.mock).not.toHaveBeenCalled(); + }); + it('should not have aria-describedby if textResources.description does not exist', async () => { await render({ component: { textResourceBindings: {}, id: 'test-id' } }); const inputField = screen.getByRole('textbox'); diff --git a/src/layout/Datepicker/index.tsx b/src/layout/Datepicker/index.tsx index fe9f386aac..03a0a16b74 100644 --- a/src/layout/Datepicker/index.tsx +++ b/src/layout/Datepicker/index.tsx @@ -76,6 +76,16 @@ export class Datepicker extends DatepickerDef implements ValidateComponent, Vali ); } + /** + * Avoid duplicate validation message. + */ + private static invalidDataFormatFilter(validation: BaseValidation): boolean { + return !( + validation.source === FrontendValidationSource.InvalidData && + validation.message.key === 'validation_errors.pattern' + ); + } + /** * Avoid duplicate validation message. */ @@ -97,7 +107,7 @@ export class Datepicker extends DatepickerDef implements ValidateComponent, Vali } getValidationFilters(baseComponentId: string, layoutLookups: LayoutLookups): ValidationFilterFunction[] { - const filters = [Datepicker.schemaFormatFilter]; + const filters = [Datepicker.schemaFormatFilter, Datepicker.invalidDataFormatFilter]; const component = layoutLookups.getComponent(baseComponentId, 'Datepicker'); if (component.minDate) { diff --git a/src/layout/Datepicker/useDatepickerValidation.ts b/src/layout/Datepicker/useDatepickerValidation.ts index a666c2c64b..b3fe88b500 100644 --- a/src/layout/Datepicker/useDatepickerValidation.ts +++ b/src/layout/Datepicker/useDatepickerValidation.ts @@ -12,7 +12,9 @@ export function useDatepickerValidation(baseComponentId: string): ComponentValid const currentLanguage = useCurrentLanguage(); const field = useDataModelBindingsFor(baseComponentId, 'Datepicker')?.simpleBinding; const component = useItemWhenType(baseComponentId, 'Datepicker'); - const data = FD.useDebouncedPick(field); + const validData = FD.useDebouncedPick(field); + const invalidData = FD.useInvalidDebouncedPick(field); + const data = validData ?? invalidData; const minDate = getDateConstraint(component?.minDate, 'min'); const maxDate = getDateConstraint(component?.maxDate, 'max'); const format = getDateFormat(component?.format, currentLanguage); From 873b7a498682ca67df854a18bf172f7c8df459c6 Mon Sep 17 00:00:00 2001 From: Philip Bruvoll Date: Tue, 17 Nov 2026 12:36:43 +0100 Subject: [PATCH 2/3] fix: only validate dates for format date-time Studio generates a C# DateTime only for format date-time. For format date it generates a string that accepts any text, so validating it would stop saving the text without preventing any crash. --- src/features/formData/convertData.test.ts | 31 ++++++++++++----------- src/features/formData/convertData.ts | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/features/formData/convertData.test.ts b/src/features/formData/convertData.test.ts index 8ba2b8b945..63fbab3c4c 100644 --- a/src/features/formData/convertData.test.ts +++ b/src/features/formData/convertData.test.ts @@ -2,26 +2,27 @@ import type { JSONSchema7 } from 'json-schema'; import { convertData } from 'src/features/formData/convertData'; -describe('convertData', () => { - describe.each(['date', 'date-time'])('string with format %s', (format) => { - const schema: JSONSchema7 = { type: 'string', format }; +describe('convertData with format date-time', () => { + const schema: JSONSchema7 = { type: 'string', format: 'date-time' }; - it.each(['2026-01-01', '2026-01-01T12:34:56.123+01:00'])('accepts %s', (value) => { - expect(convertData(value, schema)).toEqual({ newValue: value, error: false }); - }); - - it.each(['32.13.2026', '2026-13-32', '12.34.____'])('rejects %s', (value) => { - expect(convertData(value, schema)).toEqual({ newValue: undefined, error: true }); - }); + it.each(['2026-01-01', '2026-01-01T12:34:56.123+01:00'])('accepts %s', (value) => { + expect(convertData(value, schema)).toEqual({ newValue: value, error: false }); }); - it('accepts any value for a string without a date format', () => { - expect(convertData('32.13.2026', { type: 'string' })).toEqual({ newValue: '32.13.2026', error: false }); + it.each(['32.13.2026', '2026-13-32', '12.34.____'])('rejects %s', (value) => { + expect(convertData(value, schema)).toEqual({ newValue: undefined, error: true }); }); it('validates dates in an anyOf sub-schema', () => { - const schema: JSONSchema7 = { anyOf: [{ type: 'string', format: 'date-time' }, { type: 'null' }] }; - expect(convertData('2026-01-01', schema)).toEqual({ newValue: '2026-01-01', error: false }); - expect(convertData('32.13.2026', schema)).toEqual({ newValue: undefined, error: true }); + const nullableSchema: JSONSchema7 = { anyOf: [schema, { type: 'null' }] }; + expect(convertData('2026-01-01', nullableSchema)).toEqual({ newValue: '2026-01-01', error: false }); + expect(convertData('32.13.2026', nullableSchema)).toEqual({ newValue: undefined, error: true }); + }); + + it('does not validate format date, as it is generated as a C# string that accepts any text', () => { + expect(convertData('32.13.2026', { type: 'string', format: 'date' })).toEqual({ + newValue: '32.13.2026', + error: false, + }); }); }); diff --git a/src/features/formData/convertData.ts b/src/features/formData/convertData.ts index f9f783c8a4..aa2cef2e5d 100644 --- a/src/features/formData/convertData.ts +++ b/src/features/formData/convertData.ts @@ -66,7 +66,7 @@ function convertToType(value: Value, schema: JSONSchema7 | JSONSchema7Definition function convertToScalar(value: Value, targetType: ValidTypes, xsdType?: string, format?: string): ReturnType { const sVal = String(value); if (targetType === 'string') { - if ((format === 'date-time' || format === 'date') && !strictParseISO(sVal)) { + if (format === 'date-time' && !strictParseISO(sVal)) { return { newValue: undefined, error: true }; } return { newValue: sVal, error: false }; From 80d1311f77e3f2caa0b893b236ec0194999f7e78 Mon Sep 17 00:00:00 2001 From: Philip Bruvoll Date: Tue, 17 Nov 2026 12:43:53 +0100 Subject: [PATCH 3/3] test: clarify convertData test names for date-time and date --- src/features/formData/convertData.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/formData/convertData.test.ts b/src/features/formData/convertData.test.ts index 63fbab3c4c..13356c4771 100644 --- a/src/features/formData/convertData.test.ts +++ b/src/features/formData/convertData.test.ts @@ -13,13 +13,13 @@ describe('convertData with format date-time', () => { expect(convertData(value, schema)).toEqual({ newValue: undefined, error: true }); }); - it('validates dates in an anyOf sub-schema', () => { + it('validates date-time in an anyOf sub-schema', () => { const nullableSchema: JSONSchema7 = { anyOf: [schema, { type: 'null' }] }; expect(convertData('2026-01-01', nullableSchema)).toEqual({ newValue: '2026-01-01', error: false }); expect(convertData('32.13.2026', nullableSchema)).toEqual({ newValue: undefined, error: true }); }); - it('does not validate format date, as it is generated as a C# string that accepts any text', () => { + it('does not reject an invalid date for format "date", as the C# string accepts any text (validation still flags it)', () => { expect(convertData('32.13.2026', { type: 'string', format: 'date' })).toEqual({ newValue: '32.13.2026', error: false,