use crate::didcomm::PlainMessage;
use crate::error::{Error, Result};
use crate::message::tap_message_trait::TapMessageBody;
use crate::message::{
AddAgents, Authorize, DIDCommPresentation, ErrorBody, Presentation, Reject, Settle, Transfer,
};
use serde_json::Value;
pub fn validate_message(message: &PlainMessage) -> Result<()> {
if message.id.is_empty() {
return Err(Error::Validation("Message ID is required".to_string()));
}
let message_type = &message.type_;
validate_message_body(message_type, &message.body)?;
Ok(())
}
pub fn validate_message_body(message_type: &str, body: &Value) -> Result<()> {
match message_type {
"https://tap.rsvp/schema/1.0#Transfer" => {
let transfer: Transfer = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
transfer.validate()
}
"https://tap.rsvp/schema/1.0#Presentation" => {
let presentation: Presentation = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
presentation.validate()
}
"https://didcomm.org/present-proof/3.0/presentation" => {
let didcomm_presentation: DIDCommPresentation = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
didcomm_presentation.validate()
}
"https://tap.rsvp/schema/1.0#Authorize" => {
let authorize: Authorize = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
authorize.validate()
}
"https://tap.rsvp/schema/1.0#Reject" => {
let reject: Reject = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
reject.validate()
}
"https://tap.rsvp/schema/1.0#Settle" => {
let settle: Settle = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
settle.validate()
}
"https://tap.rsvp/schema/1.0#AddAgents" => {
let add_agents: AddAgents = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
add_agents.validate()
}
"https://tap.rsvp/schema/1.0#Error" => {
let error: ErrorBody = serde_json::from_value(body.clone())
.map_err(|e| Error::SerializationError(e.to_string()))?;
error.validate()
}
_ => {
Ok(())
}
}
}