tap_msg/message/
validation.rs1use crate::didcomm::PlainMessage;
6use crate::error::{Error, Result};
7use crate::message::tap_message_trait::TapMessageBody;
8use crate::message::{
9 AddAgents, Authorize, DIDCommPresentation, ErrorBody, Presentation, Reject, Settle, Transfer,
10};
11use serde_json::Value;
12
13pub fn validate_message(message: &PlainMessage) -> Result<()> {
15 if message.id.is_empty() {
17 return Err(Error::Validation("Message ID is required".to_string()));
18 }
19
20 let message_type = &message.type_;
22
23 validate_message_body(message_type, &message.body)?;
25
26 Ok(())
27}
28
29pub fn validate_message_body(message_type: &str, body: &Value) -> Result<()> {
32 match message_type {
33 "https://tap.rsvp/schema/1.0#Transfer" => {
34 let transfer: Transfer = serde_json::from_value(body.clone())
35 .map_err(|e| Error::SerializationError(e.to_string()))?;
36 transfer.validate()
37 }
38 "https://tap.rsvp/schema/1.0#Presentation" => {
39 let presentation: Presentation = serde_json::from_value(body.clone())
40 .map_err(|e| Error::SerializationError(e.to_string()))?;
41 presentation.validate()
42 }
43 "https://didcomm.org/present-proof/3.0/presentation" => {
44 let didcomm_presentation: DIDCommPresentation = serde_json::from_value(body.clone())
46 .map_err(|e| Error::SerializationError(e.to_string()))?;
47 didcomm_presentation.validate()
48 }
49 "https://tap.rsvp/schema/1.0#Authorize" => {
50 let authorize: Authorize = serde_json::from_value(body.clone())
51 .map_err(|e| Error::SerializationError(e.to_string()))?;
52 authorize.validate()
53 }
54 "https://tap.rsvp/schema/1.0#Reject" => {
55 let reject: Reject = serde_json::from_value(body.clone())
56 .map_err(|e| Error::SerializationError(e.to_string()))?;
57 reject.validate()
58 }
59 "https://tap.rsvp/schema/1.0#Settle" => {
60 let settle: Settle = serde_json::from_value(body.clone())
61 .map_err(|e| Error::SerializationError(e.to_string()))?;
62 settle.validate()
63 }
64 "https://tap.rsvp/schema/1.0#AddAgents" => {
65 let add_agents: AddAgents = serde_json::from_value(body.clone())
66 .map_err(|e| Error::SerializationError(e.to_string()))?;
67 add_agents.validate()
68 }
69 "https://tap.rsvp/schema/1.0#Error" => {
70 let error: ErrorBody = serde_json::from_value(body.clone())
71 .map_err(|e| Error::SerializationError(e.to_string()))?;
72 error.validate()
73 }
74 _ => {
75 Ok(())
77 }
78 }
79}