container_device_interface/schema/
mod.rs

1use anyhow::Ok;
2// use core::panic;
3// use jsonschema::Draft;
4// use jsonschema::Validator;
5// use serde_json::json;
6// use serde_json::Value;
7
8use anyhow::Result;
9
10const _SCHEMA_JSON: &str = include_str!("schema.json");
11const _DEFS_JSON: &str = include_str!("defs.json");
12
13pub fn validate(_schema: &jsonschema::Validator, _doc_data: &[u8]) -> Result<()> {
14    let mut schema_json: serde_json::Value = serde_json::from_str(include_str!("schema.json"))?;
15    let defs_json: serde_json::Value = serde_json::from_str(include_str!("defs.json"))?;
16
17    // Merge the definitions into the main schema under the "definitions" key
18    if let Some(obj) = schema_json.as_object_mut() {
19        obj.insert("definitions".to_string(), defs_json);
20    }
21    /*
22        let compiled_schema = Validator::options()
23            .with_draft(Draft::Draft7) // Adjust the draft version as needed
24            .compile(&schema_json)?;
25
26        let doc = &serde_json::from_slice(doc_data)?;
27
28        let result = compiled_schema.validate(doc);
29
30    */
31
32    Ok(())
33}
34
35/*
36fn validate_data(schema: &Value, data: &Value) -> Result<(), Vec<jsonschema::ValidationError>> {
37    let compiled_schema = Validator::options()
38        .with_draft(Draft::Draft7) // Adjust the draft version as needed
39        .compile(schema)?;
40
41    compiled_schema.validate(data).map_err(|e| e.collect())
42}
43
44
45
46pub fn load(schema_file: &str) -> Result<jsonschema::Validator> {
47
48    let schema_context = SchemaContext::builtin()?;
49    Ok(schema_context.compiled_schema)
50    /*
51    if schema_file == "builtin" {
52        println!("Loading schema from {}...", schema_file);
53
54        print!("schema:\n{}", builtin_schema);
55
56        match jsonschema::Validator::compile(&serde_json::from_str(&builtin_schema)?) {
57            Ok(schema) => return Ok(schema),
58            Err(e) => return Err(anyhow!("failed to compile builtin schema {}", e)),
59        }
60    }
61    */
62    //panic!("not implemented yet loading from other sources")
63}
64
65
66 pub fn validate(schema: &jsonschema::Validator, doc_data: &[u8]) -> Result<()> {
67    let doc = serde_json::from_slice(doc_data)?;
68    match schema.validate(&doc) {
69        Ok(_) => (),
70        Err(_e) => return Err(anyhow!("validation failed")),
71    }
72    Ok(())
73    }
74    */