rydantic 0.7.0-beta.4

Type-driven validation, JSON Schema, Cap'n Proto, Axum and SQLx models for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#![cfg(feature = "sqlite")]
use rydantic::sqlx::{self, sqlite::SqlitePoolOptions, Row, SqlitePool};
use rydantic::{
    sql::{Compare, Order},
    Model, SqlError, SqlModel,
};

#[derive(Debug, PartialEq, Model, SqlModel)]
#[sql(table = "users")]
struct User {
    #[sql(primary_key, auto_increment)]
    id: Option<i64>,
    #[model(trim, min_length = 2)]
    #[sql(unique)]
    name: String,
    #[model(ge = 18, le = 120)]
    #[sql(index)]
    age: u8,
    note: Option<String>,
    avatar: Vec<u8>,
    #[sql(skip)]
    #[model(default = "local")]
    internal: String,
}
async fn pool() -> SqlitePool {
    SqlitePoolOptions::new()
        .max_connections(1)
        .connect("sqlite::memory:")
        .await
        .unwrap()
}
fn user(name: &str) -> User {
    User {
        id: None,
        name: name.into(),
        age: 25,
        note: None,
        avatar: vec![0, 255],
        internal: "local".into(),
    }
}

#[tokio::test]
async fn schema_crud_indexes_aliases_and_native_values_roundtrip() {
    let db = pool().await;
    User::create_table(&db).await.unwrap();
    User::create_table(&db).await.unwrap();
    let parsed = User::parse_json(r#"{"name":" Alice ","age":25,"avatar":[0,255]}"#).unwrap();
    let mut stored = parsed.insert(&db).await.unwrap();
    assert_eq!(stored.id, Some(1));
    assert_eq!(stored.name, "Alice");
    assert_eq!(parsed.id, None);
    assert_eq!(
        User::get(&db, &1).await.unwrap(),
        Some(user_with_id("Alice", 1))
    );
    stored.age = 30;
    stored.note = Some("edited".into());
    stored = stored.update(&db).await.unwrap();
    assert_eq!(User::get(&db, &1).await.unwrap(), Some(stored));
    let rows = sqlx::query("PRAGMA index_list(users)")
        .fetch_all(&db)
        .await
        .unwrap();
    assert!(rows
        .iter()
        .any(|row| row.get::<String, _>("name").contains("age")));
    let decoded: User = sqlx::query_as("SELECT * FROM users WHERE id = 1")
        .fetch_one(&db)
        .await
        .unwrap();
    assert_eq!(decoded.note.as_deref(), Some("edited"));
    assert!(User::delete(&db, &1).await.unwrap());
    assert!(!User::delete(&db, &1).await.unwrap());
    assert!(User::get(&db, &1).await.unwrap().is_none());
    assert!(matches!(
        user_with_id("missing", 999).update(&db).await,
        Err(SqlError::NotFound)
    ));
}
fn user_with_id(name: &str, id: i64) -> User {
    User {
        id: Some(id),
        ..user(name)
    }
}

#[tokio::test]
async fn invalid_models_and_duplicate_keys_cannot_persist() {
    let db = pool().await;
    User::create_table(&db).await.unwrap();
    let mut invalid = user("bad");
    invalid.age = 17;
    assert!(matches!(
        invalid.insert(&db).await,
        Err(SqlError::Validation(_))
    ));
    assert_eq!(User::select().fetch_all(&db).await.unwrap().len(), 0);
    user("Alice").insert(&db).await.unwrap();
    assert!(matches!(
        user("Alice").insert(&db).await,
        Err(SqlError::Database(_))
    ));
    assert_eq!(User::select().fetch_all(&db).await.unwrap().len(), 1);
    let mut invalid = user("Bob");
    invalid.id = Some(1);
    invalid.age = 17;
    assert!(invalid.update(&db).await.is_err());
    assert_eq!(User::get(&db, &1).await.unwrap().unwrap().age, 25);
    sqlx::query("UPDATE users SET age = 1 WHERE id = 1")
        .execute(&db)
        .await
        .unwrap();
    assert!(matches!(
        User::get(&db, &1).await,
        Err(SqlError::Validation(_))
    ));
    assert!(sqlx::query_as::<_, User>("SELECT * FROM users")
        .fetch_one(&db)
        .await
        .is_err());
}
#[tokio::test]
async fn filters_bind_parameters_validate_columns_and_paginate() {
    let db = pool().await;
    User::create_table(&db).await.unwrap();
    for name in ["Alice", "Bob", "x' OR 1=1 --"] {
        user(name).insert(&db).await.unwrap();
    }
    let values = User::select()
        .where_eq("name", &"x' OR 1=1 --".to_owned())
        .unwrap()
        .fetch_all(&db)
        .await
        .unwrap();
    assert_eq!(values.len(), 1);
    assert!(matches!(
        User::select().where_eq("name; DROP TABLE users", &1i64),
        Err(SqlError::InvalidColumn(_))
    ));
    assert!(User::select().where_eq("name", &25i64).is_err());
    let values = User::select()
        .filter("age", Compare::GreaterOrEqual, &18u8)
        .unwrap()
        .order_by("name", Order::Ascending)
        .unwrap()
        .limit(1)
        .offset(1)
        .fetch_all(&db)
        .await
        .unwrap();
    assert_eq!(values[0].name, "Bob");
    assert_eq!(
        User::select()
            .where_eq("note", &Option::<String>::None)
            .unwrap()
            .fetch_all(&db)
            .await
            .unwrap()
            .len(),
        3
    );
    assert!(User::select()
        .offset(u64::MAX)
        .fetch_all(&db)
        .await
        .is_err());
}
#[tokio::test]
async fn operations_compose_with_connections_and_outer_transactions() {
    let db = pool().await;
    User::create_table(&db).await.unwrap();
    let mut tx = db.begin().await.unwrap();
    let stored = user("Alice").insert(&mut tx).await.unwrap();
    assert_eq!(
        User::get(&mut tx, &stored.id.unwrap()).await.unwrap(),
        Some(stored)
    );
    tx.rollback().await.unwrap();
    assert!(User::select().fetch_all(&db).await.unwrap().is_empty());
    let mut connection = db.acquire().await.unwrap();
    user("Bob").insert(&mut *connection).await.unwrap();
    assert_eq!(
        User::select()
            .fetch_all(&mut *connection)
            .await
            .unwrap()
            .len(),
        1
    );
}

#[derive(Debug, PartialEq, Model, SqlModel)]
struct Wide {
    #[sql(primary_key)]
    id: String,
    unsigned: u128,
    signed: i128,
    limited: u64,
}
#[tokio::test]
async fn wide_integers_are_exact_and_sqlite_integer_overflow_is_rejected() {
    let db = pool().await;
    Wide::create_table(&db).await.unwrap();
    let mut model = Wide {
        id: "key".into(),
        unsigned: u128::MAX,
        signed: i128::MIN,
        limited: i64::MAX as u64,
    };
    let stored = model.insert(&db).await.unwrap();
    assert_eq!(model, stored);
    model.id = "overflow".into();
    model.limited = u64::MAX;
    let error = model.insert(&db).await.unwrap_err();
    let SqlError::Validation(error) = error else {
        panic!("expected validation error")
    };
    assert_eq!(error.errors()[0].pointer(), "/limited");
    assert_eq!(Wide::select().fetch_all(&db).await.unwrap().len(), 1);
}

#[derive(Debug, Model, SqlModel)]
struct Post {
    #[sql(primary_key, auto_increment)]
    id: Option<i64>,
    #[sql(references = "users.id", on_delete = "cascade", index)]
    user_id: i64,
    title: String,
}
#[tokio::test]
async fn foreign_keys_and_cascade_are_enforced_by_sqlite() {
    let db = pool().await;
    User::create_table(&db).await.unwrap();
    Post::create_table(&db).await.unwrap();
    let parent = user("Alice").insert(&db).await.unwrap();
    Post {
        id: None,
        user_id: parent.id.unwrap(),
        title: "post".into(),
    }
    .insert(&db)
    .await
    .unwrap();
    assert!(Post {
        id: None,
        user_id: 999,
        title: "invalid".into()
    }
    .insert(&db)
    .await
    .is_err());
    User::delete(&db, &parent.id.unwrap()).await.unwrap();
    assert!(Post::select().fetch_all(&db).await.unwrap().is_empty());
}

#[derive(Debug, PartialEq, Model, SqlModel)]
#[sql(table = "select")]
struct Renamed {
    #[sql(primary_key, rename = "primary_key")]
    key: String,
    #[model(rename = "displayName")]
    #[sql(rename = "display_name")]
    name: String,
}
#[tokio::test]
async fn sql_names_are_independent_of_json_aliases_and_quoted_identifiers_work() {
    let db = pool().await;
    Renamed::create_table(&db).await.unwrap();
    let value = Renamed::parse_json(r#"{"key":"one","displayName":"Alice"}"#).unwrap();
    assert_eq!(value.insert(&db).await.unwrap(), value);
    assert_eq!(Renamed::get(&db, &"one".into()).await.unwrap(), Some(value));
    assert!(!Renamed::COLUMNS[0].nullable);
}

#[derive(Debug, Model, SqlModel)]
struct Generated {
    #[sql(primary_key, auto_increment)]
    #[model(ge = 100)]
    id: Option<i64>,
}
#[tokio::test]
async fn invalid_returning_rows_roll_back_the_entire_write() {
    let db = pool().await;
    Generated::create_table(&db).await.unwrap();
    assert!(matches!(
        Generated { id: None }.insert(&db).await,
        Err(SqlError::Validation(_))
    ));
    assert!(Generated::select().fetch_all(&db).await.unwrap().is_empty());
    Generated { id: Some(100) }.insert(&db).await.unwrap();
    assert_eq!(
        Generated { id: None }.insert(&db).await.unwrap().id,
        Some(101)
    );
}

async fn not_reserved(name: &str) -> Result<(), rydantic::ValidationError> {
    tokio::task::yield_now().await;
    if name == "reserved" {
        Err(rydantic::ValidationError::new(
            "reserved",
            "name is reserved",
        ))
    } else {
        Ok(())
    }
}
#[derive(Debug, Model, SqlModel)]
struct AsyncRecord {
    #[sql(primary_key)]
    id: i64,
    #[model(async_validate=not_reserved)]
    name: String,
}
#[tokio::test]
async fn database_reads_and_writes_await_model_validators() {
    let db = pool().await;
    AsyncRecord::create_table(&db).await.unwrap();
    assert!(matches!(
        AsyncRecord {
            id: 1,
            name: "reserved".into()
        }
        .insert(&db)
        .await,
        Err(SqlError::Validation(_))
    ));
    assert!(AsyncRecord::select()
        .fetch_all(&db)
        .await
        .unwrap()
        .is_empty());
    AsyncRecord {
        id: 1,
        name: "allowed".into(),
    }
    .insert(&db)
    .await
    .unwrap();
    assert_eq!(
        AsyncRecord::get(&db, &1).await.unwrap().unwrap().name,
        "allowed"
    );
    assert!(
        sqlx::query_as::<_, AsyncRecord>("SELECT * FROM async_record")
            .fetch_one(&db)
            .await
            .is_err()
    );
    sqlx::query("UPDATE async_record SET name = 'reserved'")
        .execute(&db)
        .await
        .unwrap();
    assert!(matches!(
        AsyncRecord::get(&db, &1).await,
        Err(SqlError::Validation(_))
    ));
}

static CHECK_POOL: std::sync::OnceLock<SqlitePool> = std::sync::OnceLock::new();
async fn database_check(_: &str) -> Result<(), rydantic::ValidationError> {
    sqlx::query_scalar::<_, i64>("SELECT 1")
        .fetch_one(CHECK_POOL.get().unwrap())
        .await
        .map(|_| ())
        .map_err(|_| rydantic::ValidationError::custom("database check failed"))
}
#[derive(Debug, Model, SqlModel)]
struct DatabaseChecked {
    #[sql(primary_key)]
    id: i64,
    #[model(async_validate=database_check)]
    name: String,
}
#[tokio::test]
async fn async_validators_can_reuse_a_single_connection_pool() {
    let db = pool().await;
    CHECK_POOL.set(db.clone()).unwrap();
    DatabaseChecked::create_table(&db).await.unwrap();
    let operations = async {
        let mut value = DatabaseChecked {
            id: 1,
            name: "Alice".into(),
        }
        .insert(&db)
        .await
        .unwrap();
        value.name = "Bob".into();
        value.update(&db).await.unwrap();
        assert_eq!(
            DatabaseChecked::get(&db, &1).await.unwrap().unwrap().name,
            "Bob"
        );
        assert_eq!(
            DatabaseChecked::select()
                .fetch_all(&db)
                .await
                .unwrap()
                .len(),
            1
        );
    };
    tokio::time::timeout(std::time::Duration::from_secs(3), operations)
        .await
        .unwrap();
    db.close().await;
}

#[cfg(all(feature = "chrono", feature = "decimal"))]
#[tokio::test]
async fn dates_and_decimal_roundtrip_as_exact_text() {
    #[derive(Debug, PartialEq, Model, SqlModel)]
    struct Payment {
        #[sql(primary_key)]
        id: i64,
        date: rydantic::chrono::NaiveDate,
        amount: rydantic::rust_decimal::Decimal,
    }
    let db = pool().await;
    Payment::create_table(&db).await.unwrap();
    let value = Payment {
        id: 1,
        date: rydantic::chrono::NaiveDate::from_ymd_opt(2024, 2, 29).unwrap(),
        amount: "12345678901234567890.123456789".parse().unwrap(),
    };
    assert_eq!(value.insert(&db).await.unwrap(), value);
    assert_eq!(Payment::get(&db, &1).await.unwrap(), Some(value));
    let amount: String = sqlx::query_scalar("SELECT amount FROM payment")
        .fetch_one(&db)
        .await
        .unwrap();
    assert_eq!(amount, "12345678901234567890.123456789");
}