#validation #rocket #json #data #validate-json

rocket-validation

Rocket Guards to support validation using validator

5 releases

0.2.0 Feb 13, 2024
0.1.4 Feb 13, 2024
0.1.3 Oct 3, 2022
0.1.2 Jul 28, 2022
0.1.0 Mar 20, 2022

#1207 in Web programming

Download history 24/week @ 2023-12-24 20/week @ 2024-01-07 21/week @ 2024-01-14 30/week @ 2024-01-21 12/week @ 2024-01-28 15/week @ 2024-02-04 99/week @ 2024-02-11 44/week @ 2024-02-18 61/week @ 2024-02-25 140/week @ 2024-03-03 70/week @ 2024-03-10 18/week @ 2024-03-17 18/week @ 2024-03-24 62/week @ 2024-03-31 13/week @ 2024-04-07

114 downloads per month
Used in 3 crates (via open-stock)

MIT license

19KB
121 lines

Rocket Validation

Welcome to the Rocket Validation crate. If you are looking to validate your Json, Form or Query structs using Rocket you have come to the right place!

Why

Rocket is using Rusts powerful typing system. Which is amazing because you can be sure its what you want. But is it? How about kebab-case strings or phone number inputs, these aren’t really types. You could implement a custom deserializer for a wrapped type or write custom logic to validate it on endpoint calls, thats error prone and not ergonomic and doesn't allow you to return meaningful and contextual errors.

If you are coming from TypeScript you might have heard of class-validator which is simple, declarative and can be implemented into middleware. Using validator this crate achieves a similar result using rockets guard mechanism.

Anything implementing Json, FromRequest or FromForm as well as Validate are able to use the Validated guard of this crate, so you can be sure your data is validated once you receive it in your handler.

Using rockets catchers you are able to route errors which occurs during validation to your user.

Current validation in rocket: Rocket has validation for FromForm structs but for nothing else.

Usage

In order to get going, you need to depend on the rocket-validation. Add this to your Cargo.toml

[dependencies]
rocket-validation = "0.1.3"
validator="?"

validator is needed as the derive macros of the crate validator generate code dependent on it being available in a global scope

Now you can go on and implement your Validation

// Because we use rocket....
#[macro_use]
extern crate rocket;

// Some types for Json types
use rocket::serde::{json::Json, Deserialize, Serialize};

// Will be important for validation....
use rocket_validation::{Validate, Validated};

#[derive(Debug, Deserialize, Serialize, Validate)] // Implements `Validate`
#[serde(crate = "rocket::serde")]
pub struct HelloData {
	#[validate(length(min = 1))] // Your validation annotation
	name: String,
	#[validate(range(min = 0, max = 100))] // Your validation annotation
	age: u8,
}

#[post("/hello", format = "application/json", data = "<data>")]
fn validated_hello(data: /*Uses the `Validated` type*/ Validated<Json<HelloData>>) -> Json<HelloData> {
	Json(data.into_inner())
}

#[launch]
fn rocket() -> _ {
	rocket::build()
		.mount("/", routes![validated_hello])
}

Exposing errors to clients

Before you use the following, you should be aware of what errors you expose to your clients as well as what that means for security.

If you would like to respond invalid requests with some custom messages, you can implement the validation_catcher catcher to do so.

#[launch]
fn rocket() -> _ {
	rocket::build()
		.mount("/", routes![validated_hello])
		.register("/", catchers![rocket_validation::validation_catcher])
}

Currently limited to Json or FromData validations due to inernal rocket limitations

Dependencies

~20–55MB
~1M SLoC