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
use crate::registry::REGISTRY;
use crate::validate::validate;
use prost_reflect::ReflectMessage;

mod number;
mod string;
mod r#enum;
mod duration;
mod timestamp;
mod message;
mod bool;
mod any;
mod utils;
mod bytes;
mod registry;
mod validate;
mod list;
mod map;
mod field;

mod validate_proto {
    use once_cell::sync::Lazy;
    use prost_reflect::DescriptorPool;

    pub(crate) static DESCRIPTOR_POOL: Lazy<DescriptorPool>
    = Lazy::new(|| DescriptorPool::decode(include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin")).as_ref()).unwrap());
    include!(concat!(env!("OUT_DIR"), "/validate.rs"));
}

pub trait ValidatorExt {
    fn validate(&self) -> anyhow::Result<()>;
    fn validate_slow(&self) -> anyhow::Result<()>;
}

impl<T: ReflectMessage> ValidatorExt for T {
    fn validate(&self) -> anyhow::Result<()> {
        let msg = self.transcode_to_dynamic();
        REGISTRY.validate(&msg)
    }

    fn validate_slow(&self) -> anyhow::Result<()> {
        validate(self)
    }
}