Skip to main content

Crate vld_utoipa

Crate vld_utoipa 

Source
Expand description

§vld-utoipa — Bridge between vld and utoipa

This crate lets you use vld validation schemas as the single source of truth for both runtime validation and OpenAPI documentation generated by utoipa.

Instead of duplicating schema definitions with #[derive(ToSchema)] and vld::schema!, you define validation rules once in vld and get utoipa compatibility for free.

§Quick Start

use vld::prelude::*;
use vld_utoipa::impl_to_schema;

// 1. Define your validated struct as usual
vld::schema! {
    #[derive(Debug)]
    pub struct User {
        pub name: String => vld::string().min(2).max(50),
        pub email: String => vld::string().email(),
    }
}

// 2. Bridge to utoipa — one line
impl_to_schema!(User);

// Now `User` implements `utoipa::ToSchema` and can be used in
// `#[utoipa::path]` annotations.

§Converting arbitrary JSON Schema

use vld_utoipa::json_schema_to_schema;

let json_schema = serde_json::json!({
    "type": "object",
    "required": ["name"],
    "properties": {
        "name": { "type": "string", "minLength": 1 }
    }
});

let utoipa_schema = json_schema_to_schema(&json_schema);
// Returns `utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>`

Re-exports§

pub use utoipa;

Modules§

prelude
Prelude module — import everything you need.

Macros§

impl_to_schema
Implement utoipa::PartialSchema and utoipa::ToSchema for a type that has a json_schema() associated function (generated by vld::schema! with the openapi feature enabled).

Functions§

json_schema_to_schema
Convert a serde_json::Value (JSON Schema) produced by vld into a utoipa::openapi::RefOr<Schema>.