Rewrite from TypeScript to Rust
This commit is contained in:
parent
a8b8b549d0
commit
25d71ba0f2
29 changed files with 221685 additions and 232325 deletions
32
src/schema.rs
Normal file
32
src/schema.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
use crate::{manifest::Manifest, resource::ResourceFile, Error, REPO};
|
||||
|
||||
pub fn validate_manifest(manifest: &Manifest) -> Result<(), Error> {
|
||||
let manifest: serde_json::Value = serde_yaml::from_str(&manifest.serialize()).unwrap();
|
||||
|
||||
let normal: serde_json::Value = serde_yaml::from_str(&read_data("schema.yaml")).unwrap();
|
||||
let strict: serde_json::Value = serde_yaml::from_str(&read_data("schema.strict.yaml")).unwrap();
|
||||
|
||||
for schema in [normal, strict] {
|
||||
if !check(&schema, &manifest) {
|
||||
return Err(Error::ManifestSchema);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_data(file: &str) -> String {
|
||||
std::fs::read_to_string(format!("{}/data/{}", REPO, file)).unwrap()
|
||||
}
|
||||
|
||||
fn check(schema: &serde_json::Value, instance: &serde_json::Value) -> bool {
|
||||
let mut valid = true;
|
||||
let compiled = jsonschema::JSONSchema::compile(schema).unwrap();
|
||||
if let Err(errors) = compiled.validate(instance) {
|
||||
valid = false;
|
||||
for error in errors {
|
||||
println!("Schema error: {} | {}", error, error.instance_path);
|
||||
}
|
||||
}
|
||||
valid
|
||||
}
|
Reference in a new issue