|
9 | 9 | import { suggestIfTypo } from "../modules/utils.js"; |
10 | 10 | import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections } from "../types/Common.js"; |
11 | 11 | import { randomUUID } from "crypto"; |
| 12 | +import dayjs from "dayjs"; |
12 | 13 |
|
13 | 14 |
|
14 | 15 | export default class AdminForthBaseConnector implements IAdminForthDataSourceConnectorBase { |
@@ -164,9 +165,9 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon |
164 | 165 | return { ok: false, error: `Value for operator '${filters.operator}' should not be empty array, in filter object: ${JSON.stringify(filters) }` }; |
165 | 166 | } |
166 | 167 | } |
167 | | - filters.value = filters.value.map((val: any) => this.setFieldValue(fieldObj, val)); |
| 168 | + filters.value = filters.value.map((val: any) => this.validateAndSetFieldValue(fieldObj, val)); |
168 | 169 | } else { |
169 | | - filtersAsSingle.value = this.setFieldValue(fieldObj, filtersAsSingle.value); |
| 170 | + filtersAsSingle.value = this.validateAndSetFieldValue(fieldObj, filtersAsSingle.value); |
170 | 171 | } |
171 | 172 | } else if (filtersAsSingle.insecureRawSQL || filtersAsSingle.insecureRawNoSQL) { |
172 | 173 | // if "insecureRawSQL" filter is insecure sql string |
@@ -219,6 +220,97 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon |
219 | 220 | throw new Error('Method not implemented.'); |
220 | 221 | } |
221 | 222 |
|
| 223 | + validateAndSetFieldValue(field: AdminForthResourceColumn, value: any): any { |
| 224 | + // Int |
| 225 | + if (field.type === AdminForthDataTypes.INTEGER) { |
| 226 | + if (value === "" || value === null) return this.setFieldValue(field, null); |
| 227 | + if (!Number.isFinite(value) || Math.trunc(value) !== value) { |
| 228 | + throw new Error(`Value is not an integer. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`); |
| 229 | + } |
| 230 | + return this.setFieldValue(field, Math.trunc(value)); |
| 231 | + } |
| 232 | + |
| 233 | + // Float |
| 234 | + if (field.type === AdminForthDataTypes.FLOAT) { |
| 235 | + if (value === "" || value === null) return this.setFieldValue(field, null); |
| 236 | + const n = |
| 237 | + typeof value === "number" |
| 238 | + ? value |
| 239 | + : (typeof value === "object" && value !== null ? (value as any).valueOf() : NaN); |
| 240 | + |
| 241 | + if (typeof n !== "number" || !Number.isFinite(n)) { |
| 242 | + throw new Error( |
| 243 | + `Value is not a float. Field ${field.name} with type is ${field.type}, but got value: ${String(value)} with type ${typeof value}` |
| 244 | + ); |
| 245 | + } |
| 246 | + |
| 247 | + return this.setFieldValue(field, n); |
| 248 | + } |
| 249 | + |
| 250 | + // Decimal |
| 251 | + if (field.type === AdminForthDataTypes.DECIMAL) { |
| 252 | + if (value === "" || value === null) return this.setFieldValue(field, null); |
| 253 | + |
| 254 | + if (typeof value === "number") { |
| 255 | + if (!Number.isFinite(value)) { |
| 256 | + throw new Error(`Value is not a decimal. Field ${field.name} got: ${value} (number)`); |
| 257 | + } |
| 258 | + return this.setFieldValue(field, value); |
| 259 | + } |
| 260 | + |
| 261 | + if (typeof value === "string") { |
| 262 | + const s = value.trim(); |
| 263 | + if (!s) return this.setFieldValue(field, null); |
| 264 | + if (Number.isFinite(Number(s))) return this.setFieldValue(field, s); |
| 265 | + throw new Error(`Value is not a decimal. Field ${field.name} got: ${value} (string)`); |
| 266 | + } |
| 267 | + |
| 268 | + if (typeof value === "object" && value) { |
| 269 | + const v: any = value; |
| 270 | + if (typeof v.toString !== "function") { |
| 271 | + throw new Error(`Decimal object has no toString(). Field ${field.name} got: ${String(value)}`); |
| 272 | + } |
| 273 | + const s = v.toString().trim(); |
| 274 | + if (!s) return this.setFieldValue(field, null); |
| 275 | + if (Number.isFinite(Number(s))) return this.setFieldValue(field, s); |
| 276 | + throw new Error(`Value is not a decimal. Field ${field.name} got: ${s} (object->string)`); |
| 277 | + } |
| 278 | + |
| 279 | + throw new Error(`Value is not a decimal. Field ${field.name} got: ${String(value)} (${typeof value})`); |
| 280 | + } |
| 281 | + |
| 282 | + // Date |
| 283 | + |
| 284 | + |
| 285 | + // DateTime |
| 286 | + if (field.type === AdminForthDataTypes.DATETIME) { |
| 287 | + if (value === "" || value === null) return this.setFieldValue(field, null); |
| 288 | + if (!dayjs(value).isValid()) { |
| 289 | + 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}`); |
| 290 | + } |
| 291 | + return this.setFieldValue(field, dayjs(value).toISOString()); |
| 292 | + } |
| 293 | + |
| 294 | + // Time |
| 295 | + |
| 296 | + // Boolean |
| 297 | + if (field.type === AdminForthDataTypes.BOOLEAN) { |
| 298 | + if (value === "" || value === null) return this.setFieldValue(field, null); |
| 299 | + if (typeof value !== 'boolean') { |
| 300 | + throw new Error(`Value is not a boolean. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`); |
| 301 | + } |
| 302 | + return this.setFieldValue(field, value); |
| 303 | + } |
| 304 | + |
| 305 | + // JSON |
| 306 | + |
| 307 | + // String |
| 308 | + if (field.type === AdminForthDataTypes.STRING) { |
| 309 | + if (value === "" || value === null) return this.setFieldValue(field, null); |
| 310 | + } |
| 311 | + return this.setFieldValue(field, value); |
| 312 | + } |
| 313 | + |
222 | 314 | getMinMaxForColumnsWithOriginalTypes({ resource, columns }: { resource: AdminForthResource; columns: AdminForthResourceColumn[]; }): Promise<{ [key: string]: { min: any; max: any; }; }> { |
223 | 315 | throw new Error('Method not implemented.'); |
224 | 316 | } |
@@ -268,7 +360,7 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon |
268 | 360 | } |
269 | 361 | if (filledRecord[col.name] !== undefined) { |
270 | 362 | // no sense to set value if it is not defined |
271 | | - recordWithOriginalValues[col.name] = this.setFieldValue(col, filledRecord[col.name]); |
| 363 | + recordWithOriginalValues[col.name] = this.validateAndSetFieldValue(col, filledRecord[col.name]); |
272 | 364 | } |
273 | 365 | } |
274 | 366 |
|
@@ -325,7 +417,7 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon |
325 | 417 | Update record received field '${field}' (with value ${newValues[field]}), but such column not found in resource '${resource.resourceId}'. ${similar ? `Did you mean '${similar}'?` : ''} |
326 | 418 | `); |
327 | 419 | } |
328 | | - recordWithOriginalValues[col.name] = this.setFieldValue(col, newValues[col.name]); |
| 420 | + recordWithOriginalValues[col.name] = this.validateAndSetFieldValue(col, newValues[col.name]); |
329 | 421 | } |
330 | 422 | const record = await this.getRecordByPrimaryKey(resource, recordId); |
331 | 423 | let error: string | null = null; |
|
0 commit comments