fix(bridges): validate empty topics for mqtt bridge

This commit is contained in:
Shawn 2022-03-11 20:16:03 +08:00
parent 0b4b3a7cf0
commit 92a9fe46ea
1 changed files with 12 additions and 1 deletions

View File

@ -28,6 +28,8 @@
, egress_desc/0 , egress_desc/0
]). ]).
-export([non_empty_string/1]).
-import(emqx_schema, [mk_duration/2]). -import(emqx_schema, [mk_duration/2]).
roots() -> roots() ->
@ -101,6 +103,7 @@ fields("ingress") ->
[ {remote_topic, [ {remote_topic,
sc(binary(), sc(binary(),
#{ required => true #{ required => true
, validator => fun ?MODULE:non_empty_string/1
, desc => "Receive messages from which topic of the remote broker" , desc => "Receive messages from which topic of the remote broker"
})} })}
, {remote_qos, , {remote_qos,
@ -110,7 +113,8 @@ fields("ingress") ->
})} })}
, {local_topic, , {local_topic,
sc(binary(), sc(binary(),
#{ desc => """ #{ validator => fun ?MODULE:non_empty_string/1
, desc => """
Send messages to which topic of the local broker.<br> Send messages to which topic of the local broker.<br>
Template with variables is allowed. Template with variables is allowed.
""" """
@ -135,10 +139,12 @@ fields("egress") ->
[ {local_topic, [ {local_topic,
sc(binary(), sc(binary(),
#{ desc => "The local topic to be forwarded to the remote broker" #{ desc => "The local topic to be forwarded to the remote broker"
, validator => fun ?MODULE:non_empty_string/1
})} })}
, {remote_topic, , {remote_topic,
sc(binary(), sc(binary(),
#{ default => <<"${topic}">> #{ default => <<"${topic}">>
, validator => fun ?MODULE:non_empty_string/1
, desc => """ , desc => """
Forward to which topic of the remote broker.<br> Forward to which topic of the remote broker.<br>
Template with variables is allowed. Template with variables is allowed.
@ -233,5 +239,10 @@ Template with variables is allowed."""
qos() -> qos() ->
hoconsc:union([emqx_schema:qos(), binary()]). hoconsc:union([emqx_schema:qos(), binary()]).
non_empty_string(<<>>) -> {error, empty_string_not_allowed};
non_empty_string("") -> {error, empty_string_not_allowed};
non_empty_string(S) when is_binary(S); is_list(S) -> ok;
non_empty_string(_) -> {error, invalid_string}.
sc(Type, Meta) -> hoconsc:mk(Type, Meta). sc(Type, Meta) -> hoconsc:mk(Type, Meta).
ref(Field) -> hoconsc:ref(?MODULE, Field). ref(Field) -> hoconsc:ref(?MODULE, Field).