Skip to content

Commit edaf764

Browse files
committed
refactor: improve null value handling in validateAndSetFieldValue and normalizeMongoValue methods
1 parent 454161f commit edaf764

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
223223
validateAndSetFieldValue(field: AdminForthResourceColumn, value: any): any {
224224
// Int
225225
if (field.type === AdminForthDataTypes.INTEGER) {
226-
if (value === "" || value === null) return this.setFieldValue(field, null);
226+
if (value === "" || value === null) {
227+
return this.setFieldValue(field, null);
228+
}
227229
if (!Number.isFinite(value)) {
228230
throw new Error(`Value is not an integer. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`);
229231
}
@@ -232,7 +234,9 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
232234

233235
// Float
234236
if (field.type === AdminForthDataTypes.FLOAT) {
235-
if (value === "" || value === null) return this.setFieldValue(field, null);
237+
if (value === "" || value === null) {
238+
return this.setFieldValue(field, null);
239+
}
236240

237241
if (typeof value !== "number" || !Number.isFinite(value)) {
238242
throw new Error(
@@ -245,11 +249,17 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
245249

246250
// Decimal
247251
if (field.type === AdminForthDataTypes.DECIMAL) {
248-
if (value === "" || value === null) return this.setFieldValue(field, null);
252+
if (value === "" || value === null) {
253+
return this.setFieldValue(field, null);
254+
}
249255
if (typeof value === "string") {
250256
const string = value.trim();
251-
if (!string) return this.setFieldValue(field, null);
252-
if (Number.isFinite(Number(string))) return this.setFieldValue(field, string);
257+
if (!string) {
258+
return this.setFieldValue(field, null);
259+
}
260+
if (Number.isFinite(Number(string))) {
261+
return this.setFieldValue(field, string);
262+
}
253263
throw new Error(`Value is not a decimal. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`);
254264
}
255265

@@ -261,7 +271,9 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
261271

262272
// DateTime
263273
if (field.type === AdminForthDataTypes.DATETIME) {
264-
if (value === "" || value === null) return this.setFieldValue(field, null);
274+
if (value === "" || value === null) {
275+
return this.setFieldValue(field, null);
276+
}
265277
if (!dayjs(value).isValid()) {
266278
throw new Error(`Value is not a valid datetime. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`);
267279
}
@@ -272,7 +284,9 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
272284

273285
// Boolean
274286
if (field.type === AdminForthDataTypes.BOOLEAN) {
275-
if (value === "" || value === null) return this.setFieldValue(field, null);
287+
if (value === "" || value === null) {
288+
return this.setFieldValue(field, null);
289+
}
276290
if (typeof value !== 'boolean') {
277291
throw new Error(`Value is not a boolean. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`);
278292
}
@@ -283,7 +297,9 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
283297

284298
// String
285299
if (field.type === AdminForthDataTypes.STRING) {
286-
if (value === "" || value === null) return this.setFieldValue(field, null);
300+
if (value === "" || value === null){
301+
return this.setFieldValue(field, null);
302+
}
287303
}
288304
return this.setFieldValue(field, value);
289305
}

adminforth/dataConnectors/mongo.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ const escapeRegex = (value) => {
1010
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // Escapes special characters
1111
};
1212
function normalizeMongoValue(v: any) {
13-
if (v == null) return v;
14-
if (v instanceof Decimal128) return v.toString();
15-
if (v instanceof Double) return v.valueOf();
16-
if (typeof v === "object" && v.$numberDecimal) return String(v.$numberDecimal);
17-
if (typeof v === "object" && v.$numberDouble) return Number(v.$numberDouble);
13+
if (v == null) {
14+
return v;
15+
}
16+
if (v instanceof Decimal128) {
17+
return v.toString();
18+
}
19+
if (v instanceof Double) {
20+
return v.valueOf();
21+
}
22+
if (typeof v === "object" && v.$numberDecimal) {
23+
return String(v.$numberDecimal);
24+
}
25+
if (typeof v === "object" && v.$numberDouble) {
26+
return Number(v.$numberDouble);
27+
}
1828
return v;
1929
}
2030

@@ -212,22 +222,30 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
212222
if (value === null) return null;
213223

214224
if (field.type === AdminForthDataTypes.DATETIME) {
215-
if (value === "" || value === null) return null;
225+
if (value === "" || value === null) {
226+
return null;
227+
}
216228
return dayjs(value).isValid() ? dayjs(value).toDate() : null;
217229
}
218230

219231
if (field.type === AdminForthDataTypes.INTEGER) {
220-
if (value === "" || value === null) return null;
232+
if (value === "" || value === null) {
233+
return null;
234+
}
221235
return Number.isFinite(value) ? Math.trunc(value) : null;
222236
}
223237

224238
if (field.type === AdminForthDataTypes.FLOAT) {
225-
if (value === "" || value === null) return null;
239+
if (value === "" || value === null) {
240+
return null;
241+
}
226242
return Number.isFinite(value) ? value : null;
227243
}
228244

229245
if (field.type === AdminForthDataTypes.DECIMAL) {
230-
if (value === "" || value === null) return null;
246+
if (value === "" || value === null) {
247+
return null;
248+
}
231249
return value.toString();
232250
}
233251

0 commit comments

Comments
 (0)