use crate::engine::StepKindSpec;
pub fn json_schema(kinds: &[StepKindSpec]) -> serde_json::Value {
let schema = schemars::schema_for!(super::RawPack);
let mut root = serde_json::to_value(&schema).unwrap_or(serde_json::Value::Bool(true));
if let Some(step_properties) = root
.pointer_mut("/$defs/RawStep/properties")
.and_then(serde_json::Value::as_object_mut)
{
for kind in kinds {
let fragment: serde_json::Value =
serde_json::from_str(kind.schema).unwrap_or(serde_json::Value::Bool(true));
step_properties.insert(kind.prefix.to_owned(), fragment);
}
}
root
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn engine_fragments_merge_into_the_step_schema() {
let kinds = [StepKindSpec {
prefix: "hurl",
schema: r#"{ "type": "string" }"#,
validate: None,
fragments: None,
options: None,
}];
let schema = json_schema(&kinds);
assert_eq!(
schema.pointer("/$defs/RawStep/properties/hurl/type"),
Some(&serde_json::Value::String("string".into()))
);
assert!(schema.pointer("/$defs/RawMacro/properties/match").is_some());
}
}