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
//! # JSON Schema Validation Module
//!
//! Provides custom validation functions for JSON Schema validation in the
//! ZAI-RS model API. This module ensures that JSON schemas used in function
//! definitions and tool configurations are valid and conform to the JSON Schema
//! specification.
//!
//! ## Validation Functions
//!
//! - [`validate_json_schema`] - Validates JSON Schema from string input
//! - [`validate_json_schema_value`] - Validates JSON Schema from parsed JSON
//! value
//!
//! ## Error Handling
//!
//! Both validation functions return `ValidationError` with specific error
//! codes:
//! - `"invalid_json"` - Input string is not valid JSON
//! - `"invalid_json_schema"` - JSON is valid but not a valid JSON Schema
//!
//! ## Usage Examples
//!
//! ```rust,ignore
//! use zai_rs::model::model_validate::*;
//! use validator::Validate;
//!
//! // Validate from string
//! let schema_str = r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#;
//! assert!(validate_json_schema(schema_str).is_ok());
//!
//! // Validate from parsed JSON
//! let json_value = serde_json::json!({
//! "type": "object",
//! "properties": {
//! "name": {"type": "string"}
//! }
//! });
//! assert!(validate_json_schema_value(&json_value).is_ok());
//! ```
//!
//! ## JSON Schema Requirements
//!
//! Valid JSON Schemas must:
//! - Be valid JSON syntax
//! - Conform to JSON Schema meta-schema validation
//! - Have appropriate schema structure for function parameters
//!
//! ## Integration with Validation
//!
//! These functions are designed to work with the `validator` crate's custom
//! validation system, allowing them to be used as field validators in struct
//! definitions.
use ValidationError;
/// Validates a JSON Schema from a string input.
///
/// This function parses the input string as JSON and then validates that it
/// conforms to the JSON Schema specification using the `jsonschema` crate's
/// meta-validation.
///
/// # Arguments
///
/// * `parameters` - A string containing JSON that should represent a valid JSON
/// Schema
///
/// # Returns
///
/// * `Ok(())` - If the input is valid JSON and a valid JSON Schema
/// * `Err(ValidationError)` - If the input is invalid JSON or not a valid JSON
/// Schema
///
/// # Error Codes
///
/// * `"invalid_json"` - The input string is not valid JSON
/// * `"invalid_json_schema"` - The JSON is valid but not a valid JSON Schema
///
/// # Examples
///
/// ```rust,ignore
/// // Valid JSON Schema
/// let valid_schema = r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#;
/// assert!(validate_json_schema(valid_schema).is_ok());
///
/// // Invalid JSON
/// let invalid_json = "{ invalid json }";
/// assert!(validate_json_schema(invalid_json).is_err());
///
/// // Invalid JSON Schema
/// let invalid_schema = r#"{"type": "invalid_type"}"#;
/// assert!(validate_json_schema(invalid_schema).is_err());
/// ```
/// Validates a JSON Schema from a parsed JSON value.
///
/// This function validates that the provided JSON value conforms to the JSON
/// Schema specification using the `jsonschema` crate's meta-validation.
///
/// # Arguments
///
/// * `parameters` - A reference to a `serde_json::Value` that should represent
/// a valid JSON Schema
///
/// # Returns
///
/// * `Ok(())` - If the value is a valid JSON Schema
/// * `Err(ValidationError)` - If the value is not a valid JSON Schema
///
/// # Error Codes
///
/// * `"invalid_json_schema"` - The JSON value is not a valid JSON Schema
///
/// # Examples
///
/// ```rust,ignore
/// use serde_json::json;
///
/// // Valid JSON Schema
/// let valid_schema = json!({
/// "type": "object",
/// "properties": {
/// "name": {"type": "string"}
/// }
/// });
/// assert!(validate_json_schema_value(&valid_schema).is_ok());
///
/// // Invalid JSON Schema
/// let invalid_schema = json!({"type": "invalid_type"});
/// assert!(validate_json_schema_value(&invalid_schema).is_err());
/// ```