Merge pull request #13179 from SergeTupchiy/dashboard-swagger-apply-body-validator

feat(emqx_dashboard_swagger): apply field validator in request body check
This commit is contained in:
SergeTupchiy 2024-06-05 21:25:07 +03:00 committed by GitHub
commit 64862b296c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 1 deletions

View File

@ -575,7 +575,14 @@ check_request_body(#{body := Body}, Schema, Module, CheckFun, true) ->
?REF(StructName) -> ?R_REF(Module, StructName); ?REF(StructName) -> ?R_REF(Module, StructName);
_ -> Type0 _ -> Type0
end, end,
NewSchema = ?INIT_SCHEMA#{roots => [{root, Type}]}, Validations =
case hocon_schema:field_schema(Schema, validator) of
undefined ->
[];
Fun when is_function(Fun) ->
[{validator, fun(#{<<"root">> := B}) -> Fun(B) end}]
end,
NewSchema = ?INIT_SCHEMA#{roots => [{root, Type}], validations => Validations},
Option = #{required => false}, Option = #{required => false},
#{<<"root">> := NewBody} = CheckFun(NewSchema, #{<<"root">> => Body}, Option), #{<<"root">> := NewBody} = CheckFun(NewSchema, #{<<"root">> => Body}, Option),
{ok, NewBody}; {ok, NewBody};