Skip to content

Commit 21b13db

Browse files
committed
add TemplateRule initialisation
1 parent f56471c commit 21b13db

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl Rules {
127127
ver: VerRule,
128128
content_type: ContentTypeRule::new(&doc_spec.headers.content_type)?,
129129
content_encoding: ContentEncodingRule::new(&doc_spec.headers.content_encoding)?,
130-
template: TemplateRule::NotSpecified,
130+
template: TemplateRule::new(&spec.docs, &doc_spec.metadata.template)?,
131131
parameters: ParametersRule::NotSpecified,
132132
doc_ref: RefRule::new(&spec.docs, &doc_spec.metadata.doc_ref)?,
133133
reply: ReplyRule::NotSpecified,

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
//! `template` rule type impl.
22
3+
use std::collections::HashMap;
4+
5+
use catalyst_signed_doc_spec::{
6+
is_required::IsRequired, metadata::template::Template, DocSpec, DocumentName,
7+
};
8+
39
use crate::{
410
providers::CatalystSignedDocumentProvider,
511
validator::{
@@ -22,6 +28,39 @@ pub(crate) enum TemplateRule {
2228
}
2329

2430
impl TemplateRule {
31+
/// Generating `TemplateRule` from specs
32+
pub(crate) fn new(
33+
docs: &HashMap<DocumentName, DocSpec>,
34+
spec: &Template,
35+
) -> anyhow::Result<Self> {
36+
if let IsRequired::Excluded = spec.required {
37+
anyhow::ensure!(
38+
spec.doc_type.is_none() && spec.multiple.is_none(),
39+
"'type' and 'multiple' fields could not been specified when 'required' is 'excluded' for 'template' metadata definition"
40+
);
41+
return Ok(Self::NotSpecified);
42+
}
43+
44+
anyhow::ensure!(
45+
spec.multiple.is_some_and(|v| !v),
46+
"'multiple' must be `false` for 'template' metadata definition"
47+
);
48+
anyhow::ensure!(
49+
spec.required != IsRequired::Optional,
50+
"'required' field cannot been 'optional' for 'template' metadata definition"
51+
);
52+
53+
let doc_name = spec.doc_type.as_ref().ok_or(anyhow::anyhow!(
54+
"'type' field should exists for the required 'template' metadata definition"
55+
))?;
56+
let docs_spec = docs.get(doc_name).ok_or(anyhow::anyhow!(
57+
"cannot find a document definition {doc_name}"
58+
))?;
59+
let allowed_type = docs_spec.doc_type.as_str().parse()?;
60+
61+
Ok(Self::Specified { allowed_type })
62+
}
63+
2564
/// Field validation rule
2665
pub(crate) async fn check<Provider>(
2766
&self,

0 commit comments

Comments
 (0)