[CSHARP-428] Add new IsDatabaseNameValid and IsCollectionNameValid methods Created: 03/Apr/12  Updated: 02/Apr/15  Resolved: 04/Apr/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.4
Fix Version/s: 1.4.1

Type: Improvement Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Some types of applications (like management UIs) have a need to test whether a given database or collection name is valid before actually trying to create a database or collection object. These methods allow testing a name without attempting to create a database or collection object.

IsDatabaseNameValid will be an instance method of MongoServer in case at some point in the future the validity of a database name is a function of the operating system the server is running on or the version of the server.

IsCollectionNameValid will be an instance method of MongoDatabase for similar reasons.



 Comments   
Comment by Roman Kuzmin [ 05/Apr/12 ]

Please, take a look at this post:
http://stackoverflow.com/a/4650483/323582

It seems to show a more effective (or is it just more concise?) way with `IndexOfAny()`.

Also, at least on Windows, there are more troublesome names than one may expect:
http://stackoverflow.com/a/62888/323582

Perhaps, it makes sense to take this into account if the goal is to make it really bulletproof.

In one of my applications I recently started to use this approach:

/// <summary>
/// Gets true if a string is not a valid file system file name.
/// </summary>
public static bool IsInvalidFileName(string name)
{
    if (string.IsNullOrEmpty(name))
        return true;
 
    if (_invalidName == null)
        _invalidName = new Regex("[" + Regex.Escape(new string(Path.GetInvalidFileNameChars())) + @"]|[\s.]$|^(?:CON|PRN|AUX|NUL|(?:COM|LPT)[1-9])$", RegexOptions.IgnoreCase);
 
    return _invalidName.IsMatch(name);
}
static Regex _invalidName;

Comment by ??? [ 05/Apr/12 ]

Thank you.

Generated at Wed Feb 07 21:36:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.