• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / catalyst-libs / 19934578682

04 Dec 2025 03:37PM UTC coverage: 67.785% (-0.05%) from 67.835%
19934578682

Pull #680

github

web-flow
Merge d7f554c56 into f08d044b7
Pull Request #680: feat(rust/signed-doc): Catalyst Signed Documents extended validation capabilities

252 of 259 new or added lines in 27 files covered. (97.3%)

7 existing lines in 2 files now uncovered.

13997 of 20649 relevant lines covered (67.79%)

2708.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.97
/rust/signed_doc/src/validator/mod.rs
1
//! Catalyst Signed Documents validation logic
2

3
pub(crate) mod rules;
4

5
use std::{collections::HashMap, fmt::Debug, sync::LazyLock};
6

7
use futures::{StreamExt, TryStreamExt};
8

9
use crate::{
10
    CatalystSignedDocument,
11
    metadata::DocType,
12
    providers::{
13
        CatalystIdProvider, CatalystSignedDocumentAndCatalystIdProvider,
14
        CatalystSignedDocumentProvider,
15
    },
16
    validator::rules::{Rules, documents_rules_from_spec},
17
};
18

19
/// `CatalystSignedDocument` validation rule trait
20
#[async_trait::async_trait]
21
pub trait CatalystSignedDocumentValidationRule: Send + Sync + Debug {
22
    /// Validates `CatalystSignedDocument`, return `false` if the provided
23
    /// `CatalystSignedDocument` violates some validation rules with properly filling the
24
    /// problem report.
25
    async fn check(
26
        &self,
27
        doc: &CatalystSignedDocument,
28
        provider: &dyn CatalystSignedDocumentAndCatalystIdProvider,
29
    ) -> anyhow::Result<bool>;
30
}
31

32
/// A table representing a full set or validation rules per document id.
33
static DOCUMENT_RULES: LazyLock<HashMap<DocType, Rules>> = LazyLock::new(document_rules_init);
34

35
/// `DOCUMENT_RULES` initialization function
36
#[allow(clippy::expect_used)]
37
fn document_rules_init() -> HashMap<DocType, Rules> {
106✔
38
    let document_rules_map: HashMap<DocType, Rules> = documents_rules_from_spec()
106✔
39
        .expect("cannot fail to initialize validation rules")
106✔
40
        .collect();
106✔
41

42
    document_rules_map
106✔
43
}
106✔
44

45
/// A comprehensive document type based validation of the `CatalystSignedDocument`.
46
/// Includes time based validation of the `id` and `ver` fields based on the provided
47
/// `future_threshold` and `past_threshold` threshold values (in seconds).
48
/// Return true if it is valid, otherwise return false.
49
///
50
/// # Errors
51
/// If `provider` returns error, fails fast throwing that error.
52
pub async fn validate<Provider>(
105✔
53
    doc: &CatalystSignedDocument,
105✔
54
    provider: &Provider,
105✔
55
) -> anyhow::Result<bool>
105✔
56
where
105✔
57
    Provider: CatalystSignedDocumentProvider + CatalystIdProvider,
105✔
58
{
105✔
59
    let Ok(doc_type) = doc.doc_type() else {
105✔
UNCOV
60
        doc.report().missing_field(
×
UNCOV
61
            "type",
×
UNCOV
62
            "Can't get a document type during the validation process",
×
63
        );
64
        return Ok(false);
×
65
    };
66

67
    let Some(rules) = DOCUMENT_RULES.get(doc_type) else {
105✔
68
        doc.report().invalid_value(
×
UNCOV
69
            "`type`",
×
UNCOV
70
            &doc.doc_type()?.to_string(),
×
UNCOV
71
            "Must be a known document type value",
×
72
            "Unsupported document type",
×
73
        );
74
        return Ok(false);
×
75
    };
76

77
    let iter = rules.iter().map(|v| v.check(doc, provider));
1,575✔
78
    let res = futures::stream::iter(iter)
105✔
79
        .buffer_unordered(rules.len())
105✔
80
        .try_collect::<Vec<_>>()
105✔
81
        .await?
105✔
82
        .iter()
105✔
83
        .all(|res| *res);
105✔
84
    Ok(res)
105✔
85
}
105✔
86

87
#[cfg(test)]
88
mod tests {
89
    use crate::validator::document_rules_init;
90

91
    #[test]
92
    fn document_rules_init_test() {
1✔
93
        document_rules_init();
1✔
94
    }
1✔
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc