|
| 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