Skip to content

Commit d2de4de

Browse files
committed
fix: improve null and empty string handling in setFieldValue method
1 parent ab3ade3 commit d2de4de

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

adminforth/dataConnectors/mongo.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
124124
return Array.from(fieldTypes.entries()).map(([name, types]) => {
125125
const primaryKey = name === '_id';
126126

127-
const priority = ['datetime','date','decimal','integer','float','boolean','json','string'];
127+
const priority = ['datetime', 'date', 'decimal', 'integer', 'float', 'boolean', 'json', 'string'];
128128

129129
const matched = priority.find(t => types.has(t)) || 'string';
130130

@@ -201,34 +201,41 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
201201

202202
setFieldValue(field, value) {
203203
if (value === undefined) return undefined;
204-
if (value === null || value === "") return null;
204+
if (value === null) return null;
205205

206206
if (field.type === AdminForthDataTypes.DATETIME) {
207+
if (value === "" || value === null) return null;
207208
return dayjs(value).isValid() ? dayjs(value).toDate() : null;
208209
}
209210

210211
if (field.type === AdminForthDataTypes.DATE) {
212+
if (value === "" || value === null) return null;
211213
const d = dayjs(value);
212214
return d.isValid() ? d.startOf("day").toDate() : null;
213215
}
214216

215217
if (field.type === AdminForthDataTypes.BOOLEAN) {
216-
return value === null ? null : !!value;
218+
if (value === "" || value === null) return null;
219+
return !!value;
217220
}
218221

219222
if (field.type === AdminForthDataTypes.INTEGER) {
223+
if (value === "" || value === null) return null;
220224
const n = typeof value === "number" ? value : Number(String(value).replace(",", "."));
221225
return Number.isFinite(n) ? Math.trunc(n) : null;
222226
}
223227

224228
if (field.type === AdminForthDataTypes.FLOAT) {
225-
const n = typeof value === "number" ? value : Number(String(value).replace(",", "."));
226-
return Number.isFinite(n) ? new Double(n) : null;
229+
if (value === "" || value === null) return null;
230+
const n = typeof value === "number" ? value : Number(String(value).replace(",", "."));
231+
return Number.isFinite(n) ? new Double(n) : null;
227232
}
228233

229234
if (field.type === AdminForthDataTypes.DECIMAL) {
235+
if (value === "" || value === null) return null;
230236
return Decimal128.fromString(value.toString());
231237
}
238+
232239
return value;
233240
}
234241

@@ -266,7 +273,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
266273
return { $expr: { [mongoExprOp]: [left, right] } };
267274
}
268275
const column = resource.dataSourceColumns.find((col) => col.name === (filter as IAdminForthSingleFilter).field);
269-
if ([AdminForthDataTypes.INTEGER, AdminForthDataTypes.DECIMAL, AdminForthDataTypes.FLOAT].includes(column.type)) {
276+
if (column && [AdminForthDataTypes.INTEGER, AdminForthDataTypes.DECIMAL, AdminForthDataTypes.FLOAT].includes(column.type)) {
270277
return { [(filter as IAdminForthSingleFilter).field]: this.OperatorsMap[filter.operator](+(filter as IAdminForthSingleFilter).value) };
271278
}
272279
return { [(filter as IAdminForthSingleFilter).field]: this.OperatorsMap[filter.operator]((filter as IAdminForthSingleFilter).value) };

0 commit comments

Comments
 (0)