feat: support inline json schema validation dynamically

This commit is contained in:
2026-05-26 16:47:04 -04:00
parent 2c18163961
commit 0091e5c226

View File

@ -42,13 +42,46 @@ impl Validator {
} }
pub fn validate(&self, schema_id: &str, instance: &Value) -> crate::drop::Drop { pub fn validate(&self, schema_id: &str, instance: &Value) -> crate::drop::Drop {
let schema_opt = self.db.schemas.get(schema_id); let schema_arc = if schema_id.trim().starts_with('{') {
match serde_json::from_str::<crate::database::schema::Schema>(schema_id) {
Ok(schema) => {
let mut errors = Vec::new();
schema.compile(&self.db, "inline", "inline".to_string(), &mut errors);
if !errors.is_empty() {
return crate::drop::Drop::with_errors(errors);
}
Arc::new(schema)
}
Err(e) => {
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_PARSE_FAILED".to_string(),
message: format!("Failed to parse inline schema: {}", e),
details: crate::drop::ErrorDetails::default(),
}]);
}
}
} else {
match self.db.schemas.get(schema_id) {
Some(schema) => Arc::clone(schema),
None => {
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_NOT_FOUND".to_string(),
message: format!("Schema {} not found", schema_id),
details: crate::drop::ErrorDetails {
path: Some("/".to_string()),
cause: None,
context: None,
schema: None,
},
}]);
}
}
};
if let Some(schema) = schema_opt {
let ctx = ValidationContext::new( let ctx = ValidationContext::new(
&self.db, &self.db,
&schema, &schema_arc,
&schema, &schema_arc,
instance, instance,
HashSet::new(), HashSet::new(),
false, false,
@ -87,17 +120,5 @@ impl Validator {
}, },
}]), }]),
} }
} else {
crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_NOT_FOUND".to_string(),
message: format!("Schema {} not found", schema_id),
details: crate::drop::ErrorDetails {
path: Some("/".to_string()),
cause: None,
context: None,
schema: None,
},
}])
}
} }
} }