Skip to content

Commit 9b83411

Browse files
committed
wip
1 parent 92c9cdf commit 9b83411

File tree

6 files changed

+69
-23
lines changed

6 files changed

+69
-23
lines changed

rust/catalyst-signed-doc-spec/src/metadata/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ pub struct Metadata {
1313
#[serde(rename = "ref")]
1414
pub doc_ref: doc_ref::Ref,
1515
pub reply: reply::Reply,
16+
pub parameters: parameters::Parameters,
1617
}

rust/signed_doc/src/validator/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn proposal_rule() -> Rules {
4949
allowed_type: PROPOSAL_FORM_TEMPLATE.clone(),
5050
},
5151
parameters: ParametersRule::Specified {
52-
exp_parameters_type: parameters.clone(),
52+
allowed_type: parameters.clone(),
5353
optional: false,
5454
},
5555
doc_ref: RefRule::NotSpecified,
@@ -88,7 +88,7 @@ fn proposal_comment_rule() -> Rules {
8888
allowed_type: PROPOSAL_COMMENT_FORM_TEMPLATE.clone(),
8989
},
9090
doc_ref: RefRule::Specified {
91-
exp_ref_types: vec![PROPOSAL.clone()],
91+
allowed_type: vec![PROPOSAL.clone()],
9292
multiple: false,
9393
optional: false,
9494
},
@@ -98,7 +98,7 @@ fn proposal_comment_rule() -> Rules {
9898
},
9999
section: SectionRule::NotSpecified,
100100
parameters: ParametersRule::Specified {
101-
exp_parameters_type: parameters.clone(),
101+
allowed_type: parameters.clone(),
102102
optional: false,
103103
},
104104
content: ContentRule::NotNil,
@@ -143,11 +143,11 @@ fn proposal_submission_action_rule() -> Rules {
143143
},
144144
template: TemplateRule::NotSpecified,
145145
parameters: ParametersRule::Specified {
146-
exp_parameters_type: parameters,
146+
allowed_type: parameters,
147147
optional: false,
148148
},
149149
doc_ref: RefRule::Specified {
150-
exp_ref_types: vec![PROPOSAL.clone()],
150+
allowed_type: vec![PROPOSAL.clone()],
151151
multiple: false,
152152
optional: false,
153153
},

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
pub(crate) enum RefRule {
1515
/// Is 'ref' specified
1616
Specified {
17-
/// expected `type` field of the referenced doc
18-
exp_ref_types: Vec<DocType>,
17+
/// allowed `type` field of the referenced doc
18+
allowed_type: Vec<DocType>,
1919
/// allows multiple document references or only one
2020
multiple: bool,
2121
/// optional flag for the `ref` field
@@ -60,7 +60,7 @@ impl RefRule {
6060
))?;
6161

6262
Ok(Self::Specified {
63-
exp_ref_types,
63+
allowed_type: exp_ref_types,
6464
multiple,
6565
optional,
6666
})
@@ -77,7 +77,7 @@ impl RefRule {
7777
{
7878
let context: &str = "Ref rule check";
7979
if let Self::Specified {
80-
exp_ref_types,
80+
allowed_type: exp_ref_types,
8181
multiple,
8282
optional,
8383
} = self
@@ -466,7 +466,7 @@ mod tests {
466466
let doc = doc_gen(&exp_types, &mut provider);
467467

468468
let non_optional_res = RefRule::Specified {
469-
exp_ref_types: exp_types.to_vec(),
469+
allowed_type: exp_types.to_vec(),
470470
multiple: true,
471471
optional: false,
472472
}
@@ -475,7 +475,7 @@ mod tests {
475475
.unwrap();
476476

477477
let optional_res = RefRule::Specified {
478-
exp_ref_types: exp_types.to_vec(),
478+
allowed_type: exp_types.to_vec(),
479479
multiple: true,
480480
optional: true,
481481
}
@@ -568,7 +568,7 @@ mod tests {
568568
let doc = doc_gen(&exp_types, &mut provider);
569569

570570
let non_optional_res = RefRule::Specified {
571-
exp_ref_types: exp_types.to_vec(),
571+
allowed_type: exp_types.to_vec(),
572572
multiple: false,
573573
optional: false,
574574
}
@@ -577,7 +577,7 @@ mod tests {
577577
.unwrap();
578578

579579
let optional_res = RefRule::Specified {
580-
exp_ref_types: exp_types.to_vec(),
580+
allowed_type: exp_types.to_vec(),
581581
multiple: false,
582582
optional: true,
583583
}
@@ -593,7 +593,7 @@ mod tests {
593593
async fn ref_specified_optional_test() {
594594
let provider = TestCatalystProvider::default();
595595
let rule = RefRule::Specified {
596-
exp_ref_types: vec![UuidV4::new().into()],
596+
allowed_type: vec![UuidV4::new().into()],
597597
multiple: true,
598598
optional: true,
599599
};
@@ -603,7 +603,7 @@ mod tests {
603603

604604
let provider = TestCatalystProvider::default();
605605
let rule = RefRule::Specified {
606-
exp_ref_types: vec![UuidV4::new().into()],
606+
allowed_type: vec![UuidV4::new().into()],
607607
multiple: true,
608608
optional: false,
609609
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Rules {
128128
content_type: ContentTypeRule::new(&doc_spec.headers.content_type)?,
129129
content_encoding: ContentEncodingRule::new(&doc_spec.headers.content_encoding)?,
130130
template: TemplateRule::new(&spec.docs, &doc_spec.metadata.template)?,
131-
parameters: ParametersRule::NotSpecified,
131+
parameters: ParametersRule::new(&spec.docs, &doc_spec.metadata.parameters)?,
132132
doc_ref: RefRule::new(&spec.docs, &doc_spec.metadata.doc_ref)?,
133133
reply: ReplyRule::new(&spec.docs, &doc_spec.metadata.reply)?,
134134
section: SectionRule::NotSpecified,

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! `parameters` rule type impl.
22
3+
use std::collections::HashMap;
4+
5+
use catalyst_signed_doc_spec::{
6+
is_required::IsRequired, metadata::parameters::Parameters, DocSpec, DocumentName,
7+
};
38
use catalyst_types::problem_report::ProblemReport;
49
use futures::FutureExt;
510

@@ -14,7 +19,7 @@ pub(crate) enum ParametersRule {
1419
/// Is `parameters` specified
1520
Specified {
1621
/// expected `type` field of the parameter doc
17-
exp_parameters_type: Vec<DocType>,
22+
allowed_type: Vec<DocType>,
1823
/// optional flag for the `parameters` field
1924
optional: bool,
2025
},
@@ -24,6 +29,46 @@ pub(crate) enum ParametersRule {
2429
}
2530

2631
impl ParametersRule {
32+
/// Generating `ParametersRule` from specs
33+
pub(crate) fn new(
34+
docs: &HashMap<DocumentName, DocSpec>,
35+
spec: &Parameters,
36+
) -> anyhow::Result<Self> {
37+
let optional = match spec.required {
38+
IsRequired::Yes => false,
39+
IsRequired::Optional => true,
40+
IsRequired::Excluded => {
41+
anyhow::ensure!(
42+
spec.doc_type.is_empty() && spec.multiple.is_none(),
43+
"'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'template' metadata definition"
44+
);
45+
return Ok(Self::NotSpecified);
46+
},
47+
};
48+
49+
anyhow::ensure!(!spec.doc_type.is_empty(), "'type' field should exists and has at least one entry for the required 'ref' metadata definition");
50+
anyhow::ensure!(
51+
spec.multiple.is_some_and(|v| !v),
52+
"'multiple' field should be only set to false for the required 'reply' metadata definition"
53+
);
54+
55+
let allowed_type = spec.doc_type.iter().try_fold(
56+
Vec::new(),
57+
|mut res, doc_name| -> anyhow::Result<_> {
58+
let docs_spec = docs.get(doc_name).ok_or(anyhow::anyhow!(
59+
"cannot find a document definition {doc_name}"
60+
))?;
61+
res.push(docs_spec.doc_type.as_str().parse()?);
62+
Ok(res)
63+
},
64+
)?;
65+
66+
Ok(Self::Specified {
67+
allowed_type,
68+
optional,
69+
})
70+
}
71+
2772
/// Field validation rule
2873
pub(crate) async fn check<Provider>(
2974
&self,
@@ -35,7 +80,7 @@ impl ParametersRule {
3580
{
3681
let context: &str = "Parameter rule check";
3782
if let Self::Specified {
38-
exp_parameters_type,
83+
allowed_type: exp_parameters_type,
3984
optional,
4085
} = self
4186
{
@@ -722,15 +767,15 @@ mod tests {
722767
let doc = doc_gen(&exp_param_types, &mut provider);
723768

724769
let non_optional_res = ParametersRule::Specified {
725-
exp_parameters_type: exp_param_types.to_vec(),
770+
allowed_type: exp_param_types.to_vec(),
726771
optional: false,
727772
}
728773
.check(&doc, &provider)
729774
.await
730775
.unwrap();
731776

732777
let optional_res = ParametersRule::Specified {
733-
exp_parameters_type: exp_param_types.to_vec(),
778+
allowed_type: exp_param_types.to_vec(),
734779
optional: true,
735780
}
736781
.check(&doc, &provider)
@@ -745,7 +790,7 @@ mod tests {
745790
async fn ref_specified_optional_test() {
746791
let provider = TestCatalystProvider::default();
747792
let rule = ParametersRule::Specified {
748-
exp_parameters_type: vec![UuidV4::new().into()],
793+
allowed_type: vec![UuidV4::new().into()],
749794
optional: true,
750795
};
751796

@@ -754,7 +799,7 @@ mod tests {
754799

755800
let provider = TestCatalystProvider::default();
756801
let rule = ParametersRule::Specified {
757-
exp_parameters_type: vec![UuidV4::new().into()],
802+
allowed_type: vec![UuidV4::new().into()],
758803
optional: false,
759804
};
760805

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) enum ReplyRule {
2626
}
2727

2828
impl ReplyRule {
29-
/// Generating `RefRule` from specs
29+
/// Generating `ReplyRule` from specs
3030
pub(crate) fn new(
3131
docs: &HashMap<DocumentName, DocSpec>,
3232
spec: &Reply,

0 commit comments

Comments
 (0)