peprs-eido 0.2.0

JSON-schema validation for PEP projects (eido extensions)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::path::Path;

use peprs_core::project::Project;
use peprs_core::utils::any_value_to_json;
use polars_jsonschema_bridge::schema_to_polars_fields;
use serde_json::Value;
use tracing::warn;

use crate::error::{EidoError, MissingFile, Result, ValidationError};
use crate::schema::EidoSchema;

/// Validate samples against the schema using a structural pre-check (polars-jsonschema-bridge)
/// followed by per-sample JSON Schema validation.
pub fn validate_samples(project: &Project, schema: &EidoSchema) -> Result<()> {
    let mut errors = Vec::new();

    // Validate against imported schemas first
    for import in &schema.imports {
        if let Err(EidoError::Validation(import_errors)) = validate_samples(project, import) {
            errors.extend(import_errors);
        }
    }

    let Some(sample_schema) = &schema.sample_schema else {
        return if errors.is_empty() {
            Ok(())
        } else {
            Err(EidoError::Validation(errors))
        };
    };

    // Strategy B: Structural pre-check via polars-jsonschema-bridge
    if let Err(structural_errors) = structural_precheck(project, sample_schema) {
        errors.extend(structural_errors);
    }

    // Strategy A: Per-sample JSON Schema validation
    let validator = jsonschema::validator_for(sample_schema)
        .map_err(|e| EidoError::SchemaCompile(format!("Failed to compile sample schema: {e}")))?;

    // Use Project.to_json_string() to bulk-convert samples to JSON
    let json_str = project
        .to_json_string()
        .map_err(|e| EidoError::Project(e))?;
    let samples_json: Vec<Value> = serde_json::from_str(&json_str)?;

    let sample_index = &project.sample_table_index;

    for sample_value in &samples_json {
        let sample_name = sample_value
            .get(sample_index)
            .and_then(|v| v.as_str())
            .unwrap_or("<unknown>")
            .to_string();

        for error in validator.iter_errors(sample_value) {
            errors.push(ValidationError {
                path: error.instance_path.to_string(),
                message: format_schema_error(&error, sample_schema),
                sample_name: Some(sample_name.clone()),
            });
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(EidoError::Validation(errors))
    }
}

/// Validate project-level config against the schema.
pub fn validate_project(project: &Project, schema: &EidoSchema) -> Result<()> {
    let mut errors = Vec::new();

    // Validate against imported schemas first
    for import in &schema.imports {
        if let Err(EidoError::Validation(import_errors)) = validate_project(project, import) {
            errors.extend(import_errors);
        }
    }

    let Some(project_schema) = &schema.project_schema else {
        return if errors.is_empty() {
            Ok(())
        } else {
            Err(EidoError::Validation(errors))
        };
    };

    // Use ProjectConfig.raw directly — already a serde_json::Value
    let config_value = match &project.config {
        Some(cfg) => match &cfg.raw {
            Some(raw) => raw.clone(),
            None => Value::Object(serde_json::Map::new()),
        },
        None => Value::Object(serde_json::Map::new()),
    };

    let validator = jsonschema::validator_for(project_schema)
        .map_err(|e| EidoError::SchemaCompile(format!("Failed to compile project schema: {e}")))?;

    for error in validator.iter_errors(&config_value) {
        errors.push(ValidationError {
            path: error.instance_path.to_string(),
            message: format_schema_error(&error, project_schema),
            sample_name: None,
        });
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(EidoError::Validation(errors))
    }
}

/// Validate that tangible file attributes point to existing files.
pub fn validate_input_files(project: &Project, schema: &EidoSchema) -> Result<()> {
    if schema.tangible.is_empty() {
        return Ok(());
    }

    let mut missing = Vec::new();
    let sample_index = &project.sample_table_index;

    // Use iter_samples() + any_value_to_json for per-sample file path checking
    for sample in project.iter_samples() {
        let sample_name = sample
            .get(sample_index)
            .map(|v| any_value_to_json(v.clone()))
            .and_then(|v| v.as_str().map(String::from))
            .unwrap_or_else(|| "<unknown>".to_string());

        for attr in &schema.tangible {
            let Some(value) = sample.get(attr) else {
                missing.push(MissingFile {
                    sample_name: sample_name.clone(),
                    attribute: attr.clone(),
                    path: "<attribute not found>".to_string(),
                });
                continue;
            };

            let json_val = any_value_to_json(value.clone());
            let paths: Vec<&str> = match &json_val {
                Value::String(s) => vec![s.as_str()],
                Value::Array(arr) => arr.iter().filter_map(|v| v.as_str()).collect(),
                _ => continue,
            };

            for p in paths {
                if p.is_empty() || p == "null" {
                    missing.push(MissingFile {
                        sample_name: sample_name.clone(),
                        attribute: attr.clone(),
                        path: "<empty>".to_string(),
                    });
                } else if !Path::new(p).exists() {
                    missing.push(MissingFile {
                        sample_name: sample_name.clone(),
                        attribute: attr.clone(),
                        path: p.to_string(),
                    });
                }
            }
        }
    }

    // Check optional files — just warn, don't error
    for sample in project.iter_samples() {
        let sample_name = sample
            .get(sample_index)
            .map(|v| any_value_to_json(v.clone()))
            .and_then(|v| v.as_str().map(String::from))
            .unwrap_or_else(|| "<unknown>".to_string());

        for attr in &schema.files {
            // Skip if also in tangible (already checked)
            if schema.tangible.contains(attr) {
                continue;
            }
            if let Some(value) = sample.get(attr) {
                let json_val = any_value_to_json(value.clone());
                let paths: Vec<&str> = match &json_val {
                    Value::String(s) => vec![s.as_str()],
                    Value::Array(arr) => arr.iter().filter_map(|v| v.as_str()).collect(),
                    _ => continue,
                };
                for p in paths {
                    if !p.is_empty() && p != "null" && !Path::new(p).exists() {
                        warn!(
                            sample = sample_name,
                            attribute = attr,
                            path = p,
                            "Optional file attribute points to non-existent file"
                        );
                    }
                }
            }
        }
    }

    if missing.is_empty() {
        Ok(())
    } else {
        Err(EidoError::MissingFiles(missing))
    }
}

/// Validate a single sample (as a JSON value) against the schema's sample schema.
pub fn validate_single_sample(
    sample: &Value,
    schema: &EidoSchema,
    sample_name: &str,
) -> Result<()> {
    let mut errors = Vec::new();

    // Validate against imported schemas first
    for import in &schema.imports {
        if let Err(EidoError::Validation(import_errors)) =
            validate_single_sample(sample, import, sample_name)
        {
            errors.extend(import_errors);
        }
    }

    let Some(sample_schema) = &schema.sample_schema else {
        return if errors.is_empty() {
            Ok(())
        } else {
            Err(EidoError::Validation(errors))
        };
    };

    let validator = jsonschema::validator_for(sample_schema)
        .map_err(|e| EidoError::SchemaCompile(format!("Failed to compile sample schema: {e}")))?;

    for error in validator.iter_errors(sample) {
        errors.push(ValidationError {
            path: error.instance_path.to_string(),
            message: format_schema_error(&error, sample_schema),
            sample_name: Some(sample_name.to_string()),
        });
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(EidoError::Validation(errors))
    }
}

/// Format a jsonschema validation error with improved messages for `anyOf` failures.
///
/// When a value fails `anyOf` validation (common with Pydantic `Optional[T]` schemas),
/// the default message is unhelpful ("is not valid under any of the schemas listed in the
/// 'anyOf' keyword").  This function navigates the schema to extract the expected types
/// and shows them alongside the actual type of the failing value.
fn format_schema_error(error: &jsonschema::ValidationError, schema: &Value) -> String {
    use jsonschema::error::ValidationErrorKind;

    if !matches!(error.kind, ValidationErrorKind::AnyOf) {
        return error.to_string();
    }

    // schema_path points to e.g. "/properties/protocol/anyOf" — navigate to the parent
    // property node to find the anyOf array.
    let schema_path = error.schema_path.to_string();
    if let Some(any_of_node) = navigate_json_pointer(schema, &schema_path) {
        if let Some(variants) = any_of_node.as_array() {
            let expected: Vec<&str> = variants
                .iter()
                .filter_map(|v| v.get("type").and_then(|t| t.as_str()))
                .collect();
            if !expected.is_empty() {
                let actual = json_type_name(&error.instance);
                let field = error.instance_path.to_string();
                let field_label = if field.is_empty() {
                    String::new()
                } else {
                    format!(" at '{field}'")
                };
                return format!(
                    "type mismatch{field_label}: got {actual}, expected {}",
                    expected.join(" or ")
                );
            }
        }
    }

    // Fallback to default message if we can't extract better info
    error.to_string()
}

/// Return the JSON type name of a serde_json::Value.
fn json_type_name(value: &Value) -> &'static str {
    match value {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(n) => {
            if n.is_f64() && n.as_i64().is_none() {
                "number"
            } else {
                "integer"
            }
        }
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}

/// Navigate a JSON value by a JSON Pointer string (e.g. "/properties/protocol/anyOf").
fn navigate_json_pointer<'a>(root: &'a Value, pointer: &str) -> Option<&'a Value> {
    let segments: Vec<&str> = pointer.split('/').filter(|s| !s.is_empty()).collect();
    let mut current = root;
    for segment in &segments {
        current = current.get(*segment)?;
    }
    Some(current)
}

/// Structural pre-check: compare DataFrame schema against JSON Schema using polars-jsonschema-bridge.
fn structural_precheck(
    project: &Project,
    sample_schema: &Value,
) -> std::result::Result<(), Vec<ValidationError>> {
    let Some(properties) = sample_schema.get("properties") else {
        return Ok(());
    };

    // Build a minimal JSON Schema object to pass to schema_to_polars_fields
    let schema_for_bridge = serde_json::json!({
        "type": "object",
        "properties": unwrap_any_of_properties(properties),
    });

    let expected_fields = match schema_to_polars_fields(
        &schema_for_bridge,
        polars_jsonschema_bridge::SchemaFormat::JsonSchema,
        false,
    ) {
        Ok(fields) => fields,
        Err(e) => {
            // If the bridge can't parse it, skip structural check — per-sample validation
            // will still catch issues.
            warn!(error = %e, "polars-jsonschema-bridge could not parse schema, skipping structural pre-check");
            return Ok(());
        }
    };

    let df_schema = project.samples.schema();
    let mut errors = Vec::new();

    // Check required columns exist
    if let Some(required) = sample_schema.get("required").and_then(|r| r.as_array()) {
        for req in required {
            if let Some(col_name) = req.as_str() {
                if df_schema.get(col_name).is_none() {
                    errors.push(ValidationError {
                        path: format!("/properties/{col_name}"),
                        message: format!(
                            "Required column '{col_name}' is missing from sample table"
                        ),
                        sample_name: None,
                    });
                }
            }
        }
    }

    // Check type compatibility for columns that exist in both
    // expected_fields is Vec<(field_name: String, dtype_string: String)>
    for (field_name, expected_dtype_str) in &expected_fields {
        if let Some(df_dtype) = df_schema.get(field_name.as_str()) {
            if !dtype_str_compatible(df_dtype, expected_dtype_str) {
                errors.push(ValidationError {
                    path: format!("/properties/{field_name}"),
                    message: format!(
                        "Column '{field_name}' has type {:?} but schema expects {expected_dtype_str}",
                        df_dtype,
                    ),
                    sample_name: None,
                });
            }
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

/// Unwrap anyOf wrappers we added during preprocessing so the bridge sees plain types.
fn unwrap_any_of_properties(properties: &Value) -> Value {
    let Some(obj) = properties.as_object() else {
        return properties.clone();
    };

    let mut result = serde_json::Map::new();
    for (key, value) in obj {
        if let Some(any_of) = value.get("anyOf").and_then(|a| a.as_array()) {
            // Take the first variant (the scalar one)
            if let Some(first) = any_of.first() {
                result.insert(key.clone(), first.clone());
                continue;
            }
        }
        result.insert(key.clone(), value.clone());
    }
    Value::Object(result)
}

/// Check if a Polars DataType is compatible with an expected type string from polars-jsonschema-bridge.
/// The bridge returns strings like "Int64", "Float64", "String", "Boolean", etc.
/// We're lenient: e.g., any integer type is compatible with "Int64", String is always compatible.
fn dtype_str_compatible(actual: &polars::prelude::DataType, expected_str: &str) -> bool {
    use polars::prelude::DataType;

    let actual_str = format!("{actual:?}");
    if actual_str == expected_str {
        return true;
    }

    // String is always compatible (CSV data is often all strings)
    if matches!(actual, DataType::String) || expected_str == "String" {
        return true;
    }

    // List types: Polars Debug prints List(T) but the bridge returns List[T]
    if let DataType::List(inner) = actual {
        if let Some(inner_expected) = expected_str
            .strip_prefix("List[")
            .and_then(|s| s.strip_suffix(']'))
        {
            return dtype_str_compatible(inner, inner_expected);
        }
    }

    let is_actual_int = matches!(
        actual,
        DataType::Int8
            | DataType::Int16
            | DataType::Int32
            | DataType::Int64
            | DataType::UInt8
            | DataType::UInt16
            | DataType::UInt32
            | DataType::UInt64
    );
    let is_actual_float = matches!(actual, DataType::Float32 | DataType::Float64);
    let is_expected_int = matches!(
        expected_str,
        "Int8" | "Int16" | "Int32" | "Int64" | "UInt8" | "UInt16" | "UInt32" | "UInt64"
    );
    let is_expected_float = matches!(expected_str, "Float32" | "Float64");

    // All numeric types are compatible with each other
    if (is_actual_int || is_actual_float) && (is_expected_int || is_expected_float) {
        return true;
    }

    false
}