Skip to content

Commit 48450f1

Browse files
committed
wip
1 parent 6bcbda6 commit 48450f1

File tree

10 files changed

+43
-160
lines changed

10 files changed

+43
-160
lines changed

rust/catalyst-signed-doc-spec/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ license.workspace = true
1111
workspace = true
1212

1313
[dependencies]
14+
catalyst-types = { version = "0.0.11", git = "https://github.com/input-output-hk/catalyst-libs.git", branch = "feat/json-schema-to-catalyst-types" }
15+
1416
serde_json = "1.0.142"
1517
anyhow = "1.0.99"
1618
serde = { version = "1.0.219", features = ["derive"] }
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! `signed_doc.json` "payload" field JSON definition
22
3+
use catalyst_types::json_schema::JsonSchema;
4+
use serde::Deserialize;
5+
36
/// `signed_doc.json` "payload" field JSON object
47
#[derive(serde::Deserialize)]
58
#[allow(clippy::missing_docs_in_private_items)]
@@ -8,9 +11,28 @@ pub struct Payload {
811
pub schema: Option<Schema>,
912
}
1013

11-
#[derive(serde::Deserialize)]
12-
#[serde(untagged)]
1314
pub enum Schema {
1415
Cddl(String),
15-
JsonSchema(serde_json::Value),
16+
Json(JsonSchema),
17+
}
18+
19+
impl<'de> Deserialize<'de> for Schema {
20+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
21+
where D: serde::Deserializer<'de> {
22+
#[derive(serde::Deserialize)]
23+
#[serde(untagged)]
24+
pub enum SchemaSerde {
25+
Cddl(String),
26+
Json(serde_json::Value),
27+
}
28+
29+
match SchemaSerde::deserialize(deserializer)? {
30+
SchemaSerde::Json(schema) => {
31+
JsonSchema::try_from(&schema)
32+
.map(|v| Self::Json(v))
33+
.map_err(|e| serde::de::Error::custom(e))
34+
},
35+
SchemaSerde::Cddl(schema) => Ok(Self::Cddl(schema)),
36+
}
37+
}
1638
}

rust/signed_doc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license.workspace = true
1111
workspace = true
1212

1313
[dependencies]
14-
catalyst-types = { version = "0.0.10", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "catalyst-types/v0.0.10" }
14+
catalyst-types = { version = "0.0.11", git = "https://github.com/input-output-hk/catalyst-libs.git", branch = "feat/json-schema-to-catalyst-types" }
1515
cbork-utils = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "cbork-utils-v0.0.2" }
1616

1717
catalyst-signed-doc-macro = { version = "0.0.1", path = "../catalyst-signed-doc-macro" }

rust/signed_doc/src/validator/json_schema.rs

Lines changed: 0 additions & 139 deletions
This file was deleted.

rust/signed_doc/src/validator/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Catalyst Signed Documents validation logic
22
3-
pub(crate) mod json_schema;
43
pub(crate) mod rules;
54

65
use std::{collections::HashMap, sync::LazyLock};

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ mod tests;
66
use std::fmt::Debug;
77

88
use catalyst_signed_doc_spec::payload::{Payload, Schema};
9+
use catalyst_types::json_schema::JsonSchema;
910
use minicbor::Encode;
1011

11-
use crate::{
12-
validator::{json_schema, rules::utils::content_json_schema_check},
13-
CatalystSignedDocument,
14-
};
12+
use crate::{validator::rules::utils::content_json_schema_check, CatalystSignedDocument};
1513

1614
/// Enum represents different content schemas, against which documents content would be
1715
/// validated.
1816
pub(crate) enum ContentSchema {
1917
/// Draft 7 JSON schema
20-
Json(json_schema::JsonSchema),
18+
Json(JsonSchema),
2119
/// CDDL schema
2220
Cddl,
2321
}
@@ -57,10 +55,8 @@ impl ContentRule {
5755
}
5856

5957
match &spec.schema {
60-
Some(Schema::JsonSchema(schema)) => {
61-
Ok(Self::StaticSchema(ContentSchema::Json(
62-
json_schema::JsonSchema::try_from(schema)?,
63-
)))
58+
Some(Schema::Json(schema)) => {
59+
Ok(Self::StaticSchema(ContentSchema::Json(schema.clone())))
6460
},
6561
Some(Schema::Cddl(_)) => Ok(Self::StaticSchema(ContentSchema::Cddl)),
6662
None => Ok(Self::NotNil),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use catalyst_types::json_schema::JsonSchema;
12
use test_case::test_case;
23

34
use super::*;
@@ -36,7 +37,7 @@ use crate::builder::tests::Builder;
3637
async fn content_rule_specified_test(
3738
doc_gen: impl FnOnce(Vec<u8>) -> CatalystSignedDocument
3839
) -> bool {
39-
let schema = json_schema::JsonSchema::try_from(&serde_json::json!({})).unwrap();
40+
let schema = JsonSchema::try_from(&serde_json::json!({})).unwrap();
4041
let content_schema = ContentSchema::Json(schema);
4142
let valid_content = serde_json::to_vec(&serde_json::json!({})).unwrap();
4243

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#[cfg(test)]
44
mod tests;
55

6-
use crate::{metadata::ContentType, validator::json_schema::JsonSchema, CatalystSignedDocument};
6+
use catalyst_types::json_schema::JsonSchema;
7+
8+
use crate::{metadata::ContentType, CatalystSignedDocument};
79

810
/// `content-type` field validation rule
911
#[derive(Debug)]

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ mod tests;
66
use catalyst_signed_doc_spec::{
77
is_required::IsRequired, metadata::template::Template, DocSpecs, DocumentName,
88
};
9+
use catalyst_types::json_schema::JsonSchema;
910

1011
use crate::{
1112
providers::CatalystSignedDocumentProvider,
12-
validator::{
13-
json_schema::JsonSchema,
14-
rules::{doc_ref::doc_refs_check, utils::content_json_schema_check},
15-
},
13+
validator::rules::{doc_ref::doc_refs_check, utils::content_json_schema_check},
1614
CatalystSignedDocument, ContentType, DocType,
1715
};
1816

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
use std::fmt::Write;
44

5-
use crate::{validator::json_schema::JsonSchema, CatalystSignedDocument};
5+
use catalyst_types::json_schema::JsonSchema;
6+
7+
use crate::CatalystSignedDocument;
68

79
/// Validating the document's content against the provided JSON schema
810
pub(crate) fn content_json_schema_check(

0 commit comments

Comments
 (0)