use serde_json::Value;
use std::collections::HashSet;
use crate::config::VerifierConfig;
use crate::schema::generate_recipe_schema;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Severity {
Error,
Warning,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationIssue {
pub path: String,
pub message: String,
pub severity: Severity,
}
impl ValidationIssue {
pub fn error(path: impl Into<String>, message: impl Into<String>) -> Self {
Self {
path: path.into(),
message: message.into(),
severity: Severity::Error,
}
}
pub fn warning(path: impl Into<String>, message: impl Into<String>) -> Self {
Self {
path: path.into(),
message: message.into(),
severity: Severity::Warning,
}
}
}
pub fn has_errors(issues: &[ValidationIssue]) -> bool {
issues.iter().any(|i| i.severity == Severity::Error)
}
pub fn validate_recipe_value(data: &Value, config: &VerifierConfig) -> Vec<ValidationIssue> {
let mut issues = Vec::new();
validate_recipe_version(data, &mut issues);
validate_via_schema(data, &mut issues);
validate_legacy_int_fields(data, &mut issues);
validate_plugin_names(data, config, &mut issues);
issues
}
pub fn validate_recipe(
recipe: &crate::recipe::Recipe,
config: &VerifierConfig,
) -> Vec<ValidationIssue> {
let value = serde_json::to_value(recipe).expect("recipe serializes");
validate_recipe_value(&value, config)
}
fn validate_recipe_version(data: &Value, issues: &mut Vec<ValidationIssue>) {
match data.get("recipe_version") {
None => {
issues.push(ValidationIssue::warning(
"recipe_version",
"missing recipe_version; treating recipe as legacy v0.x",
));
}
Some(Value::Number(n)) if n.as_u64() == Some(1) => {}
Some(Value::Bool(_)) | Some(_) => {
if let Some(Value::Bool(_)) = data.get("recipe_version") {
issues.push(ValidationIssue::error("recipe_version", "must be 1"));
} else if !matches!(data.get("recipe_version"), Some(Value::Number(n)) if n.as_u64() == Some(1))
{
issues.push(ValidationIssue::error("recipe_version", "must be 1"));
}
}
}
}
fn validate_via_schema(data: &Value, issues: &mut Vec<ValidationIssue>) {
let schema = generate_recipe_schema();
let validator = match jsonschema::validator_for(&schema) {
Ok(v) => v,
Err(e) => {
issues.push(ValidationIssue::error("$", format!("invalid schema: {e}")));
return;
}
};
for error in validator.iter_errors(data) {
let instance_path = error.instance_path().to_string();
if instance_path == "/recipe_version" {
continue;
}
if is_legacy_tolerated(data, &error) {
continue;
}
if is_legacy_int_owned(&error) {
continue;
}
let path = format_instance_path(&error);
issues.push(ValidationIssue::error(path, error.to_string()));
}
}
fn format_instance_path(error: &jsonschema::ValidationError<'_>) -> String {
let path = error.instance_path().to_string();
if path.is_empty() {
let msg = error.to_string();
if let Some(prop) = extract_required_property(&msg) {
return prop;
}
return "$".to_string();
}
let parts: Vec<&str> = path.trim_start_matches('/').split('/').collect();
let mut out = String::new();
for (i, part) in parts.iter().enumerate() {
let decoded = part.replace("~1", "/").replace("~0", "~");
let is_index = decoded.chars().all(|c| c.is_ascii_digit()) && !decoded.is_empty();
if is_index {
out.push_str(&format!("[{decoded}]"));
} else if i == 0 {
out.push_str(&decoded);
} else {
out.push('.');
out.push_str(&decoded);
}
}
if out.is_empty() {
"$".to_string()
} else {
out
}
}
fn extract_required_property(message: &str) -> Option<String> {
if let Some(start) = message.find('\'') {
if let Some(end) = message[start + 1..].find('\'') {
let prop = &message[start + 1..start + 1 + end];
if message.contains("required") {
return Some(prop.to_string());
}
}
}
if let Some(start) = message.find('`') {
if let Some(end) = message[start + 1..].find('`') {
let prop = &message[start + 1..start + 1 + end];
if message.contains("required") {
return Some(prop.to_string());
}
}
}
None
}
fn is_legacy_tolerated(data: &Value, error: &jsonschema::ValidationError<'_>) -> bool {
let path_str = error.instance_path().to_string();
if path_str.is_empty() {
return false;
}
if path_str == "/description" || path_str == "/intent" {
return true;
}
if (path_str.starts_with("/steps/") || path_str.starts_with("/assertions/"))
&& path_str.ends_with("/name")
{
let parts: Vec<&str> = path_str.split('/').collect();
if parts.len() == 4 {
return true;
}
}
let null_tolerant_top: HashSet<&str> = [
"priority",
"execution",
"determinism",
"timeout_seconds",
"cols",
"rows",
"checks",
"ci_paths",
"operator",
"renderers",
"description",
"intent",
]
.into_iter()
.collect();
let null_tolerant_command: HashSet<&str> = ["cwd", "pty"].into_iter().collect();
let null_tolerant_step: HashSet<&str> = ["timeout_seconds"].into_iter().collect();
let value_at = pointer_get(data, &path_str);
if value_at != Some(&Value::Null) {
return false;
}
if path_str.matches('/').count() == 1 {
let key = path_str.trim_start_matches('/');
return null_tolerant_top.contains(key);
}
if path_str.starts_with("/command/") && path_str.matches('/').count() == 2 {
let key = path_str.trim_start_matches("/command/");
return null_tolerant_command.contains(key);
}
if path_str.starts_with("/steps/") {
let parts: Vec<&str> = path_str.split('/').collect();
if parts.len() == 4
&& parts[1].chars().all(|c| c.is_ascii_digit())
&& parts[3] == "timeout_seconds"
{
return null_tolerant_step.contains("timeout_seconds");
}
}
false
}
fn pointer_get<'a>(data: &'a Value, pointer: &str) -> Option<&'a Value> {
data.pointer(pointer)
}
fn is_legacy_int_owned(error: &jsonschema::ValidationError<'_>) -> bool {
matches!(
error.instance_path().to_string().as_str(),
"/cols" | "/rows" | "/expect_exit_code"
)
}
fn validate_legacy_int_fields(data: &Value, issues: &mut Vec<ValidationIssue>) {
for key in ["cols", "rows"] {
if let Some(v) = data.get(key) {
if !is_valid_positive_int(v) {
if *v != Value::Null {
issues.push(ValidationIssue::error(key, "must be a positive integer"));
}
}
}
}
if let Some(v) = data.get("expect_exit_code") {
if *v != Value::Null && !is_valid_int(v) {
issues.push(ValidationIssue::error(
"expect_exit_code",
"must be an integer or null",
));
}
}
}
fn is_valid_positive_int(v: &Value) -> bool {
match v {
Value::Number(n) => {
if let Some(i) = n.as_u64() {
i > 0
} else if let Some(i) = n.as_i64() {
i > 0
} else if let Some(f) = n.as_f64() {
let _ = f;
false
} else {
false
}
}
Value::Null => true, _ => false,
}
}
fn is_valid_int(v: &Value) -> bool {
match v {
Value::Number(n) => n.is_u64() || n.is_i64(),
_ => false,
}
}
fn validate_plugin_names(data: &Value, config: &VerifierConfig, issues: &mut Vec<ValidationIssue>) {
if let Some(Value::Array(steps)) = data.get("steps") {
for (i, step) in steps.iter().enumerate() {
if let Some(Value::Object(map)) = step.as_object().map(|_| step) {
if let Some(Value::String(action)) = map.get("action") {
if !config.steps.contains_key(action) {
issues.push(ValidationIssue::error(
format!("steps[{i}].action"),
format!("unknown step action {action:?}"),
));
}
}
}
}
}
if let Some(Value::Array(assertions)) = data.get("assertions") {
for (i, assertion) in assertions.iter().enumerate() {
if let Some(Value::Object(map)) = assertion.as_object().map(|_| assertion) {
if let Some(Value::String(kind)) = map.get("type") {
if !config.assertions.contains_key(kind) {
issues.push(ValidationIssue::error(
format!("assertions[{i}].type"),
format!("unknown assertion type {kind:?}"),
));
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn cfg() -> VerifierConfig {
VerifierConfig::builtin()
}
#[test]
fn valid_recipe_has_no_errors() {
let data = json!({
"recipe_version": 1,
"name": "valid",
"command": {"argv": ["echo", "hi"]},
});
let issues = validate_recipe_value(&data, &cfg());
assert!(!has_errors(&issues));
}
#[test]
fn missing_version_is_warning_only() {
let data = json!({
"name": "x",
"command": {"argv": ["true"]},
});
let issues = validate_recipe_value(&data, &cfg());
assert!(!has_errors(&issues));
assert!(issues.iter().any(|i| i.severity == Severity::Warning));
}
#[test]
fn unknown_step_is_error() {
let data = json!({
"recipe_version": 1,
"name": "x",
"command": {"argv": ["true"]},
"steps": [{"action": "nope"}]
});
let issues = validate_recipe_value(&data, &cfg());
assert!(has_errors(&issues));
}
#[test]
fn integral_float_cols_is_error() {
let data = json!({
"recipe_version": 1,
"name": "x",
"command": {"argv": ["true"]},
"cols": 1.0
});
let issues = validate_recipe_value(&data, &cfg());
assert!(has_errors(&issues));
assert!(issues.iter().any(|i| i.path == "cols"));
}
#[test]
fn null_description_is_tolerated() {
let data = json!({
"recipe_version": 1,
"name": "x",
"command": {"argv": ["true"]},
"description": null
});
let issues = validate_recipe_value(&data, &cfg());
assert!(!has_errors(&issues));
}
}