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!implementsschemars::JsonSchemafrom a vld type’sjson_schema(); nestedvld::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 aschemars::JsonSchematype against its schema (validate_with_schema) and implementsvld::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::JsonSchemafor a type with ajson_schema()associated function (vld::schema!,#[derive(Validate)]):impl_json_schema!(T)orimpl_json_schema!(T, "SchemaName"). See the module docs. - impl_
vld_ parse - Implement
vld::schema::VldParseandSchemarsValidatefor a type that derivesschemars::JsonSchema,serde::Serializeandserde::Deserialize:impl_vld_parse!(T). The value is validated against the type’s schemars schema, then deserialized. See the module docs.
Traits§
- Schemars
Validate - 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::Schemafor a type implementingschemars::JsonSchema. - list_
properties_ schemars - Extract property info from a
schemars::Schema(list_propertieson its JSON). - merge_
schemas - Merge two
schemars::Schemainto one usingallOf. - schemars_
to_ json - Convert a
schemars::Schemato aserde_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::Valueagainst aschemars::Schema(validate_with_schemaon its JSON). - vld_
schema_ to_ schemars - Generate a
schemars::Schemafrom thejson_schema()of a vld schema or type (vld::schema!with theopenapifeature,#[derive(Validate)]). - vld_
to_ schemars - Convert a
serde_json::Value(JSON Schema produced byvld) into aschemars::Schema.