Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add release schema validation #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// build.rs

use serde_json::{map::Map, Value};
use std::{env, error::Error, fs::File, path::Path};
use std::{env, error::Error, fs::File, io::Write, path::Path};
use wax::Glob;

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -13,29 +13,23 @@ fn main() -> Result<(), Box<dyn Error>> {
}

fn merge_version(version: u8) -> Result<(), Box<dyn Error>> {
// Open the file to write each schema to.
let out_dir = env::var_os("OUT_DIR").unwrap();
let dst_file = format!("pgxn-meta-v{version}.schemas.json");
let dst_path = Path::new(&out_dir).join(&dst_file);
let file = File::create(dst_path)?;

// Set up the search directory.
let src_dir = Path::new("schema").join(format!("v{version}"));
let glob = Glob::new("*.schema.json")?;
let mut defs = serde_json::map::Map::new();

// Write each file to the destination.
for path in glob.walk(src_dir) {
let path = &path?.into_path();
let schema: Map<String, Value> = serde_json::from_reader(File::open(path)?)?;
// let id = schema["$id"].as_str().ok_or("No $id found in {path}")?;
let id = path.file_name().unwrap().to_str().unwrap();
defs.insert(id.to_string(), Value::Object(schema));
serde_json::to_writer(&file, &schema)?;
writeln!(&file)?;
}

const ROOT: &str = "distribution.schema.json";
let mut dist = defs.remove(ROOT).unwrap();
dist.as_object_mut()
.unwrap()
.insert("$defs".to_string(), Value::Object(defs));

let out_dir = env::var_os("OUT_DIR").unwrap();
let dst_file = format!("pgxn-meta-v{version}.schema.json");
let dst_path = Path::new(&out_dir).join(&dst_file);
let file = File::create(dst_path)?;
serde_json::to_writer(&file, &dist)?;

Ok(())
}
18 changes: 10 additions & 8 deletions src/valid/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ use serde_json::Value;
/// new returns a new boon::Compiler with the schema files loaded from `dir`
/// and configured to validate `path` and `license` formats.
pub fn new() -> Compiler {
let schema_v1 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v1.schema.json"));
let schema_v2 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v2.schema.json"));
let schema_v1 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v1.schemas.json"));
let schema_v2 = include_str!(concat!(env!("OUT_DIR"), "/pgxn-meta-v2.schemas.json"));
let mut compiler = spec_compiler();

for str in [schema_v1, schema_v2] {
let schema: Value = serde_json::from_str(str).unwrap();
let id = &schema["$id"]
.as_str()
.ok_or(super::ValidationError::UnknownID)
.unwrap();
compiler.add_resource(id, schema.to_owned()).unwrap();
for line in str.lines() {
let schema: Value = serde_json::from_str(line).unwrap();
let id = &schema["$id"]
.as_str()
.ok_or(super::ValidationError::UnknownID)
.unwrap();
compiler.add_resource(id, schema.to_owned()).unwrap();
}
}

compiler
Expand Down
Loading
Loading