Only accept yyyy-MM-dd as Date + don't cast DATE from DB to Date()

This commit is contained in:
Pierre Noël 2022-01-18 17:12:25 +01:00
parent de7a92d566
commit ca910fe92c
3 changed files with 20 additions and 14 deletions

View File

@ -14,15 +14,20 @@ class MysqlDialect extends Dialect {
this.db.config.connection.connection.supportBigNumbers = true; this.db.config.connection.connection.supportBigNumbers = true;
this.db.config.connection.connection.bigNumberStrings = true; this.db.config.connection.connection.bigNumberStrings = true;
this.db.config.connection.connection.typeCast = (field, next) => { this.db.config.connection.connection.typeCast = (field, next) => {
if (field.type == 'DECIMAL' || field.type === 'NEWDECIMAL') { if (field.type === 'DECIMAL' || field.type === 'NEWDECIMAL') {
var value = field.string(); var value = field.string();
return value === null ? null : Number(value); return value === null ? null : Number(value);
} }
if (field.type == 'TINY' && field.length == 1) { if (field.type === 'TINY' && field.length === 1) {
let value = field.string(); let value = field.string();
return value ? value == '1' : null; return value ? value === '1' : null;
} }
if (field.type === 'DATE') {
return field.string();
}
return next(); return next();
}; };
} }

View File

@ -16,6 +16,7 @@ class PostgresDialect extends Dialect {
} }
initialize() { initialize() {
this.db.connection.client.driver.types.setTypeParser(1082, 'text', v => v); // Don't cast DATE string to Date()
this.db.connection.client.driver.types.setTypeParser(1700, 'text', parseFloat); this.db.connection.client.driver.types.setTypeParser(1700, 'text', parseFloat);
} }

View File

@ -98,6 +98,7 @@ class BigIntegerField extends NumberField {
} }
const timeRegex = new RegExp('^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]{1,3})?$'); const timeRegex = new RegExp('^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]{1,3})?$');
const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
const parseTime = value => { const parseTime = value => {
if (dateFns.isDate(value)) return dateFns.format(value, 'HH:mm:ss.SSS'); if (dateFns.isDate(value)) return dateFns.format(value, 'HH:mm:ss.SSS');
@ -118,16 +119,16 @@ const parseTime = value => {
}; };
const parseDate = value => { const parseDate = value => {
if (dateFns.isDate(value)) return dateFns.format(value, 'yyyy-MM-dd'); if (!dateRegex.test(value)) {
try { throw new InvalidDateError(`Invalid format, expected yyyy-MM-dd`);
let date = dateFns.parseISO(value);
if (dateFns.isValid(date)) return dateFns.format(date, 'yyyy-MM-dd');
throw new InvalidDateError(`Invalid format, expected an ISO compatible date`);
} catch (error) {
throw new InvalidDateError(`Invalid format, expected an ISO compatible date`);
} }
let date = dateFns.parseISO(value);
if (!dateFns.isValid(date)) {
throw new InvalidDateError(`Invalid date`);
}
return value;
}; };
const parseDateTimeOrTimestamp = value => { const parseDateTimeOrTimestamp = value => {
@ -151,8 +152,7 @@ class DateField extends Field {
} }
fromDB(value) { fromDB(value) {
const cast = new Date(value); return value;
return dateFns.isValid(cast) ? dateFns.formatISO(cast, { representation: 'date' }) : null;
} }
} }
class DatetimeField extends Field { class DatetimeField extends Field {