vld-schemars
Bidirectional bridge between vld and schemars.
Overview
Many Rust libraries already use schemars for JSON Schema generation — aide, paperclip, okapi,
dropshot, etc. This crate lets you share schema definitions between vld and the broader
schemars ecosystem in both directions:
| Direction | Function | Description |
|---|---|---|
| vld → schemars | vld_to_schemars() |
Convert vld JSON Schema to schemars::Schema |
| vld → schemars | impl_json_schema!() |
Implement schemars::JsonSchema for vld types |
| schemars → vld | schemars_to_json() |
Convert schemars::Schema to serde_json::Value |
| schemars → vld | generate_from_schemars::<T>() |
Get JSON Schema value from any schemars::JsonSchema type |
Plus introspection, comparison, and schema merge utilities.
Difference from vld-aide
vld-aide is specifically for the aide OpenAPI framework.
vld-schemars is a general-purpose bridge usable with any library in the schemars ecosystem.
Installation
[]
= { = "0.1", = ["openapi"] }
= "0.1"
Quick Start
vld → schemars (implement JsonSchema for vld types)
use *;
use impl_json_schema;
schema!
// One line — User now works with any schemars-based library
impl_json_schema!;
// Custom schema name
impl_json_schema!;
vld → schemars (convert JSON value)
use JsonSchema;
let vld_json = string.email.json_schema;
let schemars_schema = vld_to_schemars;
assert_eq!;
schemars → vld (extract JSON from schemars)
let schemars_schema = ;
let json_value = schemars_to_json;
// Now you have a serde_json::Value JSON Schema
schemars → vld (generate from any JsonSchema type)
let schema = ;
// Returns serde_json::Value with the full JSON Schema
schemars → vld (impl_vld_parse!)
The reverse of impl_json_schema!. Attach a macro to a schemars type and get full vld integration:
use ;
impl_vld_parse!;
Now User has:
.vld_validate() — validate existing instance
let user = User ;
user.vld_validate.unwrap;
Type::vld_validate_json() — validate JSON against type's schema
let json = json!;
vld_validate_json.unwrap;
let bad = json!; // missing age
assert!;
Type::vld_parse() — validate + deserialize
let json = json!;
let user = vld_parse.unwrap;
assert_eq!;
VldParse — for vld extractors (axum, actix, etc.)
use VldParse;
let user = vld_parse_value.unwrap;
Supported JSON Schema keywords
type, required, properties, items, minLength, maxLength,
minimum, maximum, exclusiveMinimum, exclusiveMaximum, pattern,
format, enum, const, minItems, maxItems, uniqueItems,
minProperties, maxProperties, multipleOf, oneOf, anyOf, allOf, not.
Introspection
use *;
schema!
let schema = json_schema;
// List all properties
for prop in list_properties
// Check specific fields
assert!;
assert_eq!;
let name = get_property.unwrap;
assert_eq!;
Schema Composition
Merge (allOf)
let a = vld_to_schemars;
let b = vld_to_schemars;
let merged = merge_schemas;
// Result: {"allOf": [a, b]}
Overlay constraints
let base = json!;
let extra = json!;
let result = overlay_constraints;
// name now has minLength=2 and is required
API Reference
vld → schemars
| Function | Description |
|---|---|
vld_to_schemars(&Value) |
Convert JSON value to schemars::Schema |
vld_schema_to_schemars(&Value) |
Same, convenience alias |
impl_json_schema!(Type) |
Implement schemars::JsonSchema for a vld type |
impl_json_schema!(Type, "Name") |
Same, with custom schema name |
schemars → vld
| API | Description |
|---|---|
impl_vld_parse!(Type) |
Macro: impl VldParse + SchemarsValidate for a schemars type |
.vld_validate() |
Validate existing instance (via SchemarsValidate trait) |
Type::vld_validate_json(&Value) |
Validate JSON against type's schema |
Type::vld_parse(&Value) |
Validate + deserialize from JSON |
Type::vld_parse_value(&Value) |
VldParse impl for vld extractors |
schemars_to_json(&Schema) |
Convert schemars::Schema to serde_json::Value |
generate_from_schemars::<T>() |
Generate JSON value from schemars::JsonSchema type |
validate_with_schema(&Value, &Value) |
Low-level: validate data against a raw JSON Schema |
Introspection & Composition
| Function | Description |
|---|---|
list_properties(&Value) |
Extract property info from object schema |
list_properties_schemars(&Schema) |
Same, for schemars::Schema |
schema_type(&Value) |
Get the "type" field |
is_required(&Value, &str) |
Check if field is required |
get_property(&Value, &str) |
Get property sub-schema |
schemas_equal(&Value, &Value) |
Structural equality comparison |
merge_schemas(&Schema, &Schema) |
Merge via allOf |
overlay_constraints(&Value, &Value) |
Overlay properties/required non-destructively |
Running the example
License
MIT