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

input-output-hk / catalyst-libs / 20908186781

12 Jan 2026 04:34AM UTC coverage: 69.556% (+2.4%) from 67.194%
20908186781

push

github

web-flow
feat(rust/signed-doc): `Contest Delegation` validation (#750)

* wip

* add separate `rep_nomination_ref_check` function

* wip

* wip

* wip

* wip

* add contest_delegation test for `catalyst-contest`

* wip

* fix test

* add test case

* fix fmt, fix spelling

* fix comment

* wip

* fix clippy

* cleanup

148 of 179 new or added lines in 6 files covered. (82.68%)

204 existing lines in 26 files now uncovered.

16007 of 23013 relevant lines covered (69.56%)

2651.03 hits per line

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

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

3
pub(crate) mod rules;
4

5
#[cfg(target_arch = "wasm32")]
6
use std::collections::HashMap;
7
use std::fmt::Debug;
8

9
#[cfg(not(target_arch = "wasm32"))]
10
use dashmap::DashMap;
11

12
use crate::{
13
    CatalystSignedDocument, metadata::DocType, providers::Provider,
14
    validator::rules::documents_rules_from_spec,
15
};
16

17
/// `CatalystSignedDocument` validation rule trait
18
#[cfg(not(target_arch = "wasm32"))]
19
pub trait CatalystSignedDocumentValidationRule: 'static + Send + Sync + Debug {
20
    /// Validates `CatalystSignedDocument`, return `false` if the provided
21
    /// `CatalystSignedDocument` violates some validation rules with properly filling the
22
    /// problem report.
23
    ///
24
    /// # Errors
25
    /// If `provider` returns error, fails fast throwing that error.
26
    fn check(
27
        &self,
28
        doc: &CatalystSignedDocument,
29
        provider: &dyn Provider,
30
    ) -> anyhow::Result<bool>;
31
}
32

33
/// `CatalystSignedDocument` validation rule trait
34
#[cfg(target_arch = "wasm32")]
35
pub trait CatalystSignedDocumentValidationRule: 'static + Debug {
36
    /// Validates `CatalystSignedDocument`, return `false` if the provided
37
    /// `CatalystSignedDocument` violates some validation rules with properly filling the
38
    /// problem report.
39
    ///
40
    /// # Errors
41
    /// If `provider` returns error, fails fast throwing that error.
42
    fn check(
43
        &self,
44
        doc: &CatalystSignedDocument,
45
        provider: &dyn Provider,
46
    ) -> anyhow::Result<bool>;
47
}
48

49
/// Struct represented a collection of rules
50
pub type Rules = Vec<Box<dyn CatalystSignedDocumentValidationRule>>;
51

52
/// `CatalystSignedDocument` validator type.
53
#[cfg(not(target_arch = "wasm32"))]
54
pub struct Validator(DashMap<DocType, Rules>);
55

56
/// `CatalystSignedDocument` validator type.
57
#[cfg(target_arch = "wasm32")]
58
pub struct Validator(HashMap<DocType, Rules>);
59

60
impl Default for Validator {
UNCOV
61
    fn default() -> Self {
×
UNCOV
62
        Self::new()
×
UNCOV
63
    }
×
64
}
65

66
impl Validator {
67
    /// # Panics
68
    /// - Cannot fail to initialize validation rules. Should never happen.
69
    #[allow(clippy::expect_used)]
70
    #[must_use]
71
    pub fn new() -> Self {
138✔
72
        Self(
138✔
73
            documents_rules_from_spec()
138✔
74
                .expect("Cannot fail to initialize validation rules. Should never happen.")
138✔
75
                .collect(),
138✔
76
        )
138✔
77
    }
138✔
78

79
    /// A comprehensive document type based validation of the `CatalystSignedDocument`.
80
    /// Includes time based validation of the `id` and `ver` fields based on the provided
81
    /// `future_threshold` and `past_threshold` threshold values (in seconds).
82
    /// Return true if it is valid, otherwise return false.
83
    ///
84
    /// # Errors
85
    /// If `provider` returns error, fails fast throwing that error.
86
    pub fn validate(
137✔
87
        &self,
137✔
88
        doc: &CatalystSignedDocument,
137✔
89
        provider: &impl Provider,
137✔
90
    ) -> anyhow::Result<bool> {
137✔
91
        let Ok(doc_type) = doc.doc_type() else {
137✔
92
            doc.report().missing_field(
×
93
                "type",
×
94
                "Can't get a document type during the validation process",
×
95
            );
96
            return Ok(false);
×
97
        };
98

99
        let Some(rules) = self.0.get(doc_type) else {
137✔
100
            doc.report().invalid_value(
×
UNCOV
101
                "`type`",
×
UNCOV
102
                &doc.doc_type()?.to_string(),
×
UNCOV
103
                "Must be a known document type value",
×
UNCOV
104
                "Unsupported document type",
×
105
            );
UNCOV
106
            return Ok(false);
×
107
        };
108

109
        let res = rules
137✔
110
            .iter()
137✔
111
            .map(|v| v.check(doc, provider))
2,058✔
112
            .collect::<anyhow::Result<Vec<_>>>()?
137✔
113
            .iter()
137✔
114
            .all(|res| *res);
137✔
115
        Ok(res)
137✔
116
    }
137✔
117

118
    /// Extend the current defined validation rules set for the provided document type.
119
    #[cfg(not(target_arch = "wasm32"))]
120
    pub fn extend_rules_per_document(
3✔
121
        &mut self,
3✔
122
        doc_type: DocType,
3✔
123
        rule: impl CatalystSignedDocumentValidationRule,
3✔
124
    ) {
3✔
125
        self.0.entry(doc_type).or_default().push(Box::new(rule));
3✔
126
    }
3✔
127

128
    /// Extend the current defined validation rules set for the provided document type.
129
    #[cfg(target_arch = "wasm32")]
130
    pub fn extend_rules_per_document(
131
        &mut self,
132
        doc_type: DocType,
133
        rule: impl CatalystSignedDocumentValidationRule,
134
    ) {
135
        self.0.entry(doc_type).or_default().push(Box::new(rule));
136
    }
137
}
138

139
#[cfg(test)]
140
mod tests {
141
    use super::*;
142

143
    #[test]
144
    fn document_rules_init_test() {
1✔
145
        let _unused = Validator::new();
1✔
146
    }
1✔
147
}
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