rydantic 0.7.0-beta.2

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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Native Cap'n Proto integration with automatic protocols and existing schemas.
//! Model data maps directly to official readers and builders.
//!
//! ```
//! use rydantic::{CapnpModel, CapnpSchema};
//! #[rydantic::capnp_models(namespace = "docs.users")]
//! mod models {
//!     #[derive(rydantic::Model, Debug, PartialEq)]
//!     pub struct User { pub name: String, pub age: u8 }
//! }
//! let user = models::User { name: "Alice".into(), age: 25 };
//! assert_eq!(models::User::parse_capnp(&user.dump_capnp()?)?, user);
//! assert!(models::User::capnp_schema().contains("struct User"));
//! # Ok::<(), rydantic::ValidationErrors>(())
//! ```
use crate::{AsyncCheck, Context, Model, Stage, ValidationError, ValidationErrors};
use capnp::{
    message::{Builder, HeapAllocator, ReaderOptions},
    traits::Owned,
};

/// The exported protocol derived by `#[rydantic::capnp_models]`.
pub trait CapnpSchema: Model {
    const CAPNP_SCHEMA: &'static str;
    /// Root declaration for this model (including enum root wrappers).
    const CAPNP_TYPE: &'static str;
    fn capnp_schema() -> &'static str {
        Self::CAPNP_SCHEMA
    }
    fn write_capnp_schema(path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
        std::fs::write(path, Self::CAPNP_SCHEMA)
    }
}

pub trait CapnpEnum: Sized {
    type Wire;
    fn read_enum(value: Self::Wire, ctx: &mut Context) -> Option<Self>;
    fn write_enum(&self) -> Self::Wire;
}
/// A model bound to official Cap'n Proto readers/builders.
/// Use `#[rydantic::capnp_models]` to infer the protocol automatically, or
/// `#[model(capnp = "wire::module")]` to bind an existing schema.
pub trait CapnpModel: Model {
    type Wire: Owned;
    fn read_capnp(reader: <Self::Wire as Owned>::Reader<'_>, ctx: &mut Context) -> Option<Self>;
    fn write_capnp(&self, builder: <Self::Wire as Owned>::Builder<'_>, ctx: &mut Context);
    fn parse_capnp(bytes: &[u8]) -> Result<Self, ValidationErrors> {
        Self::parse_capnp_with_options(bytes, ReaderOptions::new())
    }
    fn parse_capnp_with_options(
        bytes: &[u8],
        options: ReaderOptions,
    ) -> Result<Self, ValidationErrors> {
        crate::asynchronous::sync_guard(Self::async_required(
            &mut std::collections::HashSet::new(),
        ))?;
        decode::<Self>(bytes, false, options)
    }
    fn parse_capnp_packed(bytes: &[u8]) -> Result<Self, ValidationErrors> {
        crate::asynchronous::sync_guard(Self::async_required(
            &mut std::collections::HashSet::new(),
        ))?;
        decode::<Self>(bytes, true, ReaderOptions::new())
    }
    fn from_capnp_reader(
        reader: <Self::Wire as Owned>::Reader<'_>,
    ) -> Result<Self, ValidationErrors> {
        crate::asynchronous::sync_guard(Self::async_required(
            &mut std::collections::HashSet::new(),
        ))?;
        let mut ctx = Context::new();
        let value = Self::read_capnp(reader, &mut ctx);
        let value = ctx.finish(value)?;
        let mut ctx = Context::new();
        let value = value.prepare_default(&mut ctx);
        ctx.finish(value)
    }
    fn to_capnp_message(&self) -> Result<Builder<HeapAllocator>, ValidationErrors> {
        self.validate()?;
        build(self)
    }
    fn dump_capnp(&self) -> Result<Vec<u8>, ValidationErrors> {
        let message = self.to_capnp_message()?;
        Ok(capnp::serialize::write_message_to_words(&message))
    }
    fn dump_capnp_packed(&self) -> Result<Vec<u8>, ValidationErrors> {
        let message = self.to_capnp_message()?;
        packed_bytes(&message)
    }
    fn parse_capnp_async(
        bytes: &[u8],
    ) -> impl std::future::Future<Output = Result<Self, ValidationErrors>> + Send
    where
        Self: AsyncCheck + Send,
    {
        async move {
            let value = decode::<Self>(bytes, false, ReaderOptions::new())?;
            let mut ctx = Context::new();
            value.check_async(&mut ctx).await;
            ctx.result()?;
            Ok(value)
        }
    }
    fn parse_capnp_packed_async(
        bytes: &[u8],
    ) -> impl std::future::Future<Output = Result<Self, ValidationErrors>> + Send
    where
        Self: AsyncCheck + Send,
    {
        async move {
            let value = decode::<Self>(bytes, true, ReaderOptions::new())?;
            let mut ctx = Context::new();
            value.check_async(&mut ctx).await;
            ctx.result()?;
            Ok(value)
        }
    }
    fn dump_capnp_async(
        &self,
    ) -> impl std::future::Future<Output = Result<Vec<u8>, ValidationErrors>> + Send
    where
        Self: AsyncCheck,
    {
        async move {
            self.validate_async().await?;
            Ok(capnp::serialize::write_message_to_words(&build(self)?))
        }
    }
    fn dump_capnp_packed_async(
        &self,
    ) -> impl std::future::Future<Output = Result<Vec<u8>, ValidationErrors>> + Send
    where
        Self: AsyncCheck,
    {
        async move {
            self.validate_async().await?;
            packed_bytes(&build(self)?)
        }
    }
}
fn decode<T: CapnpModel>(
    mut bytes: &[u8],
    packed: bool,
    options: ReaderOptions,
) -> Result<T, ValidationErrors> {
    let mut ctx = Context::new();
    let value = if packed {
        let message =
            capnp::serialize_packed::read_message(&mut bytes, options).map_err(parse_error)?;
        let root = message
            .get_root::<<T::Wire as Owned>::Reader<'_>>()
            .map_err(parse_error)?;
        T::read_capnp(root, &mut ctx)
    } else {
        let message = capnp::serialize::read_message_from_flat_slice(&mut bytes, options)
            .map_err(parse_error)?;
        let root = message
            .get_root::<<T::Wire as Owned>::Reader<'_>>()
            .map_err(parse_error)?;
        T::read_capnp(root, &mut ctx)
    };
    if !bytes.is_empty() {
        ctx.push(
            ValidationError::new(
                "capnp_trailing",
                "unexpected bytes after Cap'n Proto message",
            )
            .with_stage(Stage::Parse),
        );
    }
    let value = ctx.finish(value)?;
    let mut ctx = Context::new();
    let value = value.prepare_default(&mut ctx);
    ctx.finish(value)
}
fn build<T: CapnpModel>(value: &T) -> Result<Builder<HeapAllocator>, ValidationErrors> {
    let mut message = Builder::new_default();
    let mut ctx = Context::new();
    value.write_capnp(
        message.init_root::<<T::Wire as Owned>::Builder<'_>>(),
        &mut ctx,
    );
    ctx.result()?;
    Ok(message)
}
fn packed_bytes(message: &Builder<HeapAllocator>) -> Result<Vec<u8>, ValidationErrors> {
    let mut bytes = Vec::new();
    capnp::serialize_packed::write_message(&mut bytes, message).map_err(|error| {
        ValidationError::new("capnp_encode", error.to_string()).with_stage(Stage::Serialize)
    })?;
    Ok(bytes)
}
fn parse_error(error: impl std::fmt::Display) -> ValidationErrors {
    ValidationError::new("capnp_decode", error.to_string())
        .with_stage(Stage::Parse)
        .into()
}
#[doc(hidden)]
pub fn read_result<T, E: std::fmt::Display>(value: Result<T, E>, ctx: &mut Context) -> Option<T> {
    value
        .map_err(|error| {
            ctx.push(
                ValidationError::new("capnp_decode", error.to_string()).with_stage(Stage::Parse),
            )
        })
        .ok()
}
#[doc(hidden)]
pub fn which_name(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<String> {
    let capnp::dynamic_value::Reader::Struct(reader) = value else {
        ctx.push(ValidationError::new(
            "capnp_schema",
            "expected a union struct",
        ));
        return None;
    };
    let field = read_result(reader.which(), ctx)?;
    let Some(field) = field else {
        ctx.push(ValidationError::new(
            "capnp_union",
            "schema has no active union field",
        ));
        return None;
    };
    let name = read_result(field.get_proto().get_name(), ctx)?;
    Some(read_result(name.to_str(), ctx)?.to_owned())
}
#[doc(hidden)]
pub fn optional_some(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<bool> {
    match which_name(value, ctx)?.as_str() {
        "some" => Some(true),
        "none" => Some(false),
        _ => {
            ctx.push(ValidationError::new(
                "capnp_schema",
                "optional union must contain none and some",
            ));
            None
        }
    }
}
#[doc(hidden)]
pub trait SetResult {
    fn checked(self, ctx: &mut Context) -> Option<()>;
}
impl SetResult for () {
    fn checked(self, _: &mut Context) -> Option<()> {
        Some(())
    }
}
impl SetResult for capnp::Result<()> {
    fn checked(self, ctx: &mut Context) -> Option<()> {
        self.map_err(|error| {
            ctx.push(
                ValidationError::new("capnp_encode", error.to_string())
                    .with_stage(Stage::Serialize),
            )
        })
        .ok()
    }
}
#[doc(hidden)]
pub fn write_result(value: impl SetResult, ctx: &mut Context) -> Option<()> {
    value.checked(ctx)
}
#[doc(hidden)]
pub fn list_len(length: usize, ctx: &mut Context) -> Option<u32> {
    if length >= (1 << 29) {
        ctx.push(
            ValidationError::new(
                "capnp_length",
                "lists are limited to fewer than 2^29 elements",
            )
            .with_stage(Stage::Serialize),
        );
        return None;
    }
    u32::try_from(length)
        .map_err(|_| {
            ctx.push(
                ValidationError::new("capnp_length", "list exceeds the wire format length")
                    .with_stage(Stage::Serialize),
            )
        })
        .ok()
}
#[doc(hidden)]
pub fn converted<T, U: TryFrom<T>>(value: T, ctx: &mut Context) -> Option<U> {
    U::try_from(value)
        .map_err(|_| {
            ctx.push(ValidationError::new(
                "capnp_range",
                "numeric value exceeds target wire/model range",
            ))
        })
        .ok()
}

/// Native dynamic values used for union payloads. Implemented by derived models.
#[doc(hidden)]
pub trait CapnpRead: Sized {
    fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self>;
}
#[doc(hidden)]
pub fn mismatch(ctx: &mut Context) {
    ctx.push(ValidationError::new(
        "capnp_schema",
        "wire field type does not match the model",
    ));
}
#[doc(hidden)]
pub fn field_value<'a>(
    value: capnp::dynamic_value::Reader<'a>,
    name: &str,
    ctx: &mut Context,
) -> Option<capnp::dynamic_value::Reader<'a>> {
    let capnp::dynamic_value::Reader::Struct(reader) = value else {
        mismatch(ctx);
        return None;
    };
    read_result(reader.get_named(name), ctx)
}
#[doc(hidden)]
pub fn text_value<'a>(
    value: capnp::dynamic_value::Reader<'a>,
    ctx: &mut Context,
) -> Option<&'a str> {
    let capnp::dynamic_value::Reader::Text(text) = value else {
        mismatch(ctx);
        return None;
    };
    read_result(text.to_str(), ctx)
}
#[doc(hidden)]
pub fn data_value<'a>(
    value: capnp::dynamic_value::Reader<'a>,
    ctx: &mut Context,
) -> Option<&'a [u8]> {
    let capnp::dynamic_value::Reader::Data(data) = value else {
        mismatch(ctx);
        return None;
    };
    Some(data)
}
#[doc(hidden)]
pub fn struct_value<'a, T: capnp::traits::OwnedStruct>(
    value: capnp::dynamic_value::Reader<'a>,
    ctx: &mut Context,
) -> Option<T::Reader<'a>> {
    let capnp::dynamic_value::Reader::Struct(reader) = value else {
        mismatch(ctx);
        return None;
    };
    let capnp::introspect::TypeVariant::Struct(schema) = T::introspect().which() else {
        mismatch(ctx);
        return None;
    };
    if reader.get_schema() != capnp::schema::StructSchema::new(schema) {
        mismatch(ctx);
        return None;
    }
    Some(reader.downcast::<T>())
}
macro_rules! integer_read {
    ($($ty:ty),*) => {$ (
        impl CapnpRead for $ty {
            fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self> {
                use capnp::dynamic_value::Reader::*;
                match value {
                    Int8(n) => converted(n,ctx), Int16(n) => converted(n,ctx), Int32(n) => converted(n,ctx), Int64(n) => converted(n,ctx),
                    UInt8(n) => converted(n,ctx), UInt16(n) => converted(n,ctx), UInt32(n) => converted(n,ctx), UInt64(n) => converted(n,ctx),
                    Text(text) => <Self as crate::JsonType>::decode(crate::Value::String(read_result(text.to_str(),ctx)?.to_owned()),ctx,true),
                    _ => {mismatch(ctx);None}
                }
            }
        }
    )*};
}
integer_read!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
macro_rules! scalar_read {
    ($ty:ty,$variant:ident) => {
        impl CapnpRead for $ty {
            fn read_value(
                value: capnp::dynamic_value::Reader<'_>,
                ctx: &mut Context,
            ) -> Option<Self> {
                match value {
                    capnp::dynamic_value::Reader::$variant(value) => Some(value),
                    capnp::dynamic_value::Reader::Text(text) => <Self as crate::JsonType>::decode(
                        crate::Value::String(read_result(text.to_str(), ctx)?.to_owned()),
                        ctx,
                        true,
                    ),
                    _ => {
                        mismatch(ctx);
                        None
                    }
                }
            }
        }
    };
}
scalar_read!(bool, Bool);
scalar_read!(f32, Float32);
scalar_read!(f64, Float64);
impl CapnpRead for String {
    fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self> {
        Some(text_value(value, ctx)?.to_owned())
    }
}
impl<T: CapnpRead> CapnpRead for Box<T> {
    fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self> {
        Some(Box::new(T::read_value(value, ctx)?))
    }
}
impl<T: CapnpRead> CapnpRead for Option<T> {
    fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self> {
        if optional_some(value, ctx)? {
            Some(Some(T::read_value(field_value(value, "some", ctx)?, ctx)?))
        } else {
            Some(None)
        }
    }
}
impl<T: CapnpRead> CapnpRead for Vec<T> {
    fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self> {
        if let capnp::dynamic_value::Reader::Data(data) = value {
            return data
                .iter()
                .enumerate()
                .map(|(index, byte)| {
                    ctx.at(index, |ctx| {
                        T::read_value(capnp::dynamic_value::Reader::UInt8(*byte), ctx)
                    })
                })
                .collect();
        }
        let capnp::dynamic_value::Reader::List(list) = value else {
            mismatch(ctx);
            return None;
        };
        let mut values = Vec::with_capacity(list.len() as usize);
        for index in 0..list.len() {
            values.push(ctx.at(index as usize, |ctx| {
                T::read_value(read_result(list.get(index), ctx)?, ctx)
            })?);
        }
        Some(values)
    }
}
impl<T: CapnpRead, const N: usize> CapnpRead for [T; N] {
    fn read_value(value: capnp::dynamic_value::Reader<'_>, ctx: &mut Context) -> Option<Self> {
        Vec::<T>::read_value(value, ctx)?
            .try_into()
            .map_err(|_| {
                ctx.push(ValidationError::new(
                    "capnp_length",
                    format!("expected {N} array elements"),
                ))
            })
            .ok()
    }
}
macro_rules! map_read {
    ($map:path) => {
        impl<T: CapnpRead> CapnpRead for $map {
            fn read_value(
                value: capnp::dynamic_value::Reader<'_>,
                ctx: &mut Context,
            ) -> Option<Self> {
                let capnp::dynamic_value::Reader::List(list) = value else {
                    mismatch(ctx);
                    return None;
                };
                let mut values = Self::new();
                for index in 0..list.len() {
                    let entry = read_result(list.get(index), ctx)?;
                    let key = String::read_value(field_value(entry, "key", ctx)?, ctx)?;
                    let value = ctx.at(key.clone(), |ctx| {
                        T::read_value(field_value(entry, "value", ctx)?, ctx)
                    })?;
                    if values.insert(key.clone(), value).is_some() {
                        ctx.at(key, |ctx| {
                            ctx.push(ValidationError::new(
                                "capnp_duplicate_key",
                                "duplicate map key",
                            ))
                        });
                        return None;
                    }
                }
                Some(values)
            }
        }
    };
}
map_read!(std::collections::BTreeMap<String,T>);
map_read!(std::collections::HashMap<String,T>);

#[cfg(any(feature = "chrono", feature = "decimal"))]
macro_rules! text_read {
    ($ty:ty) => {
        impl CapnpRead for $ty {
            fn read_value(
                value: capnp::dynamic_value::Reader<'_>,
                ctx: &mut Context,
            ) -> Option<Self> {
                <Self as crate::JsonType>::decode(
                    crate::Value::String(text_value(value, ctx)?.to_owned()),
                    ctx,
                    true,
                )
            }
        }
    };
}
#[cfg(feature = "chrono")]
text_read!(chrono::NaiveDate);
#[cfg(feature = "chrono")]
text_read!(chrono::NaiveTime);
#[cfg(feature = "chrono")]
text_read!(chrono::NaiveDateTime);
#[cfg(feature = "chrono")]
text_read!(chrono::DateTime<chrono::Utc>);
#[cfg(feature = "chrono")]
text_read!(chrono::DateTime<chrono::FixedOffset>);
#[cfg(feature = "decimal")]
text_read!(rust_decimal::Decimal);