From 55a352f85a73d6d8d81439ad3e9fcfed56894bc2 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 22 Apr 2024 04:26:25 +0200 Subject: [PATCH] Add secondary check to prevent empty tags from being created --- emgauwa-lib/src/db/tag.rs | 4 ++++ emgauwa-lib/src/errors/database_error.rs | 2 ++ 2 files changed, 6 insertions(+) diff --git a/emgauwa-lib/src/db/tag.rs b/emgauwa-lib/src/db/tag.rs index c6a374b..25028e1 100644 --- a/emgauwa-lib/src/db/tag.rs +++ b/emgauwa-lib/src/db/tag.rs @@ -17,6 +17,10 @@ impl DbTag { conn: &mut PoolConnection, new_tag: &str, ) -> Result { + if new_tag.len() == 0 { + return Err(DatabaseError::EmptyDataInsert); + } + sqlx::query_as!( DbTag, "INSERT INTO tags (tag) VALUES (?) RETURNING *", diff --git a/emgauwa-lib/src/errors/database_error.rs b/emgauwa-lib/src/errors/database_error.rs index 3f8c482..008e69a 100644 --- a/emgauwa-lib/src/errors/database_error.rs +++ b/emgauwa-lib/src/errors/database_error.rs @@ -12,6 +12,7 @@ pub enum DatabaseError { InsertGetError, NotFound, Protected, + EmptyDataInsert, UpdateError, UpdateGetError, MigrationError(MigrateError), @@ -57,6 +58,7 @@ impl From<&DatabaseError> for String { } DatabaseError::MigrationError(_) => "error on running migrations", DatabaseError::Unknown(_) => "unknown error", + DatabaseError::EmptyDataInsert => "empty data was attempted to be inserted", }) } }