Skip to content

Commit d3f0492

Browse files
Move the check to the constructor
1 parent 47577a6 commit d3f0492

File tree

3 files changed

+27
-67
lines changed

3 files changed

+27
-67
lines changed

rust/signed_doc/src/validator/rules/chain/mod.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! `chain` rule type impl.
22
3+
use anyhow::ensure;
34
use catalyst_signed_doc_spec::{is_required::IsRequired, metadata::chain::Chain};
4-
5+
use catalyst_signed_doc_spec::metadata::collaborators::Collaborators;
56
use crate::{
67
CatalystSignedDocument, providers::CatalystSignedDocumentProvider,
7-
validator::rules::CollaboratorsRule,
88
};
99

1010
#[cfg(test)]
@@ -24,25 +24,25 @@ pub(crate) enum ChainRule {
2424

2525
impl ChainRule {
2626
/// Generating `ChainRule` from specs
27-
pub(crate) fn new(spec: &Chain) -> Self {
27+
pub(crate) fn new(spec: &Chain, collaborators_spec: &Collaborators) -> anyhow::Result<Self> {
2828
let optional = match spec.required {
2929
IsRequired::Yes => false,
3030
IsRequired::Optional => true,
3131
IsRequired::Excluded => {
32-
return Self::NotSpecified;
33-
},
32+
return Ok(Self::NotSpecified);
33+
}
3434
};
3535

36-
Self::Specified { optional }
36+
ensure!(matches!(collaborators_spec.required, IsRequired::Excluded), "Chained Documents do not support collaborators");
37+
38+
Ok(Self::Specified { optional })
3739
}
3840

3941
/// Field validation rule
40-
#[allow(clippy::too_many_lines)]
4142
pub(crate) async fn check<Provider>(
4243
&self,
4344
doc: &CatalystSignedDocument,
4445
provider: &Provider,
45-
collaborators_rule: &CollaboratorsRule,
4646
) -> anyhow::Result<bool>
4747
where
4848
Provider: CatalystSignedDocumentProvider,
@@ -141,14 +141,6 @@ impl ChainRule {
141141
}
142142
}
143143
}
144-
145-
if let CollaboratorsRule::Specified { .. } = collaborators_rule {
146-
doc.report().functional_validation(
147-
"Chained Documents do not support collaborators",
148-
"Chained Documents validation",
149-
);
150-
return Ok(false);
151-
}
152144
}
153145
if let Self::NotSpecified = self
154146
&& chain.is_some()

rust/signed_doc/src/validator/rules/chain/tests.rs

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use catalyst_signed_doc_spec::{is_required::IsRequired, metadata::chain::Chain as ChainSpec};
12
use catalyst_types::uuid::{UuidV4, UuidV7};
23
use test_case::test_case;
34

45
use super::*;
56
use crate::{
6-
Chain, DocType, builder::tests::Builder, metadata::SupportedField,
7-
providers::tests::TestCatalystProvider,
7+
builder::tests::Builder, metadata::SupportedField, providers::tests::TestCatalystProvider,
8+
Chain, DocType,
89
};
910

1011
mod helper {
@@ -41,49 +42,22 @@ async fn test_without_chaining_documents() {
4142
.build();
4243

4344
let rule = ChainRule::NotSpecified;
44-
let collaborators_rule = CollaboratorsRule::NotSpecified;
45-
46-
assert!(
47-
rule.check(&doc, &provider, &collaborators_rule)
48-
.await
49-
.unwrap()
50-
);
45+
assert!(rule.check(&doc, &provider).await.unwrap());
5146
let rule = ChainRule::Specified { optional: true };
52-
assert!(
53-
rule.check(&doc, &provider, &collaborators_rule)
54-
.await
55-
.unwrap()
56-
);
47+
assert!(rule.check(&doc, &provider).await.unwrap());
5748
let rule = ChainRule::Specified { optional: false };
58-
assert!(
59-
!rule
60-
.check(&doc, &provider, &collaborators_rule)
61-
.await
62-
.unwrap()
63-
);
49+
assert!(!rule.check(&doc, &provider).await.unwrap());
6450
}
6551

6652
#[tokio::test]
6753
async fn chain_rule_collaborators_rule_conflict() {
68-
let doc_type = UuidV4::new();
69-
let doc_id = UuidV7::new();
70-
let doc_ver = UuidV7::new();
71-
72-
let provider = TestCatalystProvider::default();
73-
let doc = Builder::new()
74-
.with_metadata_field(SupportedField::Type(DocType::from(doc_type)))
75-
.with_metadata_field(SupportedField::Id(doc_id))
76-
.with_metadata_field(SupportedField::Ver(doc_ver))
77-
.build();
78-
79-
let rule = ChainRule::Specified { optional: true };
80-
let collaborators_rule = CollaboratorsRule::Specified { optional: true };
81-
assert!(
82-
!rule
83-
.check(&doc, &provider, &collaborators_rule)
84-
.await
85-
.unwrap()
86-
);
54+
let chain = ChainSpec {
55+
required: IsRequired::Optional,
56+
};
57+
let collaborators = Collaborators {
58+
required: IsRequired::Optional,
59+
};
60+
ChainRule::new(&chain, &collaborators).unwrap_err();
8761
}
8862

8963
#[test_case(
@@ -169,11 +143,8 @@ async fn test_valid_chained_documents(
169143
(provider, doc): (TestCatalystProvider, CatalystSignedDocument)
170144
) -> bool {
171145
let rule = ChainRule::Specified { optional: false };
172-
let collaborators_rule = CollaboratorsRule::NotSpecified;
173146

174-
rule.check(&doc, &provider, &collaborators_rule)
175-
.await
176-
.unwrap()
147+
rule.check(&doc, &provider).await.unwrap()
177148
}
178149

179150
#[test_case(
@@ -319,9 +290,6 @@ async fn test_invalid_chained_documents(
319290
(provider, doc): (TestCatalystProvider, CatalystSignedDocument)
320291
) -> bool {
321292
let rule = ChainRule::Specified { optional: false };
322-
let collaborators_rule = CollaboratorsRule::NotSpecified;
323293

324-
rule.check(&doc, &provider, &collaborators_rule)
325-
.await
326-
.unwrap()
294+
rule.check(&doc, &provider).await.unwrap()
327295
}

rust/signed_doc/src/validator/rules/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
//! <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/meta/>
33
44
use anyhow::Context;
5-
use catalyst_signed_doc_spec::{DocSpec, DocSpecs, cddl_definitions::CddlDefinitions};
5+
use catalyst_signed_doc_spec::{cddl_definitions::CddlDefinitions, DocSpec, DocSpecs};
66
use futures::FutureExt;
77

88
use crate::{
9-
CatalystSignedDocument,
109
providers::{CatalystIdProvider, CatalystSignedDocumentProvider},
10+
CatalystSignedDocument,
1111
};
1212

1313
mod chain;
@@ -98,7 +98,7 @@ impl Rules {
9898
self.reply.check(doc, provider).boxed(),
9999
self.section.check(doc).boxed(),
100100
self.parameters.check(doc, provider).boxed(),
101-
self.chain.check(doc, provider, &self.collaborators).boxed(),
101+
self.chain.check(doc, provider).boxed(),
102102
self.collaborators.check(doc).boxed(),
103103
self.content.check(doc).boxed(),
104104
self.kid.check(doc).boxed(),
@@ -129,7 +129,7 @@ impl Rules {
129129
content_encoding: ContentEncodingRule::new(&doc_spec.headers.content_encoding)?,
130130
template: TemplateRule::new(all_docs_specs, &doc_spec.metadata.template)?,
131131
parameters: ParametersRule::new(all_docs_specs, &doc_spec.metadata.parameters)?,
132-
chain: ChainRule::new(&doc_spec.metadata.chain),
132+
chain: ChainRule::new(&doc_spec.metadata.chain, &doc_spec.metadata.collaborators)?,
133133
doc_ref: RefRule::new(all_docs_specs, &doc_spec.metadata.doc_ref)?,
134134
reply: ReplyRule::new(all_docs_specs, &doc_spec.metadata.reply)?,
135135
section: SectionRule::NotSpecified,

0 commit comments

Comments
 (0)