Skip to main content

Module utoipa6

Module utoipa6 

Source
Available on crate feature utoipa-6 only.
Expand description

utoipa 6: vld schemas as utoipa OpenAPI types.

impl_to_schema! implements utoipa’s PartialSchema, ToSchema and IntoParams from the vld json_schema() of a vld::schema! or #[derive(Validate)] type, so validation rules and OpenAPI docs come from one definition. Nested vld::nested! types are registered as components automatically. json_schema_to_schema converts any JSON Schema value. utoipa 6 is re-exported as utoipa.

Mark parameter structs with utoipa’s #[into_params(parameter_in = Query)] (or Path, Header, Cookie), or pass the location to the macro: impl_to_schema!(T, query). impl_to_schema!(T, "Name") sets the component name.

use vld_openapi::utoipa6::prelude::*;
use vld_openapi::utoipa6::utoipa;
// A request body.
vld::schema! {
    #[derive(Debug)]
    pub struct User {
        pub name: String => vld::string().min(2).max(50),
        pub email: String => vld::string().email(),
    }
}
impl_to_schema!(User);

// Query / path parameters: utoipa's own attribute picks the location.
vld::schema! {
    #[derive(Debug)]
    #[into_params(parameter_in = Query)]
    pub struct Search {
        pub q: String => vld::string().min(1).max(200),
    }
}
impl_to_schema!(Search);

// A custom component name.
vld::schema! {
    #[derive(Debug)]
    pub struct Req { pub x: String => vld::string() }
}
impl_to_schema!(Req, "CreateUserRequest");

// `User` can now be used in `#[utoipa::path(request_body = User)]`,
// `Search` in `params(Search)`.
use utoipa::{IntoParams, ToSchema};
assert_eq!(<Req as ToSchema>::name(), "CreateUserRequest");
assert_eq!(Search::into_params(|| None).len(), 1);

// Any JSON Schema value: `utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>`.
let schema = json_schema_to_schema(&serde_json::json!({
    "type": "object",
    "required": ["name"],
    "properties": { "name": { "type": "string", "minLength": 1 } }
}));
assert!(matches!(schema, utoipa::openapi::RefOr::T(_)));

Guide: https://s00d.github.io/vld/integrations/openapi/utoipa

Re-exports§

pub use ::utoipa6 as utoipa;

Modules§

prelude
The conversion functions, impl_to_schema! and vld::prelude.

Macros§

impl_to_schema
Bridge a vld type to utoipa ToSchema + IntoParams: impl_to_schema!(T), impl_to_schema!(T, "ComponentName"), or impl_to_schema!(T, query) (path, header, cookie) to force the parameter location. See the module docs.

Functions§

json_schema_to_params
Convert a vld object json_schema() into OpenAPI path/query parameters.
json_schema_to_schema
Convert a serde_json::Value (JSON Schema) produced by vld into a utoipa::openapi::RefOr<Schema>.
parameter_in_from_str
Map a parameter location hint from vld::openapi::OpenApiParameterIn to utoipa.