fix: iso date parsing for three digit years (#29739)

This commit is contained in:
Daniel Dietzler 2026-07-08 21:53:16 +02:00 committed by GitHub
parent 2ad554fc61
commit 053a52aec8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 6 deletions

View file

@ -14,6 +14,10 @@ describe('asDateString', () => {
const date = new Date(2000, 0, 15); // 15 Jan 2000, local midnight
expect(asDateString(date)).toBe('2000-01-15');
});
it('should correctly pad years with a leading 0', () => {
expect(asDateString(new Date('280-12-12'))).toBe('0280-12-12');
});
});
describe('asDateTimeString', () => {

View file

@ -1,4 +1,5 @@
import { ArgumentMetadata, FileValidator, Injectable, ParseUUIDPipe } from '@nestjs/common';
import { DateTime } from 'luxon';
import { createZodDto } from 'nestjs-zod';
import sanitize from 'sanitize-filename';
import { isIP, isIPRange } from 'validator';
@ -173,12 +174,7 @@ export const isoDateToDate = z
z.date(),
{
decode: (isoString) => new Date(isoString),
encode: (date) => {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
return `${y}-${m}-${d}`;
},
encode: (date) => DateTime.fromJSDate(date).toFormat('yyyy-MM-dd'),
},
)
.meta({ example: '2024-01-01' });