diff --git a/rust/src/events/formats/mod.rs b/rust/src/events/formats/mod.rs index 29f32ae55e..c5998fd1d1 100644 --- a/rust/src/events/formats/mod.rs +++ b/rust/src/events/formats/mod.rs @@ -68,6 +68,7 @@ use std::{collections::HashMap, sync::Arc}; +use anyhow::Error; use serde::{Deserialize, Serialize}; use crate::events::{json_object::JsonObject, signatures::Signatures, unsigned::Unsigned}; @@ -128,6 +129,15 @@ impl FormattedEvent { common_fields: Arc::clone(&self.common_fields), } } + + pub fn validate(&self) -> Result<(), Error> { + match &*self.specific_fields { + EventFormatEnum::V1(format) => format.validate(&self.common_fields), + EventFormatEnum::V2V3(format) => format.validate(&self.common_fields), + EventFormatEnum::V4(format) => format.validate(&self.common_fields), + EventFormatEnum::VMSC4242(format) => format.validate(&self.common_fields), + } + } } impl FormattedEvent diff --git a/rust/src/events/formats/v1.rs b/rust/src/events/formats/v1.rs index f18f80109e..7fbc50a97c 100644 --- a/rust/src/events/formats/v1.rs +++ b/rust/src/events/formats/v1.rs @@ -25,8 +25,11 @@ use std::{collections::HashMap, sync::Arc}; +use anyhow::Error; use serde::{Deserialize, Serialize}; +use crate::events::formats::EventCommonFields; + /// Version-specific fields for room versions 1 and 2. #[derive(Serialize, Deserialize)] pub struct EventFormatV1 { @@ -37,6 +40,10 @@ pub struct EventFormatV1 { } impl EventFormatV1 { + pub fn validate(&self, _common_fields: &EventCommonFields) -> Result<(), Error> { + Ok(()) + } + pub fn auth_event_ids(&self) -> Vec { self.auth_events.iter().map(|(id, _)| id.clone()).collect() } diff --git a/rust/src/events/mod.rs b/rust/src/events/mod.rs index 8fea2e21a5..b4c818ccfe 100644 --- a/rust/src/events/mod.rs +++ b/rust/src/events/mod.rs @@ -525,23 +525,14 @@ fn depythonize_event_dict( } EventFormatVersions::ROOM_V3 | EventFormatVersions::ROOM_V4_PLUS => { let event_format: FormattedEvent = depythonize(event_dict)?; - event_format - .specific_fields - .validate(&event_format.common_fields)?; event_format.into() } EventFormatVersions::ROOM_V11_HYDRA_PLUS => { let event_format: FormattedEvent = depythonize(event_dict)?; - event_format - .specific_fields - .validate(&event_format.common_fields)?; event_format.into() } EventFormatVersions::ROOM_VMSC4242 => { let event_format: FormattedEvent = depythonize(event_dict)?; - event_format - .specific_fields - .validate(&event_format.common_fields)?; event_format.into() } _ => { @@ -552,6 +543,8 @@ fn depythonize_event_dict( } }; + formatted_event.validate()?; + Ok(formatted_event) }