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
//! OpenAPI schema validation and handling
//!
//! This module provides functionality for working with OpenAPI schemas,
//! including validation, type checking, and schema manipulation.
use jsonschema::{self, Draft};
use mockforge_foundation::error::{Error, Result};
use openapiv3::Schema;
use serde_json::Value;
/// OpenAPI schema wrapper with additional functionality
#[derive(Debug, Clone)]
pub struct OpenApiSchema {
/// The underlying OpenAPI schema
pub schema: Schema,
}
impl OpenApiSchema {
/// Create a new OpenApiSchema from an OpenAPI Schema
pub fn new(schema: Schema) -> Self {
Self { schema }
}
/// Validate a value against this schema
pub fn validate(&self, value: &Value) -> Result<()> {
// Convert OpenAPI schema to JSON Schema for validation
match serde_json::to_value(&self.schema) {
Ok(schema_json) => {
// Create JSON Schema validator
match jsonschema::options().with_draft(Draft::Draft7).build(&schema_json) {
Ok(validator) => {
// Validate the value against the schema
let errors: Vec<String> =
validator.iter_errors(value).map(|error| error.to_string()).collect();
if errors.is_empty() {
Ok(())
} else {
Err(Error::validation(errors.join("; ")))
}
}
Err(e) => {
Err(Error::validation(format!("Failed to create schema validator: {}", e)))
}
}
}
Err(e) => {
Err(Error::validation(format!("Failed to convert OpenAPI schema to JSON: {}", e)))
}
}
}
}
/// Schema validation result
#[derive(Debug, Clone)]
pub struct ValidationResult {
/// Whether validation passed
pub valid: bool,
/// Validation errors if any
pub errors: Vec<String>,
}
impl ValidationResult {
/// Create a successful validation result
pub fn valid() -> Self {
Self {
valid: true,
errors: Vec::new(),
}
}
/// Create a failed validation result with errors
pub fn invalid(errors: Vec<String>) -> Self {
Self {
valid: false,
errors,
}
}
}