mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 19:58:57 +08:00
Add validation for topics before publishing.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using MQTTnet.Exceptions;
|
||||
|
||||
namespace MQTTnet.Protocol
|
||||
{
|
||||
public static class MqttTopicValidator
|
||||
{
|
||||
public static void ThrowIfInvalid(string topic)
|
||||
{
|
||||
if (string.IsNullOrEmpty(topic))
|
||||
{
|
||||
throw new MqttProtocolViolationException("Topic should not be empty.");
|
||||
}
|
||||
|
||||
if (topic.Contains("+"))
|
||||
{
|
||||
throw new MqttProtocolViolationException("The character '+' is not allowed in topics.");
|
||||
}
|
||||
|
||||
if (topic.Contains("#"))
|
||||
{
|
||||
throw new MqttProtocolViolationException("The character '#' is not allowed in topics.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user