poem_openapi_validator_derive

Crates.iopoem_openapi_validator_derive
lib.rspoem_openapi_validator_derive
version0.1.1
sourcesrc
created_at2022-01-12 15:56:55.378523
updated_at2022-01-12 16:58:12.365983
descriptionpoem openapi validator
homepagehttps://github.com/tingfeng-key/poem_openapi_validator
repositoryhttps://github.com/tingfeng-key/poem_openapi_validator
max_upload_size
id512854
size15,588
听风 (tingfeng-key)

documentation

README

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())
        }
    }
}
Commit count: 9

cargo fmt