#[cfg(feature = "schema-validation")]
fn format_validation_error(error: &jsonschema::ValidationError, schema_type: &str) -> String {
let instance_path = error.instance_path();
let path_str = instance_path.to_string();
let path_str = if path_str == "/" || path_str.is_empty() {
"root".to_string()
} else {
path_str
};
let error_message = error.to_string();
format!(
"{} validation failed at path '{}': {}",
schema_type, path_str, error_message
)
}
#[cfg(feature = "schema-validation")]
pub fn validate_odcs_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
let is_odcl_format = if let Some(obj) = data.as_object() {
obj.contains_key("dataContractSpecification")
|| (obj.contains_key("name")
&& obj.contains_key("columns")
&& !obj.contains_key("apiVersion")
&& !obj.contains_key("kind")
&& !obj.contains_key("schema"))
} else {
false
};
if is_odcl_format {
return validate_odcl_internal(content);
}
let schema_content = include_str!("../../../../schemas/odcs-json-schema-v3.1.0.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load ODCS schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile ODCS schema: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "ODCS");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_odcs_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_odcl_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/odcl-json-schema-1.2.1.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load ODCL schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile ODCL schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "ODCL");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_odcl_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_openapi_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/openapi-3.1.1.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load OpenAPI schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile OpenAPI schema: {}", e))?;
let data: Value = if content.trim_start().starts_with('{') {
serde_json::from_str(content).map_err(|e| format!("Failed to parse JSON: {}", e))?
} else {
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?
};
if let Err(error) = validator.validate(&data) {
return Err(format!("OpenAPI validation failed: {}", error));
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_openapi_internal(_content: &str) -> Result<(), String> {
Ok(())
}
pub fn validate_protobuf_internal(content: &str) -> Result<(), String> {
if !content.contains("syntax") && !content.contains("message") && !content.contains("enum") {
return Err("File does not appear to be a valid Protobuf file".to_string());
}
let open_braces = content.matches('{').count();
let close_braces = content.matches('}').count();
if open_braces != close_braces {
return Err(format!(
"Unbalanced braces in Protobuf file ({} open, {} close)",
open_braces, close_braces
));
}
Ok(())
}
pub fn validate_avro_internal(content: &str) -> Result<(), String> {
let _value: serde_json::Value =
serde_json::from_str(content).map_err(|e| format!("Failed to parse AVRO JSON: {}", e))?;
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_json_schema_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema: Value =
serde_json::from_str(content).map_err(|e| format!("Failed to parse JSON Schema: {}", e))?;
Validator::new(&schema).map_err(|e| format!("Invalid JSON Schema: {}", e))?;
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_json_schema_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_odps_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/odps-json-schema-latest.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load ODPS schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile ODPS schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let instance_path = error.instance_path();
let path_str = instance_path.to_string();
let path_str = if path_str == "/" || path_str.is_empty() {
"root".to_string()
} else {
path_str
};
return Err(format!(
"ODPS validation failed at path '{}': {}",
path_str, error
));
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_odps_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_cads_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/cads.schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load CADS schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile CADS schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let instance_path = error.instance_path();
let path_str = instance_path.to_string();
let path_str = if path_str == "/" || path_str.is_empty() {
"root".to_string()
} else {
path_str
};
return Err(format!(
"CADS validation failed at path '{}': {}",
path_str, error
));
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_cads_internal(_content: &str) -> Result<(), String> {
Ok(())
}
pub fn validate_sql_internal(content: &str) -> Result<(), String> {
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;
let dialect = GenericDialect {};
Parser::parse_sql(&dialect, content).map_err(|e| format!("SQL validation failed: {}", e))?;
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_workspace_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/workspace-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load workspace schema: {}", e))?;
let validator = Validator::new(&schema)
.map_err(|e| format!("Failed to compile workspace schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "Workspace");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_workspace_internal(_content: &str) -> Result<(), String> {
Ok(())
}
pub fn validate_relationships_internal(content: &str) -> Result<(), String> {
use serde_json::Value;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
let relationships = data
.get("relationships")
.and_then(|v| v.as_array())
.or_else(|| data.as_array());
if let Some(rels) = relationships {
for (i, rel) in rels.iter().enumerate() {
if rel.get("source_table_id").is_none() {
return Err(format!("Relationship {} is missing 'source_table_id'", i));
}
if rel.get("target_table_id").is_none() {
return Err(format!("Relationship {} is missing 'target_table_id'", i));
}
}
}
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_decision_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/decision-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load decision schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile decision schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let instance_path = error.instance_path();
let path_str = instance_path.to_string();
let path_str = if path_str == "/" || path_str.is_empty() {
"root".to_string()
} else {
path_str
};
return Err(format!(
"Decision validation failed at path '{}': {}",
path_str, error
));
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_decision_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_knowledge_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/knowledge-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load knowledge schema: {}", e))?;
let validator = Validator::new(&schema)
.map_err(|e| format!("Failed to compile knowledge schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let instance_path = error.instance_path();
let path_str = instance_path.to_string();
let path_str = if path_str == "/" || path_str.is_empty() {
"root".to_string()
} else {
path_str
};
return Err(format!(
"Knowledge validation failed at path '{}': {}",
path_str, error
));
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_knowledge_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_decisions_index_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/decisions-index-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load decisions-index schema: {}", e))?;
let validator = Validator::new(&schema)
.map_err(|e| format!("Failed to compile decisions-index schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "Decisions Index");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_decisions_index_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_knowledge_index_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/knowledge-index-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load knowledge-index schema: {}", e))?;
let validator = Validator::new(&schema)
.map_err(|e| format!("Failed to compile knowledge-index schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "Knowledge Index");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_knowledge_index_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_sketch_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/sketch-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load sketch schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile sketch schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let instance_path = error.instance_path();
let path_str = instance_path.to_string();
let path_str = if path_str == "/" || path_str.is_empty() {
"root".to_string()
} else {
path_str
};
return Err(format!(
"Sketch validation failed at path '{}': {}",
path_str, error
));
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_sketch_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_sketch_index_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/sketch-index-schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load sketch-index schema: {}", e))?;
let validator = Validator::new(&schema)
.map_err(|e| format!("Failed to compile sketch-index schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "Sketch Index");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_sketch_index_internal(_content: &str) -> Result<(), String> {
Ok(())
}
#[cfg(feature = "schema-validation")]
pub fn validate_dbmv_internal(content: &str) -> Result<(), String> {
use jsonschema::Validator;
use serde_json::Value;
let schema_content = include_str!("../../../../schemas/dbmv.schema.json");
let schema: Value = serde_json::from_str(schema_content)
.map_err(|e| format!("Failed to load DBMV schema: {}", e))?;
let validator =
Validator::new(&schema).map_err(|e| format!("Failed to compile DBMV schema: {}", e))?;
let data: Value =
serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
if let Err(error) = validator.validate(&data) {
let error_msg = format_validation_error(&error, "DBMV");
return Err(error_msg);
}
Ok(())
}
#[cfg(not(feature = "schema-validation"))]
pub fn validate_dbmv_internal(_content: &str) -> Result<(), String> {
Ok(())
}