{"record":{"id":"d77cd751cbbd708d","repo":"jstedfast/MailKit","slug":"the-keyword-cannot-be-an-empty-string","errorCode":null,"errorMessage":"The keyword cannot be an empty string.","messagePattern":"The keyword cannot be an empty string\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Search/SearchQuery.cs","lineNumber":489,"sourceCode":"\t\t/// <para>Matches messages that have the specified keyword set.</para>\n\t\t/// <para>A keyword is a user-defined message flag that can be set (or unset) on a message.</para>\n\t\t/// <note type=\"note\">This is equivalent to the <c>KEYWORD</c> search key as defined in <a href=\"https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4\">rfc3501</a>.</note>\n\t\t/// </remarks>\n\t\t/// <returns>A <see cref=\"TextSearchQuery\"/>.</returns>\n\t\t/// <param name=\"keyword\">The keyword.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"keyword\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"keyword\"/> is empty.\n\t\t/// </exception>\n\t\tpublic static TextSearchQuery HasKeyword (string keyword)\n\t\t{\n\t\t\tif (keyword == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (keyword));\n\n\t\t\tif (keyword.Length == 0)\n\t\t\t\tthrow new ArgumentException (\"The keyword cannot be an empty string.\", nameof (keyword));\n\n\t\t\treturn new TextSearchQuery (SearchTerm.Keyword, keyword);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Match messages that have all of the specified keywords set.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Matches messages that have all of the specified keywords set.</para>\n\t\t/// <para>A keyword is a user-defined message flag that can be set (or unset) on a message.</para>\n\t\t/// <note type=\"note\">This is equivalent to AND-ing multiple <c>KEYWORD</c> search keys as defined in <a href=\"https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4\">rfc3501</a>.</note>\n\t\t/// </remarks>\n\t\t/// <returns>A <see cref=\"SearchQuery\"/>.</returns>\n\t\t/// <param name=\"keywords\">The keywords.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"keywords\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Search/SearchQuery.cs#L471-L507","documentation":"SearchQuery.HasKeyword rejects empty strings because an IMAP KEYWORD search term must be a non-empty atom; an empty keyword would produce an invalid search command. It throws ArgumentException with 'The keyword cannot be an empty string.'","triggerScenarios":"Calling SearchQuery.HasKeyword(\"\") or HasKeyword(userInput) where the user submitted a blank keyword.","commonSituations":"Blank form fields for custom label/flag search; Trim() removing all characters from user input before the call; config file with an empty keyword value.","solutions":["Check `if (!string.IsNullOrWhiteSpace(keyword))` before calling HasKeyword.","Trim user input and reject empty values with a friendlier error before building the query.","Provide a default keyword or skip this criterion when the keyword is empty."],"exampleFix":"// before\nvar query = SearchQuery.HasKeyword(input.Trim());\n// after\nvar kw = input.Trim();\nif (kw.Length == 0)\n    throw new ValidationException(\"Keyword is required.\");\nvar query = SearchQuery.HasKeyword(kw);","handlingStrategy":"validation","validationCode":"var kw = keyword?.Trim();\nif (string.IsNullOrEmpty(kw))\n    throw new ValidationException(\"Keyword must be a non-empty string.\");\nvar query = SearchQuery.HasKeyword(kw);","typeGuard":"bool IsValidKeyword(string keyword) => !string.IsNullOrWhiteSpace(keyword);","tryCatchPattern":"try { query = SearchQuery.HasKeyword(keyword); }\ncatch (ArgumentException ex) { throw new ValidationException(ex.Message, ex); }","preventionTips":["Trim user input and reject blanks before calling HasKeyword.","Validate search-form fields in the UI layer.","Treat empty keyword as 'omit criterion' rather than passing it through."],"tags":["argument-validation","empty-string","search-query","mailkit"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}