添加参数校验处理

This commit is contained in:
李运家 2024-04-12 09:33:14 +08:00
parent 3c09789a2c
commit f9be9e406b
4 changed files with 26 additions and 4 deletions

16
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

6
Cargo.lock generated
View File

@ -82,6 +82,7 @@ name = "api"
version = "0.1.0"
dependencies = [
"axum",
"axum-extra",
"domain",
"library",
"service",
@ -212,9 +213,9 @@ dependencies = [
[[package]]
name = "axum-extra"
version = "0.9.2"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "895ff42f72016617773af68fb90da2a9677d89c62338ec09162d4909d86fdd8f"
checksum = "0be6ea09c9b96cb5076af0de2e383bd2bc0c18f827cf1967bdd353e0b910d733"
dependencies = [
"axum",
"axum-core",
@ -229,6 +230,7 @@ dependencies = [
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]

View File

@ -15,3 +15,4 @@ validator = { version = "0.17", features = ["derive"] }
library = { path = "../library" }
domain = { path = "../domain" }
service = { path = "../service" }
axum-extra = "0.9.3"

View File

@ -1,9 +1,12 @@
use axum::Json;
use axum_extra::extract::WithRejection;
use domain::dto::game_account::GameAccountCreate;
use library::resp::response::{ResErr, ResOK, ResResult};
use library::resp::{rejection::IRejection, response::{ResErr, ResOK, ResResult}};
use validator::Validate;
pub async fn create(Json(req): Json<GameAccountCreate>) -> ResResult<ResOK<()>> {
pub async fn create(
WithRejection(Json(req), _): IRejection<Json<GameAccountCreate>>
) -> ResResult<ResOK<()>> {
if let Err(err) = req.validate() {
return Err(ResErr::ErrParams(Some(err.to_string())));
}