SboxSuperuser/Core/Commands/CommandRootName.cs

Validation helper for command root names in the SboxSuperuser command system. Checks the name is 1 to 32 characters, lowercase letters, digits or underscores, starting with a letter, and rejects the reserved names 'superuser' and 'superuser_request'. Returns the name or throws an ArgumentException.

namespace SboxSuperuser.Core.Commands;

internal static class CommandRootName
{
	internal static string Validate( string name )
	{
		if ( string.IsNullOrEmpty( name ) || name.Length > 32 || name[ 0 ] is not (>= 'a' and <= 'z')
			|| name.Any( value => value is not (>= 'a' and <= 'z' or >= '0' and <= '9' or '_') ) )
		{
			throw new ArgumentException( "Root names require 1–32 lowercase letters, digits or underscores, starting with a letter.", nameof( name ) );
		}

		if ( name is "superuser" or "superuser_request" )
		{
			throw new ArgumentException( "This name is reserved for the fixed native transport.", nameof( name ) );
		}

		return name;
	}
}