Skip to content

Commit 5599305

Browse files
feat(rust/signed-doc): ChainRule mutual exclusive with the CollaboratorsRule (#649)
* ChainRule mutual exclusive with the CollaboratorsRule * Move the check to the constructor * Fix formatting
1 parent bbc0a5d commit 5599305

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! `chain` rule type impl.
22
3-
use catalyst_signed_doc_spec::{is_required::IsRequired, metadata::chain::Chain};
3+
use anyhow::ensure;
4+
use catalyst_signed_doc_spec::{
5+
is_required::IsRequired,
6+
metadata::{chain::Chain, collaborators::Collaborators},
7+
};
48

59
use crate::{CatalystSignedDocument, providers::CatalystSignedDocumentProvider};
610

@@ -21,16 +25,24 @@ pub(crate) enum ChainRule {
2125

2226
impl ChainRule {
2327
/// Generating `ChainRule` from specs
24-
pub(crate) fn new(spec: &Chain) -> Self {
28+
pub(crate) fn new(
29+
spec: &Chain,
30+
collaborators_spec: &Collaborators,
31+
) -> anyhow::Result<Self> {
2532
let optional = match spec.required {
2633
IsRequired::Yes => false,
2734
IsRequired::Optional => true,
2835
IsRequired::Excluded => {
29-
return Self::NotSpecified;
36+
return Ok(Self::NotSpecified);
3037
},
3138
};
3239

33-
Self::Specified { optional }
40+
ensure!(
41+
matches!(collaborators_spec.required, IsRequired::Excluded),
42+
"Chained Documents do not support collaborators"
43+
);
44+
45+
Ok(Self::Specified { optional })
3446
}
3547

3648
/// Field validation rule

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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

@@ -48,6 +49,17 @@ async fn test_without_chaining_documents() {
4849
assert!(!rule.check(&doc, &provider).await.unwrap());
4950
}
5051

52+
#[tokio::test]
53+
async fn chain_rule_collaborators_rule_conflict() {
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();
61+
}
62+
5163
#[test_case(
5264
{
5365
let doc_type = UuidV4::new();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)