vld-sea 0.3.0

SeaORM integration for the vld validation library — validate ActiveModel before insert/update
Documentation

Crates.io docs.rs License Platform GitHub issues GitHub stars

vld-sea

SeaORM integration for the vld validation library.

Validate ActiveModel fields before insert() / update() hits the database.

Features

  • validate_active — extract Set/Unchanged values from ActiveModel into JSON and validate
  • validate_model — validate any Serialize-able struct (Model, DTO, etc.)
  • validate_json — validate raw JSON
  • Validated<S, T> — wrapper proving data has been validated
  • before_save — helper for ActiveModelBehavior::before_save
  • impl_vld_before_save! — macro for automatic validation on every insert/update
  • active_model_to_json — convert ActiveModel to JSON (skips NotSet fields)

Installation

[dependencies]
vld-sea = "0.1"
vld = "0.1"
sea-orm = "1"

Quick Start

1. Define the validation schema

vld::schema! {
    #[derive(Debug, Clone)]
    pub struct UserInput {
        pub name: String  => vld::string().min(1).max(100),
        pub email: String => vld::string().email(),
    }
}

2. Validate ActiveModel before insert

use sea_orm::Set;

let am = user::ActiveModel {
    name: Set("Alice".to_owned()),
    email: Set("alice@example.com".to_owned()),
    ..Default::default()
};

// Validate — returns parsed schema or VldSeaError
vld_sea::validate_active::<UserInput, _>(&am)?;

// Now safe to insert
am.insert(&db).await?;

3. Automatic validation via before_save

Option A — manual:

#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
    async fn before_save<C: ConnectionTrait>(
        self, _db: &C, _insert: bool,
    ) -> Result<Self, DbErr> {
        vld_sea::before_save::<UserInput, _>(&self)?;
        Ok(self)
    }
}

Option B — macro:

// Replace the default `impl ActiveModelBehavior for ActiveModel {}`
vld_sea::impl_vld_before_save!(ActiveModel, UserInput);

Option C — separate schemas for insert vs update:

vld_sea::impl_vld_before_save!(
    ActiveModel,
    insert: UserInsertSchema,
    update: UserUpdateSchema
);

4. Validate a DTO

#[derive(serde::Serialize)]
struct NewUser { name: String, email: String }

let input = NewUser { name: "Bob".into(), email: "bob@example.com".into() };
vld_sea::validate_model::<UserInput, _>(&input)?;

ActiveModel → JSON

active_model_to_json converts an ActiveModel to a JSON object. Only Set and Unchanged fields are included; NotSet fields are omitted.

Supported SQL types: bool, i8i64, u8u64, f32, f64, String, char. Feature-gated types (chrono, uuid, etc.) are mapped to null.

Running Examples

cargo run -p vld-sea --example sea_basic

License

MIT