Skip to main content

Crate vld_sqlx

Crate vld_sqlx 

Source
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 bool that passed schema S (checked as {"value": b}).
VldFloat
A validated float column: an f64 that passed schema S (checked as {"value": x}).
VldInt
A validated integer column: an i64 that passed schema S (checked as {"value": n}).
VldText
A validated text column: a String that passed schema S (checked as {"value": "..."}; the schema needs a value field).

Enums§

VldSqlxError
Error returned by vld-sqlx operations.

Functions§

validate_insert
Validate a value against schema S before inserting (or updating).
validate_row
Validate a row loaded from the database against schema S (same check as validate_insert).
validate_rows
Validate a batch of rows against schema S.
validate_updateDeprecated
Same as validate_insert.