use serde::{Deserialize, Serialize};
use crate::engram::{Engram, TimeBounds};
use crate::manifest::{BlobFetcher, BlobSource};
use crate::schema::{
Hash, ManifestId, ModerationState, PrivacyClass, ProvenanceOrigin, ProvenanceRecord, SchemaRef,
Timestamp, TrustEnvelope, TrustLevel,
};
use crate::typed::{TypedPayload, load_typed, save_typed};
use crate::{Error, Result, Store};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum SchemaFormat {
#[serde(rename = "mere-native")]
MereNative,
#[serde(rename = "json-schema")]
JsonSchema,
#[serde(rename = "json-ld")]
JsonLd,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchemaDefinition {
pub format: SchemaFormat,
pub schema_id: String,
pub body: serde_json::Value,
}
impl TypedPayload for SchemaDefinition {
fn schema_ref() -> SchemaRef {
*META_SCHEMA_REF
}
}
const META_SCHEMA_PAYLOAD: &[u8] = br#"{"format":"mere-native","schema_id":"eidetic.meta-schema/v1","body":{"version":1,"description":"Schema engram payload describing another schema in one of: mere-native, json-schema, json-ld.","required":["format","schema_id","body"],"fields":{"format":{"type":"enum","values":["mere-native","json-schema","json-ld"]},"schema_id":{"type":"string"},"body":{"type":"object"}}}}"#;
pub fn meta_schema_ref() -> SchemaRef {
SchemaRef::from_id(ManifestId::from_hash(Hash::of(META_SCHEMA_PAYLOAD)))
}
pub static META_SCHEMA_REF: std::sync::LazyLock<SchemaRef> =
std::sync::LazyLock::new(meta_schema_ref);
pub async fn bootstrap_meta_schema(store: &mut dyn Store) -> Result<()> {
let meta_id = META_SCHEMA_REF.0;
if crate::manifest::load_manifest(store, meta_id)
.await?
.is_some()
{
return Ok(());
}
let local_key = format!("blob:{}", Hash::of(META_SCHEMA_PAYLOAD).to_hex());
store.put(&local_key, META_SCHEMA_PAYLOAD).await?;
let manifest = crate::manifest::BlobManifest {
id: meta_id,
schema: *META_SCHEMA_REF,
content_hash: Hash::of(META_SCHEMA_PAYLOAD),
byte_size: META_SCHEMA_PAYLOAD.len() as u64,
created_at: Timestamp::ZERO,
last_accessed: None,
sources: vec![crate::manifest::BlobSource::Local { key: local_key }],
privacy: PrivacyClass::PublicPortable,
provenance: ProvenanceRecord {
origin: ProvenanceOrigin::Generated,
upstream: Vec::new(),
tooling: Some(format!("eidetic/{}", env!("CARGO_PKG_VERSION"))),
generated_at: Timestamp::ZERO,
},
trust: TrustEnvelope {
level: TrustLevel::CheckpointAccepted,
signatures: Vec::new(),
moderation_state: ModerationState::Accepted,
},
schema_metadata: serde_json::Value::Null,
manifest_version: crate::manifest::BlobManifest::CURRENT_VERSION,
};
crate::manifest::save_manifest(store, &manifest).await?;
Ok(())
}
pub fn meta_schema_engram() -> Engram {
Engram::new(
*META_SCHEMA_REF,
META_SCHEMA_PAYLOAD.to_vec(),
PrivacyClass::PublicPortable,
ProvenanceRecord {
origin: ProvenanceOrigin::Generated,
upstream: Vec::new(),
tooling: Some(format!("eidetic/{}", env!("CARGO_PKG_VERSION"))),
generated_at: Timestamp::ZERO,
},
TrustEnvelope {
level: TrustLevel::CheckpointAccepted,
signatures: Vec::new(),
moderation_state: ModerationState::Accepted,
},
TimeBounds::at(Timestamp::ZERO),
)
}
pub trait SchemaValidator {
fn format(&self) -> SchemaFormat;
fn validate(&self, definition: &SchemaDefinition, payload_bytes: &[u8]) -> Result<()>;
}
pub fn validate_payload(definition: &SchemaDefinition, payload_bytes: &[u8]) -> Result<()> {
match definition.format {
SchemaFormat::MereNative => MereNativeValidator.validate(definition, payload_bytes),
SchemaFormat::JsonSchema => JsonSchemaValidator.validate(definition, payload_bytes),
SchemaFormat::JsonLd => JsonLdValidator.validate(definition, payload_bytes),
}
}
pub async fn save_schema(
store: &mut dyn Store,
definition: &SchemaDefinition,
privacy: PrivacyClass,
provenance: ProvenanceRecord,
trust: TrustEnvelope,
created_at: Timestamp,
) -> Result<ManifestId> {
save_typed(
store,
definition,
Vec::<BlobSource>::new(),
privacy,
provenance,
trust,
created_at,
)
.await
}
pub async fn load_schema(
store: &mut dyn Store,
fetcher: &mut dyn BlobFetcher,
schema_id: ManifestId,
) -> Result<Option<SchemaDefinition>> {
load_typed::<SchemaDefinition>(store, fetcher, schema_id).await
}
pub async fn validate_against_schema(
store: &mut dyn Store,
fetcher: &mut dyn BlobFetcher,
schema_ref: SchemaRef,
payload_bytes: &[u8],
) -> Result<()> {
let Some(definition) = load_schema(store, fetcher, schema_ref.0).await? else {
return Ok(());
};
validate_payload(&definition, payload_bytes)
}
pub struct MereNativeSchemaBuilder {
schema_id: String,
description: String,
fields: Vec<(String, MereNativeFieldSpec, bool)>,
version: u32,
}
impl MereNativeSchemaBuilder {
pub fn new(schema_id: impl Into<String>) -> Self {
Self {
schema_id: schema_id.into(),
description: String::new(),
fields: Vec::new(),
version: 1,
}
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn version(mut self, version: u32) -> Self {
self.version = version;
self
}
pub fn field(
mut self,
name: impl Into<String>,
spec: MereNativeFieldSpec,
required: bool,
) -> Self {
self.fields.push((name.into(), spec, required));
self
}
pub fn build(self) -> SchemaDefinition {
let mut required = Vec::new();
let mut fields = serde_json::Map::new();
for (name, spec, is_required) in self.fields {
if is_required {
required.push(name.clone());
}
fields.insert(
name,
serde_json::to_value(&spec).expect("field spec serializes"),
);
}
let body = serde_json::json!({
"version": self.version,
"description": self.description,
"required": required,
"fields": fields,
});
SchemaDefinition {
format: SchemaFormat::MereNative,
schema_id: self.schema_id,
body,
}
}
}
pub async fn find_schema_by_id(
store: &mut dyn Store,
fetcher: &mut dyn BlobFetcher,
schema_id: &str,
) -> Result<Option<(ManifestId, SchemaDefinition)>> {
let manifests = crate::manifest::list_manifests(store, Some(*META_SCHEMA_REF)).await?;
for manifest in manifests {
let definition: SchemaDefinition =
match load_typed::<SchemaDefinition>(store, fetcher, manifest.id).await {
Ok(Some(d)) => d,
_ => continue,
};
if definition.schema_id == schema_id {
return Ok(Some((manifest.id, definition)));
}
}
Ok(None)
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct MereNativeSchemaBody {
#[serde(default)]
pub version: u32,
#[serde(default)]
pub description: String,
#[serde(default)]
pub required: Vec<String>,
#[serde(default)]
pub fields: std::collections::BTreeMap<String, MereNativeFieldSpec>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum MereNativeFieldSpec {
String,
U64,
I64,
F64,
Bool,
Object,
Array,
Enum { values: Vec<String> },
}
pub struct MereNativeValidator;
impl SchemaValidator for MereNativeValidator {
fn format(&self) -> SchemaFormat {
SchemaFormat::MereNative
}
fn validate(&self, definition: &SchemaDefinition, payload_bytes: &[u8]) -> Result<()> {
if definition.format != SchemaFormat::MereNative {
return Err(Error::new(format!(
"MereNativeValidator received non-MereNative format: {:?}",
definition.format
)));
}
let body: MereNativeSchemaBody = serde_json::from_value(definition.body.clone())
.map_err(|e| Error::new(format!("mere-native schema body parse: {e}")))?;
let payload_value: serde_json::Value = serde_json::from_slice(payload_bytes)
.map_err(|e| Error::new(format!("payload parse (mere-native): {e}")))?;
let object = payload_value
.as_object()
.ok_or_else(|| Error::new("mere-native payloads must be JSON objects".to_string()))?;
for required in &body.required {
if !object.contains_key(required) {
return Err(Error::new(format!(
"mere-native schema {}: missing required field `{}`",
definition.schema_id, required
)));
}
}
for (name, spec) in &body.fields {
if let Some(value) = object.get(name) {
check_field_spec(&definition.schema_id, name, spec, value)?;
}
}
Ok(())
}
}
fn check_field_spec(
schema_id: &str,
name: &str,
spec: &MereNativeFieldSpec,
value: &serde_json::Value,
) -> Result<()> {
let kind_ok = match spec {
MereNativeFieldSpec::String => value.is_string(),
MereNativeFieldSpec::U64 => value.as_u64().is_some(),
MereNativeFieldSpec::I64 => value.as_i64().is_some(),
MereNativeFieldSpec::F64 => value.as_f64().is_some(),
MereNativeFieldSpec::Bool => value.is_boolean(),
MereNativeFieldSpec::Object => value.is_object(),
MereNativeFieldSpec::Array => value.is_array(),
MereNativeFieldSpec::Enum { values } => match value.as_str() {
Some(s) => values.iter().any(|v| v == s),
None => false,
},
};
if !kind_ok {
return Err(Error::new(format!(
"schema {}: field `{}` failed type check (expected {:?}, got {})",
schema_id, name, spec, value
)));
}
Ok(())
}
mod validators;
pub use validators::{JsonLdValidator, JsonSchemaValidator};
#[cfg(test)]
mod tests;