vld-utoipa
Bridge between vld validation library and utoipa OpenAPI documentation.
Define validation rules once with vld and automatically get utoipa::ToSchema
implementation — no need to duplicate schema definitions.
Installation
[]
= { = "0.1", = ["openapi"] }
= "0.1"
= "5"
Quick Start
use *;
use impl_to_schema;
// 1. Define validated struct as usual
schema!
// 2. One line to bridge to utoipa
impl_to_schema!;
// Now CreateUser implements utoipa::ToSchema and can be used in
// #[utoipa::path(post, path = "/users", request_body = CreateUser)]
Using with #[derive(Validate)]
impl_to_schema! also works with #[derive(Validate)] from vld-derive.
This lets you use standard Rust struct syntax with serde attributes like
#[serde(rename_all = "camelCase")] and still get OpenAPI schema generation.
[]
= { = "0.1", = ["derive", "openapi"] }
= "0.1"
= "5"
use Validate;
use impl_to_schema;
impl_to_schema!;
// OpenAPI schema properties use camelCase:
// "streetAddress", "streetNumber", "streetNumberAddition", "isActive"
Nested Schemas (auto-registration)
When you use vld::nested!(Type), the nested type is automatically registered in
utoipa's components/schemas. No need to list it manually in #[openapi(components(schemas(...)))].
use *;
use impl_to_schema;
schema!
impl_to_schema!;
schema!
impl_to_schema!;
// In OpenAPI spec:
// - CreateUser.address → { "$ref": "#/components/schemas/Address" }
// - Address schema is auto-registered in components
Custom Schema Name
impl_to_schema!;
Converting Arbitrary JSON Schema
use json_schema_to_schema;
let json_schema = json!;
let utoipa_schema = json_schema_to_schema;
Supported JSON Schema Features
- Primitive types:
string,number,integer,boolean,null - Object with
propertiesandrequired - Array with
items,minItems,maxItems oneOf,allOfcompositesenumvalues- String:
minLength,maxLength,pattern,format - Number:
minimum,maximum,exclusiveMinimum,exclusiveMaximum,multipleOf $refreferencesdescription,default,example,title
Running the Example
License
MIT