identity_credential/credential/
schema.rs1use serde::Deserialize;
5use serde::Serialize;
6
7use identity_core::common::Object;
8use identity_core::common::OneOrMany;
9use identity_core::common::Url;
10
11#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
15pub struct Schema {
16 pub id: Url,
18 #[serde(rename = "type")]
20 pub types: OneOrMany<String>,
21 #[serde(flatten)]
23 pub properties: Object,
24}
25
26impl Schema {
27 pub fn new<T>(id: Url, types: T) -> Self
29 where
30 T: Into<OneOrMany<String>>,
31 {
32 Self::with_properties(id, types, Object::new())
33 }
34
35 pub fn with_properties<T>(id: Url, types: T, properties: Object) -> Self
37 where
38 T: Into<OneOrMany<String>>,
39 {
40 Self {
41 id,
42 types: types.into(),
43 properties,
44 }
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use identity_core::convert::FromJson;
51
52 use crate::credential::Schema;
53
54 const JSON1: &str = include_str!("../../tests/fixtures/schema-1.json");
55 const JSON2: &str = include_str!("../../tests/fixtures/schema-2.json");
56 const JSON3: &str = include_str!("../../tests/fixtures/schema-3.json");
57
58 #[test]
59 fn test_from_json() {
60 let schema: Schema = Schema::from_json(JSON1).unwrap();
61 assert_eq!(schema.id, "https://example.org/examples/degree.json");
62 assert_eq!(schema.types.as_slice(), ["JsonSchemaValidator2018"]);
63
64 let schema: Schema = Schema::from_json(JSON2).unwrap();
65 assert_eq!(schema.id, "https://example.org/examples/degree.zkp");
66 assert_eq!(schema.types.as_slice(), ["ZkpExampleSchema2018"]);
67
68 let schema: Schema = Schema::from_json(JSON3).unwrap();
69 assert_eq!(schema.id, "did:example:cdf:35LB7w9ueWbagPL94T9bMLtyXDj9pX5o");
70 assert_eq!(
71 schema.types.as_slice(),
72 ["did:example:schema:22KpkXgecryx9k7N6XN1QoN3gXwBkSU8SfyyYQG"]
73 );
74 }
75}