From 5ff76a2febca6985cb84c816d2151c803e12f4c1 Mon Sep 17 00:00:00 2001 From: yaroslav8765 Date: Mon, 16 Mar 2026 08:37:49 +0200 Subject: [PATCH] fix: don't throw an error, when user tries to save 1/0 in bool field - only show warn --- adminforth/dataConnectors/baseConnector.ts | 8 +++++++- adminforth/dataConnectors/clickhouse.ts | 2 +- adminforth/dataConnectors/mysql.ts | 2 +- adminforth/dataConnectors/sqlite.ts | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/adminforth/dataConnectors/baseConnector.ts b/adminforth/dataConnectors/baseConnector.ts index fa22dcb6..d0c39c4e 100644 --- a/adminforth/dataConnectors/baseConnector.ts +++ b/adminforth/dataConnectors/baseConnector.ts @@ -308,7 +308,13 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon return this.setFieldValue(field, null); } if (typeof value !== 'boolean') { - throw new Error(`Value is not a boolean. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`); + const errorMessage = `Value is not a boolean. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`; + if (value !== 1 && value !== 0) { + throw new Error(errorMessage); + } else { + afLogger.warn(errorMessage); + afLogger.warn(`Ignore this warn, if you are using an sqlite database`); + } } return this.setFieldValue(field, value); } diff --git a/adminforth/dataConnectors/clickhouse.ts b/adminforth/dataConnectors/clickhouse.ts index 8e5d1ccf..c69b5e85 100644 --- a/adminforth/dataConnectors/clickhouse.ts +++ b/adminforth/dataConnectors/clickhouse.ts @@ -192,7 +192,7 @@ class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForth return iso; } } else if (field.type == AdminForthDataTypes.BOOLEAN) { - return value === null ? null : (value ? 1 : 0); + return value === null ? null : (value ? true : false); } else if (field.type == AdminForthDataTypes.JSON) { // check underline type is text or string if (field._underlineType.startsWith('String') || field._underlineType.startsWith('FixedString')) { diff --git a/adminforth/dataConnectors/mysql.ts b/adminforth/dataConnectors/mysql.ts index 54df749d..c6ab15e7 100644 --- a/adminforth/dataConnectors/mysql.ts +++ b/adminforth/dataConnectors/mysql.ts @@ -225,7 +225,7 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS } return dayjs(value).format('YYYY-MM-DD HH:mm:ss'); } else if (field.type == AdminForthDataTypes.BOOLEAN) { - return value === null ? null : (value ? 1 : 0); + return value === null ? null : (value ? true : false); } else if (field.type == AdminForthDataTypes.JSON) { if (field._underlineType === 'json') { return value; diff --git a/adminforth/dataConnectors/sqlite.ts b/adminforth/dataConnectors/sqlite.ts index e3407b29..f0db5562 100644 --- a/adminforth/dataConnectors/sqlite.ts +++ b/adminforth/dataConnectors/sqlite.ts @@ -176,6 +176,8 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData } return JSON.stringify(value); } else if (field.type == AdminForthDataTypes.BOOLEAN) { + // SQLite does not have a native boolean type, it uses 0 and 1 + // valid only for sqlite return value === null ? null : (value ? 1 : 0); } else if (field.type == AdminForthDataTypes.JSON) { // check underline type is text or string