Skip to content

Commit 53deef7

Browse files
committed
add validator module
1 parent e199d8e commit 53deef7

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

rust/signed_doc/src/doc_types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl TryFrom<UuidV4> for DocumentType {
110110
PUBLIC_VOTE_TX_V2_UUID_TYPE => Ok(DocumentType::PublicVoteTxV2),
111111
PRIVATE_VOTE_TX_V2_UUID_TYPE => Ok(DocumentType::PrivateVoteTxV2),
112112
IMMUTABLE_LEDGER_BLOCK_UUID_TYPE => Ok(DocumentType::ImmutableLedgerBlock),
113-
uuid => anyhow::bail!("Unsupported document type {uuid}"),
113+
_ => anyhow::bail!("Unsupported document type"),
114114
}
115115
}
116116
}

rust/signed_doc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod error;
77
mod metadata;
88
mod signature;
99
mod utils;
10+
pub mod validator;
1011

1112
use std::{
1213
convert::TryFrom,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! Catalyst Signed Documents validation
2+
3+
use catalyst_types::problem_report::ProblemReport;
4+
5+
use crate::{doc_types::DocumentType, error::CatalystSignedDocError, CatalystSignedDocument};
6+
7+
/// `CatalystSignedDocument` type based specific validation
8+
///
9+
/// # Errors
10+
///
11+
/// Returns a report of validation failures and the source error.
12+
pub fn validate(doc: &CatalystSignedDocument) -> Result<(), CatalystSignedDocError> {
13+
let error_report = ProblemReport::new("Catalyst Signed Document Validation");
14+
15+
let doc_type: DocumentType = match doc.doc_type().try_into() {
16+
Ok(doc_type) => doc_type,
17+
Err(e) => {
18+
error_report.invalid_value(
19+
"document `type`",
20+
&doc.doc_type().to_string(),
21+
&e.to_string(),
22+
"verifying document type",
23+
);
24+
return Err(CatalystSignedDocError::new(
25+
error_report,
26+
anyhow::anyhow!("Validation of the Catalyst Signed Document failed"),
27+
));
28+
},
29+
};
30+
31+
#[allow(clippy::match_same_arms)]
32+
match doc_type {
33+
DocumentType::ProposalDocument => {},
34+
DocumentType::ProposalTemplate => {},
35+
DocumentType::CommentDocument => {},
36+
DocumentType::CommentTemplate => {},
37+
DocumentType::ReviewDocument => {},
38+
DocumentType::ReviewTemplate => {},
39+
DocumentType::CategoryParametersDocument => {},
40+
DocumentType::CategoryParametersTemplate => {},
41+
DocumentType::CampaignParametersDocument => {},
42+
DocumentType::CampaignParametersTemplate => {},
43+
DocumentType::BrandParametersDocument => {},
44+
DocumentType::BrandParametersTemplate => {},
45+
DocumentType::ProposalActionDocument => {},
46+
DocumentType::PublicVoteTxV2 => {},
47+
DocumentType::PrivateVoteTxV2 => {},
48+
DocumentType::ImmutableLedgerBlock => {},
49+
}
50+
51+
if error_report.is_problematic() {
52+
return Err(CatalystSignedDocError::new(
53+
error_report,
54+
anyhow::anyhow!("Validation of the Catalyst Signed Document failed"),
55+
));
56+
}
57+
58+
Ok(())
59+
}

0 commit comments

Comments
 (0)