Expand description
§vld-sqlx — SQLx integration for vld
Validate values against a vld schema before writing them with SQLx
(validate_insert, Validated) or after loading them
(validate_row, validate_rows), and bind validated column wrappers
(VldText, VldInt, VldFloat, VldBool) that implement
sqlx::Type, Encode and Decode.
Features: sqlx-0_8 (default) or sqlx-0_9 — exactly one; sqlite
(default), postgres, mysql.
use vld_sqlx::prelude::*;
vld::schema! {
#[derive(Debug)]
pub struct NewUserSchema {
pub name: String => vld::string().min(1).max(100),
pub email: String => vld::string().email(),
}
}
// Column schemas validate `{"value": ...}`.
vld::schema! {
#[derive(Debug)]
pub struct EmailField {
pub value: String => vld::string().email(),
}
}
#[derive(serde::Serialize)]
struct NewUser { name: String, email: String }
let user = NewUser { name: "Alice".into(), email: "alice@example.com".into() };
validate_insert::<NewUserSchema, _>(&user).unwrap();
// sqlx::query("INSERT ...").bind(&user.name).bind(&user.email).execute(&pool).await?;
let email = VldText::<EmailField>::new("carol@example.com").unwrap(); // bind(&email)
assert!(VldText::<EmailField>::new("bad").is_err());Decoding does not re-validate (data from the database is trusted); call
validate_row to re-check loaded rows.
Guide: https://s00d.github.io/vld/integrations/database/sqlx
Re-exports§
pub use vld;
Modules§
- prelude
- Common imports: the sqlx wrappers plus
vld::prelude.
Structs§
- Validated
- A wrapper that proves its inner value has been validated against schema
S. - VldBool
- A validated boolean column: a
boolthat passed schemaS(checked as{"value": b}). - VldFloat
- A validated float column: an
f64that passed schemaS(checked as{"value": x}). - VldInt
- A validated integer column: an
i64that passed schemaS(checked as{"value": n}). - VldText
- A validated text column: a
Stringthat passed schemaS(checked as{"value": "..."}; the schema needs avaluefield).
Enums§
- VldSqlx
Error - Error returned by
vld-sqlxoperations.
Functions§
- validate_
insert - Validate a value against schema
Sbefore inserting (or updating). - validate_
row - Validate a row loaded from the database against schema
S(same check asvalidate_insert). - validate_
rows - Validate a batch of rows against schema
S. - validate_
update Deprecated - Same as
validate_insert.