#validation #poem-openapi

poem_openapi_validator_derive

poem_openapi验证器

2 个版本

0.1.1 2022年1月12日
0.1.0 2022年1月12日

#37#validator

自定义许可

6KB

poem_openapi的validator集成

示例

register.rs

use validator::Validate;
use poem_openapi::Object;
use poem_openapi_validator_derive::Validation;

#[derive(Object, Validate, Validation)]
pub struct Register {
    #[validate(length(min = 6, max = 20, message = "user field length error"))]
    pub username: String,
    #[validate(length(min = 6, max = 20, message = "password field length error"))]
    pub password: String,
}

auth.rs

use poem_openapi::{payload::PlainText, OpenApi};
use poem_openapi::payload::Json;

#[OpenApi]
impl Authorize {
    #[oai(path="/register", method="post")]
    async fn register(&self, register: Json<Register>) -> PlainText<String> {
        //validator request
        match register.validator_is_error() {
            Some(error) => PlainText(error),
            None => PlainText("ok".to_string())
        }
    }
}

依赖项

~4.5–6.5MB
~152K SLoC