diff --git a/libs/app-error/src/lib.rs b/libs/app-error/src/lib.rs index c844e7be..d74dd029 100644 --- a/libs/app-error/src/lib.rs +++ b/libs/app-error/src/lib.rs @@ -161,6 +161,15 @@ pub enum AppError { workspace_id: Uuid, publish_name: String, }, + + #[error("There is an invalid character in the publish name: {character}")] + PublishNameInvalidCharacter { character: char }, + + #[error("The publish name is too long, given length: {given_length}, max length: {max_length}")] + PublishNameTooLong { + given_length: usize, + max_length: usize, + }, } impl AppError { @@ -232,6 +241,8 @@ impl AppError { AppError::AccessRequestAlreadyExists { .. } => ErrorCode::AccessRequestAlreadyExists, AppError::TooManyImportTask(_) => ErrorCode::TooManyImportTask, AppError::PublishNameAlreadyExists { .. } => ErrorCode::PublishNameAlreadyExists, + AppError::PublishNameInvalidCharacter { .. } => ErrorCode::PublishNameInvalidCharacter, + AppError::PublishNameTooLong { .. } => ErrorCode::PublishNameTooLong, } } } @@ -368,6 +379,8 @@ pub enum ErrorCode { CustomNamespaceTooLong = 1048, CustomNamespaceReserved = 1049, PublishNameAlreadyExists = 1050, + PublishNameInvalidCharacter = 1051, + PublishNameTooLong = 1052, } impl ErrorCode { diff --git a/src/biz/workspace/publish.rs b/src/biz/workspace/publish.rs index 69466452..9ca6149f 100644 --- a/src/biz/workspace/publish.rs +++ b/src/biz/workspace/publish.rs @@ -61,19 +61,20 @@ async fn check_workspace_owner_or_publisher( } fn check_collab_publish_name(publish_name: &str) -> Result<(), AppError> { + const MAX_PUBLISH_NAME_LENGTH: usize = 128; + // Check len - if publish_name.len() > 128 { - return Err(AppError::InvalidRequest( - "Publish name must be at most 128 characters long".to_string(), - )); + if publish_name.len() > MAX_PUBLISH_NAME_LENGTH { + return Err(AppError::PublishNameTooLong { + given_length: publish_name.len(), + max_length: MAX_PUBLISH_NAME_LENGTH, + }); } // Only contain alphanumeric characters and hyphens for c in publish_name.chars() { if !c.is_alphanumeric() && c != '-' { - return Err(AppError::InvalidRequest( - "Publish name must only contain alphanumeric characters and hyphens".to_string(), - )); + return Err(AppError::PublishNameInvalidCharacter { character: c }); } } diff --git a/tests/workspace/publish.rs b/tests/workspace/publish.rs index 952b6f7a..1f1eafa8 100644 --- a/tests/workspace/publish.rs +++ b/tests/workspace/publish.rs @@ -90,6 +90,50 @@ async fn test_publish_doc() { .await .unwrap(); + { + // Invalid publish name + let err = c + .publish_collabs::( + &workspace_id, + vec![PublishCollabItem { + meta: PublishCollabMetadata { + view_id: uuid::Uuid::new_v4(), + publish_name: "(*&^%$#!".to_string(), // invalid chars + metadata: MyCustomMetadata { + title: "my_title_1".to_string(), + }, + }, + data: "yrs_encoded_data_1".as_bytes(), + }], + ) + .await + .unwrap_err(); + assert_eq!( + err.code, + ErrorCode::PublishNameInvalidCharacter, + "{:?}", + err + ); + // Publish name too long + let err = c + .publish_collabs::( + &workspace_id, + vec![PublishCollabItem { + meta: PublishCollabMetadata { + view_id: uuid::Uuid::new_v4(), + publish_name: "a".repeat(1001), // too long + metadata: MyCustomMetadata { + title: "my_title_1".to_string(), + }, + }, + data: "yrs_encoded_data_1".as_bytes(), + }], + ) + .await + .unwrap_err(); + assert_eq!(err.code, ErrorCode::PublishNameTooLong, "{:?}", err); + } + let publish_name_1 = "publish-name-1"; let view_id_1 = uuid::Uuid::new_v4(); let publish_name_2 = "publish-name-2";