1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
use crate::identifiers::schema::SchemaId; use crate::utils::Qualifiable; use crate::{Validatable, ValidationError}; use std::collections::HashSet; use std::iter::FromIterator; pub const MAX_ATTRIBUTES_COUNT: usize = 125; #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(tag = "ver"))] pub enum Schema { #[cfg_attr(feature = "serde", serde(rename = "1.0"))] SchemaV1(SchemaV1), } impl Schema { pub fn to_unqualified(self) -> Schema { match self { Schema::SchemaV1(schema) => Schema::SchemaV1(SchemaV1 { id: schema.id.to_unqualified(), name: schema.name, version: schema.version, attr_names: schema.attr_names, seq_no: schema.seq_no, }), } } } impl Validatable for Schema { fn validate(&self) -> Result<(), ValidationError> { match self { Schema::SchemaV1(schema) => schema.validate(), } } } #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct SchemaV1 { pub id: SchemaId, pub name: String, pub version: String, #[cfg_attr(feature = "serde", serde(rename = "attrNames"))] pub attr_names: AttributeNames, pub seq_no: Option<u32>, } #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct AttributeNames(pub HashSet<String>); impl AttributeNames { pub fn new() -> Self { AttributeNames(HashSet::new()) } } impl From<Vec<String>> for AttributeNames { fn from(attrs: Vec<String>) -> Self { Self(HashSet::from_iter(attrs)) } } impl From<HashSet<String>> for AttributeNames { fn from(attrs: HashSet<String>) -> Self { Self(attrs) } } impl Into<HashSet<String>> for AttributeNames { fn into(self) -> HashSet<String> { self.0 } } impl Validatable for SchemaV1 { fn validate(&self) -> Result<(), ValidationError> { self.attr_names.validate()?; self.id.validate()?; if let Some((_, _, name, version)) = self.id.parts() { if name != self.name { return Err(format!( "Inconsistent Schema Id and Schema Name: {:?} and {}", self.id, self.name, ) .into()); } if version != self.version { return Err(format!( "Inconsistent Schema Id and Schema Version: {:?} and {}", self.id, self.version, ) .into()); } } Ok(()) } } impl Validatable for AttributeNames { fn validate(&self) -> Result<(), ValidationError> { if self.0.is_empty() { return Err("Empty list of Schema attributes has been passed".into()); } if self.0.len() > MAX_ATTRIBUTES_COUNT { return Err(format!( "The number of Schema attributes {} cannot be greater than {}", self.0.len(), MAX_ATTRIBUTES_COUNT ) .into()); } Ok(()) } } #[cfg(test)] mod test_schema_validation { use super::*; fn _schema_id_qualified() -> SchemaId { SchemaId("schema:sov:did:sov:NcYxiDXkpYi6ov5FcYDi1e:2:gvt:1.0".to_string()) } #[cfg(feature = "serde")] #[test] fn test_valid_schema() { let schema_json = json!({ "id": _schema_id_qualified(), "name": "gvt", "ver": "1.0", "version": "1.0", "attrNames": ["aaa", "bbb", "ccc"], }) .to_string(); let schema: SchemaV1 = serde_json::from_str(&schema_json).unwrap(); schema.validate().unwrap(); assert_eq!(schema.name, "gvt"); assert_eq!(schema.version, "1.0"); } #[cfg(feature = "serde")] #[test] fn test_invalid_name_schema() { let schema_json = json!({ "id": _schema_id_qualified(), "name": "gvt1", "ver": "1.0", "version": "1.0", "attrNames": ["aaa", "bbb", "ccc"], }) .to_string(); let schema: SchemaV1 = serde_json::from_str(&schema_json).unwrap(); schema.validate().unwrap_err(); } #[cfg(feature = "serde")] #[test] fn test_invalid_version_schema() { let schema_json = json!({ "id": _schema_id_qualified(), "name": "gvt", "ver": "1.0", "version": "1.1", "attrNames": ["aaa", "bbb", "ccc"], }) .to_string(); let schema: SchemaV1 = serde_json::from_str(&schema_json).unwrap(); schema.validate().unwrap_err(); } }