Skip to main content

Module schemars0

Module schemars0 

Source
Available on crate feature schemars-0 only.
Expand description

schemars >=0.9, <1: a two-way bridge between vld and schemars.

  • vld → schemars: impl_json_schema! implements schemars::JsonSchema from a vld type’s json_schema(); nested vld::nested! types are added to the generator’s definitions, with $refs pointing at its definitions path, and an OpenAPI 3.0 generator gets OpenAPI 3.0 schemas.
  • schemars → vld: impl_vld_parse! validates a schemars::JsonSchema type against its schema (validate_with_schema) and implements vld::schema::VldParse, so it works with vld extractors.

The selected schemars is re-exported as schemars; the version-independent items of crate::validator are re-exported here.

aide: aide generates OpenAPI from schemars::JsonSchema, so impl_json_schema! makes a vld type usable in aide::axum::Json<T> and friends (nested types land under aide’s #/components/schemas/). Pick the module of the schemars major your aide uses (cargo tree -i schemars): aide 0.15 builds on schemars 0.9: aide 0.15 users use this module.

use vld_openapi::schemars0::prelude::*;
use vld_openapi::schemars0::schemars;
// vld → schemars: `CreateUser` implements `schemars::JsonSchema`.
vld::schema! {
    #[derive(Debug)]
    pub struct CreateUser {
        pub name: String => vld::string().min(2).max(100),
        pub email: String => vld::string().email(),
    }
}
impl_json_schema!(CreateUser);

// With a custom schema name.
vld::schema! {
    #[derive(Debug)]
    pub struct Req { pub x: String => vld::string() }
}
impl_json_schema!(Req, "CreateUserRequest");
assert_eq!(<Req as schemars::JsonSchema>::schema_name(), "CreateUserRequest");

// schemars → vld: validate an instance, validate + deserialize JSON, or go
// through `vld::schema::VldParse` (vld extractors).
#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
struct Item { name: String, qty: u32 }
impl_vld_parse!(Item);

let item = Item { name: "Widget".into(), qty: 5 };
assert!(item.vld_validate().is_ok());
let json = serde_json::json!({"name": "Widget", "qty": 5});
assert_eq!(Item::vld_parse(&json).unwrap().name, "Widget");
use vld::schema::VldParse;
assert_eq!(Item::vld_parse_value(&json).unwrap().qty, 5);

// Conversions: non-schemas fail closed (or are an error with `try_`).
let schema = vld_to_schemars(&serde_json::json!({"type": "string", "minLength": 2}));
assert_eq!(schema.get("type").unwrap(), "string");
assert_eq!(vld_to_schemars(&serde_json::json!(42)).as_bool(), Some(false));
assert!(try_vld_to_schemars(&serde_json::json!("string")).is_err());
use vld::openapi::JsonSchema as _;
let email = vld_schema_to_schemars(&vld::string().email().json_schema());
assert_eq!(email.get("format").unwrap(), "email");
assert!(validate_with_schemars(&schema, &serde_json::json!("hello")).is_ok());
assert!(validate_with_schemars(&schema, &serde_json::json!("x")).is_err());

assert!(schemars_to_json(&generate_schemars::<String>()).is_object());
assert_eq!(generate_from_schemars::<String>()["type"], "string");

let a = vld_to_schemars(&serde_json::json!({"type": "object", "properties": {"name": {"type": "string"}}}));
let b = vld_to_schemars(&serde_json::json!({"type": "object", "properties": {"age": {"type": "integer"}}}));
assert!(merge_schemas(&a, &b).get("allOf").is_some());

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

Re-exports§

pub use crate::validator::PropertyInfo;
pub use crate::validator::VldSchemarsError;
pub use crate::validator::get_property;
pub use crate::validator::is_required;
pub use crate::validator::list_properties;
pub use crate::validator::overlay_constraints;
pub use crate::validator::schema_type;
pub use crate::validator::schemas_equal;
pub use crate::validator::to_openapi3;
pub use crate::validator::validate_with_schema;
pub use ::schemars0 as schemars;
pub use ::vld;

Modules§

prelude
Common imports: the conversion functions and macros plus vld::prelude.

Macros§

impl_json_schema
Implement schemars::JsonSchema for a type with a json_schema() associated function (vld::schema!, #[derive(Validate)]): impl_json_schema!(T) or impl_json_schema!(T, "SchemaName"). See the module docs.
impl_vld_parse
Implement vld::schema::VldParse and SchemarsValidate for a type that derives schemars::JsonSchema, serde::Serialize and serde::Deserialize: impl_vld_parse!(T). The value is validated against the type’s schemars schema, then deserialized. See the module docs.

Traits§

SchemarsValidate
Trait for types that can be validated using their schemars::JsonSchema.

Functions§

generate_from_schemars
Generate a vld-compatible JSON Schema value from a type implementing schemars::JsonSchema.
generate_schemars
Generate a root schemars::Schema for a type implementing schemars::JsonSchema.
list_properties_schemars
Extract property info from a schemars::Schema (list_properties on its JSON).
merge_schemas
Merge two schemars::Schema into one using allOf.
schemars_to_json
Convert a schemars::Schema to a serde_json::Value: a standard JSON Schema for documentation, comparison or other tools.
try_vld_to_schemars
Like vld_to_schemars, but a value that is not a JSON Schema (neither an object nor a boolean) is an error.
validate_with_schemars
Validate a serde_json::Value against a schemars::Schema (validate_with_schema on its JSON).
vld_schema_to_schemars
Generate a schemars::Schema from the json_schema() of a vld schema or type (vld::schema! with the openapi feature, #[derive(Validate)]).
vld_to_schemars
Convert a serde_json::Value (JSON Schema produced by vld) into a schemars::Schema.