tap_msg/message/
validation.rs

1//! Validation logic for TAP messages.
2//!
3//! This module provides functionality to validate TAP messages according to the specification.
4
5use 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
13/// Validate a didcomm message's contents based on its type
14pub fn validate_message(message: &PlainMessage) -> Result<()> {
15    // Basic validation for DIDComm message
16    if message.id.is_empty() {
17        return Err(Error::Validation("Message ID is required".to_string()));
18    }
19
20    // Use the type field to determine which validation to apply
21    let message_type = &message.type_;
22
23    // If there's a body, validate it based on the message type
24    validate_message_body(message_type, &message.body)?;
25
26    Ok(())
27}
28
29/// Validate a message body based on its type
30/// Used for validation without constructing a full Message object
31pub 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            // Handle the standard DIDComm present-proof protocol format
45            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            // For custom types, we don't have specific validation
76            Ok(())
77        }
78    }
79}