Skip to content

Commit 49b541e

Browse files
committed
add full template validation
1 parent d710304 commit 49b541e

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

rust/signed_doc/src/doc_types/comment_document.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub struct CommentDocument {
2626
/// `section` field
2727
#[allow(dead_code)]
2828
section: Option<String>,
29+
/// Comment content
30+
content: serde_json::Value,
2931
}
3032

3133
impl Validator for CommentDocument {
@@ -122,6 +124,34 @@ fn template_full_check(
122124
);
123125
return false;
124126
}
127+
let Ok(template_json_schema) =
128+
serde_json::from_slice(template_doc.doc_content().decoded_bytes())
129+
else {
130+
report.functional_validation(
131+
"Template document content must be json encoded",
132+
"Invalid referenced template document content",
133+
);
134+
return false;
135+
};
136+
let Ok(schema_validator) = jsonschema::options()
137+
.with_draft(jsonschema::Draft::Draft7)
138+
.build(&template_json_schema)
139+
else {
140+
report.functional_validation(
141+
"Template document content must be Draft 7 JSON schema",
142+
"Invalid referenced template document content",
143+
);
144+
return false;
145+
};
146+
147+
if schema_validator.validate(&doc.content).is_err() {
148+
report.functional_validation(
149+
"Comment document content does not compliant with the template json schema",
150+
"Invalid Comment document content",
151+
);
152+
return false;
153+
}
154+
125155
true
126156
};
127157
validate_provided_doc(
@@ -209,12 +239,14 @@ impl CommentDocument {
209239
.ok_or(anyhow::anyhow!("missing `ref` field"))?;
210240
let reply = doc.doc_meta().reply();
211241
let section = doc.doc_meta().section().cloned();
242+
let content = serde_json::from_slice(doc.doc_content().decoded_bytes())?;
212243

213244
Ok(Self {
214245
template,
215246
doc_ref,
216247
reply,
217248
section,
249+
content,
218250
})
219251
}
220252
}

rust/signed_doc/src/doc_types/proposal_document.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ pub const PROPOSAL_DOCUMENT_UUID_TYPE: Uuid =
1919
Uuid::from_u128(0x7808_D2BA_D511_40AF_84E8_C0D1_625F_DFDC);
2020

2121
/// Proposal Document struct
22-
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
2322
pub struct ProposalDocument {
2423
/// `template` field
2524
template: DocumentRef,
2625
/// `category_id` field
2726
category: Option<DocumentRef>,
27+
/// Proposal content
28+
content: serde_json::Value,
2829
}
2930

3031
impl Validator for ProposalDocument {
@@ -110,6 +111,33 @@ fn template_full_check(
110111
);
111112
return false;
112113
}
114+
let Ok(template_json_schema) =
115+
serde_json::from_slice(template_doc.doc_content().decoded_bytes())
116+
else {
117+
report.functional_validation(
118+
"Template document content must be json encoded",
119+
"Invalid referenced template document content",
120+
);
121+
return false;
122+
};
123+
let Ok(schema_validator) = jsonschema::options()
124+
.with_draft(jsonschema::Draft::Draft7)
125+
.build(&template_json_schema)
126+
else {
127+
report.functional_validation(
128+
"Template document content must be Draft 7 JSON schema",
129+
"Invalid referenced template document content",
130+
);
131+
return false;
132+
};
133+
134+
if schema_validator.validate(&doc.content).is_err() {
135+
report.functional_validation(
136+
"Proposal document content does not compliant with the template json schema",
137+
"Invalid Proposal document content",
138+
);
139+
return false;
140+
}
113141
true
114142
};
115143
validate_provided_doc(
@@ -158,8 +186,13 @@ impl ProposalDocument {
158186
.doc_meta()
159187
.template()
160188
.ok_or(anyhow::anyhow!("missing `template` field"))?;
189+
let content = serde_json::from_slice(doc.doc_content().decoded_bytes())?;
161190

162-
Ok(Self { template, category })
191+
Ok(Self {
192+
template,
193+
category,
194+
content,
195+
})
163196
}
164197
}
165198

0 commit comments

Comments
 (0)