Skip to content

Commit 9313ec8

Browse files
committed
add content type rule
1 parent 21b13db commit 9313ec8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::fmt::Debug;
44

5+
use catalyst_signed_doc_spec::payload::Payload;
56
use minicbor::Encode;
67

78
use crate::{
@@ -39,6 +40,26 @@ pub(crate) enum ContentRule {
3940
}
4041

4142
impl ContentRule {
43+
/// Generating `ContentRule` from specs
44+
pub(crate) fn new(spec: &Payload) -> anyhow::Result<Self> {
45+
if spec.nil {
46+
anyhow::ensure!(
47+
spec.schema.is_none(),
48+
"'schema' field could not been specified when 'nil' is 'true' for 'payload' definition"
49+
);
50+
return Ok(Self::Nil);
51+
}
52+
53+
if let Some(schema) = &spec.schema {
54+
let schema_str = schema.to_string();
55+
Ok(Self::StaticSchema(ContentSchema::Json(
56+
json_schema::JsonSchema::try_from(&serde_json::from_str(&schema_str)?)?,
57+
)))
58+
} else {
59+
Ok(Self::NotNil)
60+
}
61+
}
62+
4263
/// Field validation rule
4364
#[allow(clippy::unused_async)]
4465
pub(crate) async fn check(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Rules {
132132
doc_ref: RefRule::new(&spec.docs, &doc_spec.metadata.doc_ref)?,
133133
reply: ReplyRule::NotSpecified,
134134
section: SectionRule::NotSpecified,
135-
content: ContentRule::Nil,
135+
content: ContentRule::new(&doc_spec.payload)?,
136136
kid: SignatureKidRule { exp: &[] },
137137
signature: SignatureRule { mutlisig: false },
138138
original_author: OriginalAuthorRule,

0 commit comments

Comments
 (0)