Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How validate generated by protoc models? #2055

Open
rdcm opened this issue Nov 18, 2024 · 3 comments
Open

Question: How validate generated by protoc models? #2055

rdcm opened this issue Nov 18, 2024 · 3 comments

Comments

@rdcm
Copy link

rdcm commented Nov 18, 2024

Hi,

I am currently actively developing an application using tonic and have now encountered a task to validate input data.
Is there any built-in validation mechanism in tonic?

Something like this:

#[derive(Debug, Validate, Deserialize)]
struct SignupData {
    #[validate(email)]
    mail: String,
    #[validate(url)]
    site: String,
    #[validate(length(min = 1), custom(function = "validate_unique_username"))]
    #[serde(rename = "firstName")]
    first_name: String,
    #[validate(range(min = 18, max = 20))]
    age: u32,
    #[validate(range(exclusive_min = 0.0, max = 100.0))]
    height: f32,
}

Project: https://github.com/rdcm/news-board

Thanks

@imotai
Copy link

imotai commented Nov 20, 2024

Great idea! I think moving the request to https://github.com/tokio-rs/prost is better

@rdcm
Copy link
Author

rdcm commented Nov 21, 2024

@imotai Which way preferred for validate requests models in tonic app at this moment without feature request to prost? Quick solution, writing code in each endpoint like this: is_valid(req)?.

@imotai
Copy link

imotai commented Nov 23, 2024

@imotai Which way preferred for validate requests models in tonic app at this moment without feature request to prost? Quick solution, writing code in each endpoint like this: is_valid(req)?.

You can add an interceptor

fn intercept(req: Request<()>) -> Result<Request<()>, Status> {
     is_valid(req)
}

Server setup

let greeter = MyGreeter::default();
    let svc = GreeterServer::with_interceptor(greeter, intercept);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants