mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Check if a date string is valid ISO8601 before parsing it (#14127)
* add datetime format check * updated error message thrown * removed unneeded "T" from date format * updated date validation and parsing to use the date-fns functions
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Accountability, Query, SchemaOverview } from '@directus/shared/types';
|
||||
import { format, parseISO, isValid } from 'date-fns';
|
||||
import { parseJSON, toArray } from '@directus/shared/utils';
|
||||
import { format } from 'date-fns';
|
||||
import { unflatten } from 'flat';
|
||||
import Joi from 'joi';
|
||||
import { Knex } from 'knex';
|
||||
@@ -318,25 +318,19 @@ export class PayloadService {
|
||||
} else {
|
||||
if (value instanceof Date === false && typeof value === 'string') {
|
||||
if (dateColumn.type === 'date') {
|
||||
const [date] = value.split('T');
|
||||
const [year, month, day] = date.split('-');
|
||||
|
||||
payload[name] = new Date(Number(year), Number(month) - 1, Number(day));
|
||||
const parsedDate = parseISO(value);
|
||||
if (!isValid(parsedDate)) {
|
||||
throw new InvalidPayloadException(`Invalid Date format in field "${dateColumn.field}"`);
|
||||
}
|
||||
payload[name] = parsedDate;
|
||||
}
|
||||
|
||||
if (dateColumn.type === 'dateTime') {
|
||||
const [date, time] = value.split('T');
|
||||
const [year, month, day] = date.split('-');
|
||||
const [hours, minutes, seconds] = time.substring(0, 8).split(':');
|
||||
|
||||
payload[name] = new Date(
|
||||
Number(year),
|
||||
Number(month) - 1,
|
||||
Number(day),
|
||||
Number(hours),
|
||||
Number(minutes),
|
||||
Number(seconds)
|
||||
);
|
||||
const parsedDate = parseISO(value);
|
||||
if (!isValid(parsedDate)) {
|
||||
throw new InvalidPayloadException(`Invalid DateTime format in field "${dateColumn.field}"`);
|
||||
}
|
||||
payload[name] = parsedDate;
|
||||
}
|
||||
|
||||
if (dateColumn.type === 'timestamp') {
|
||||
|
||||
Reference in New Issue
Block a user