spikard 0.4.0-x64-mingw-ucrt
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +1 -0
- data/README.md +659 -0
- data/ext/spikard_rb/Cargo.toml +17 -0
- data/ext/spikard_rb/extconf.rb +10 -0
- data/ext/spikard_rb/src/lib.rs +6 -0
- data/lib/spikard/app.rb +405 -0
- data/lib/spikard/background.rb +27 -0
- data/lib/spikard/config.rb +396 -0
- data/lib/spikard/converters.rb +13 -0
- data/lib/spikard/handler_wrapper.rb +113 -0
- data/lib/spikard/provide.rb +214 -0
- data/lib/spikard/response.rb +173 -0
- data/lib/spikard/schema.rb +243 -0
- data/lib/spikard/sse.rb +111 -0
- data/lib/spikard/streaming_response.rb +44 -0
- data/lib/spikard/testing.rb +221 -0
- data/lib/spikard/upload_file.rb +131 -0
- data/lib/spikard/version.rb +5 -0
- data/lib/spikard/websocket.rb +59 -0
- data/lib/spikard.rb +43 -0
- data/sig/spikard.rbs +366 -0
- data/vendor/bundle/ruby/3.4.0/gems/diff-lcs-1.6.2/mise.toml +5 -0
- data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +139 -0
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +561 -0
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +403 -0
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +274 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +25 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +298 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +637 -0
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +309 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +355 -0
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +502 -0
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +389 -0
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +413 -0
- data/vendor/crates/spikard-core/Cargo.toml +40 -0
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
- data/vendor/crates/spikard-core/src/debug.rs +63 -0
- data/vendor/crates/spikard-core/src/di/container.rs +726 -0
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
- data/vendor/crates/spikard-core/src/di/error.rs +118 -0
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
- data/vendor/crates/spikard-core/src/di/value.rs +283 -0
- data/vendor/crates/spikard-core/src/errors.rs +39 -0
- data/vendor/crates/spikard-core/src/http.rs +153 -0
- data/vendor/crates/spikard-core/src/lib.rs +29 -0
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
- data/vendor/crates/spikard-core/src/metadata.rs +397 -0
- data/vendor/crates/spikard-core/src/parameters.rs +723 -0
- data/vendor/crates/spikard-core/src/problem.rs +310 -0
- data/vendor/crates/spikard-core/src/request_data.rs +189 -0
- data/vendor/crates/spikard-core/src/router.rs +249 -0
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +689 -0
- data/vendor/crates/spikard-core/src/validation/mod.rs +459 -0
- data/vendor/crates/spikard-http/Cargo.toml +58 -0
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +147 -0
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +91 -0
- data/vendor/crates/spikard-http/src/auth.rs +247 -0
- data/vendor/crates/spikard-http/src/background.rs +1562 -0
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
- data/vendor/crates/spikard-http/src/cors.rs +490 -0
- data/vendor/crates/spikard-http/src/debug.rs +63 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +1878 -0
- data/vendor/crates/spikard-http/src/handler_response.rs +532 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +861 -0
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
- data/vendor/crates/spikard-http/src/lib.rs +524 -0
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +930 -0
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +541 -0
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +867 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +678 -0
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
- data/vendor/crates/spikard-http/src/response.rs +399 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +1557 -0
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +806 -0
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +630 -0
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +497 -0
- data/vendor/crates/spikard-http/src/sse.rs +961 -0
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
- data/vendor/crates/spikard-http/src/testing.rs +377 -0
- data/vendor/crates/spikard-http/src/websocket.rs +831 -0
- data/vendor/crates/spikard-http/tests/background_behavior.rs +918 -0
- data/vendor/crates/spikard-http/tests/common/handlers.rs +308 -0
- data/vendor/crates/spikard-http/tests/common/mod.rs +21 -0
- data/vendor/crates/spikard-http/tests/di_integration.rs +202 -0
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +4 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1135 -0
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +688 -0
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +324 -0
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +728 -0
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +724 -0
- data/vendor/crates/spikard-rb/Cargo.toml +43 -0
- data/vendor/crates/spikard-rb/build.rs +199 -0
- data/vendor/crates/spikard-rb/src/background.rs +63 -0
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/config/server_config.rs +283 -0
- data/vendor/crates/spikard-rb/src/conversion.rs +459 -0
- data/vendor/crates/spikard-rb/src/di/builder.rs +105 -0
- data/vendor/crates/spikard-rb/src/di/mod.rs +413 -0
- data/vendor/crates/spikard-rb/src/handler.rs +612 -0
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
- data/vendor/crates/spikard-rb/src/lib.rs +1857 -0
- data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -0
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +427 -0
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +326 -0
- data/vendor/crates/spikard-rb/src/server.rs +283 -0
- data/vendor/crates/spikard-rb/src/sse.rs +231 -0
- data/vendor/crates/spikard-rb/src/testing/client.rs +404 -0
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
- data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -0
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +221 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
- data/vendor/crates/spikard-rb/tests/magnus_ffi_tests.rs +14 -0
- metadata +213 -0
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
//! Table-driven error mapping for JSON Schema validation failures
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides a structured approach to mapping JSON Schema validation errors
|
|
4
|
+
//! to consistent error codes and messages. Instead of massive if-else chains, we use
|
|
5
|
+
//! enum variants and pattern matching for maintainability and testability.
|
|
6
|
+
|
|
7
|
+
use serde_json::Value;
|
|
8
|
+
|
|
9
|
+
/// Represents the different types of validation errors that can occur
|
|
10
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
11
|
+
pub enum ErrorCondition {
|
|
12
|
+
/// String length is below minimum
|
|
13
|
+
StringTooShort { min_length: Option<u64> },
|
|
14
|
+
/// String length exceeds maximum
|
|
15
|
+
StringTooLong { max_length: Option<u64> },
|
|
16
|
+
/// Number is not greater than exclusive minimum
|
|
17
|
+
GreaterThan { value: Option<i64> },
|
|
18
|
+
/// Number is not greater than or equal to minimum
|
|
19
|
+
GreaterThanEqual { value: Option<i64> },
|
|
20
|
+
/// Number is not less than exclusive maximum
|
|
21
|
+
LessThan { value: Option<i64> },
|
|
22
|
+
/// Number is not less than or equal to maximum
|
|
23
|
+
LessThanEqual { value: Option<i64> },
|
|
24
|
+
/// Value is not one of allowed enum values
|
|
25
|
+
Enum { values: Option<Vec<String>> },
|
|
26
|
+
/// String does not match pattern
|
|
27
|
+
StringPatternMismatch { pattern: Option<String> },
|
|
28
|
+
/// Email format validation failed
|
|
29
|
+
EmailFormat,
|
|
30
|
+
/// UUID format validation failed
|
|
31
|
+
UuidFormat,
|
|
32
|
+
/// Date-time format validation failed
|
|
33
|
+
DatetimeFormat,
|
|
34
|
+
/// Date format validation failed
|
|
35
|
+
DateFormat,
|
|
36
|
+
/// Other format validation failed
|
|
37
|
+
FormatError,
|
|
38
|
+
/// Type mismatch
|
|
39
|
+
TypeMismatch { expected_type: String },
|
|
40
|
+
/// Required field is missing
|
|
41
|
+
Missing,
|
|
42
|
+
/// Additional properties not allowed
|
|
43
|
+
AdditionalProperties { field: String },
|
|
44
|
+
/// Array has too few items
|
|
45
|
+
TooFewItems { min_items: Option<usize> },
|
|
46
|
+
/// Array has too many items
|
|
47
|
+
TooManyItems,
|
|
48
|
+
/// Fallback for unmapped errors
|
|
49
|
+
ValidationError,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
impl ErrorCondition {
|
|
53
|
+
/// Determine the error condition from schema path and error message
|
|
54
|
+
pub fn from_schema_error(schema_path_str: &str, error_msg: &str) -> Self {
|
|
55
|
+
// Check schema path for specific constraint keywords
|
|
56
|
+
match () {
|
|
57
|
+
_ if schema_path_str.contains("minLength") => Self::StringTooShort { min_length: None },
|
|
58
|
+
_ if schema_path_str.contains("maxLength") => Self::StringTooLong { max_length: None },
|
|
59
|
+
_ if schema_path_str.contains("exclusiveMinimum")
|
|
60
|
+
|| (error_msg.contains("less than or equal to") && error_msg.contains("minimum")) =>
|
|
61
|
+
{
|
|
62
|
+
Self::GreaterThan { value: None }
|
|
63
|
+
}
|
|
64
|
+
_ if schema_path_str.contains("minimum") || error_msg.contains("less than the minimum") => {
|
|
65
|
+
Self::GreaterThanEqual { value: None }
|
|
66
|
+
}
|
|
67
|
+
_ if schema_path_str.contains("exclusiveMaximum")
|
|
68
|
+
|| (error_msg.contains("greater than or equal to") && error_msg.contains("maximum")) =>
|
|
69
|
+
{
|
|
70
|
+
Self::LessThan { value: None }
|
|
71
|
+
}
|
|
72
|
+
_ if schema_path_str.contains("maximum") || error_msg.contains("greater than the maximum") => {
|
|
73
|
+
Self::LessThanEqual { value: None }
|
|
74
|
+
}
|
|
75
|
+
_ if schema_path_str.contains("enum") || error_msg.contains("is not one of") => Self::Enum { values: None },
|
|
76
|
+
_ if schema_path_str.contains("pattern") || error_msg.contains("does not match") => {
|
|
77
|
+
Self::StringPatternMismatch { pattern: None }
|
|
78
|
+
}
|
|
79
|
+
_ if schema_path_str.contains("format") => {
|
|
80
|
+
if error_msg.contains("email") {
|
|
81
|
+
Self::EmailFormat
|
|
82
|
+
} else if error_msg.contains("uuid") {
|
|
83
|
+
Self::UuidFormat
|
|
84
|
+
} else if error_msg.contains("date-time") {
|
|
85
|
+
Self::DatetimeFormat
|
|
86
|
+
} else if error_msg.contains("date") {
|
|
87
|
+
Self::DateFormat
|
|
88
|
+
} else {
|
|
89
|
+
Self::FormatError
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
_ if schema_path_str.contains("/type") => Self::TypeMismatch {
|
|
93
|
+
expected_type: "unknown".to_string(),
|
|
94
|
+
},
|
|
95
|
+
_ if schema_path_str.ends_with("/required") => Self::Missing,
|
|
96
|
+
_ if schema_path_str.contains("/additionalProperties")
|
|
97
|
+
|| error_msg.contains("Additional properties are not allowed") =>
|
|
98
|
+
{
|
|
99
|
+
Self::AdditionalProperties { field: String::new() }
|
|
100
|
+
}
|
|
101
|
+
_ if schema_path_str.contains("/minItems") => Self::TooFewItems { min_items: None },
|
|
102
|
+
_ if schema_path_str.contains("/maxItems") => Self::TooManyItems,
|
|
103
|
+
_ => Self::ValidationError,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Get the error type code for this condition
|
|
108
|
+
pub fn error_type(&self) -> &'static str {
|
|
109
|
+
match self {
|
|
110
|
+
Self::StringTooShort { .. } => "string_too_short",
|
|
111
|
+
Self::StringTooLong { .. } => "string_too_long",
|
|
112
|
+
Self::GreaterThan { .. } => "greater_than",
|
|
113
|
+
Self::GreaterThanEqual { .. } => "greater_than_equal",
|
|
114
|
+
Self::LessThan { .. } => "less_than",
|
|
115
|
+
Self::LessThanEqual { .. } => "less_than_equal",
|
|
116
|
+
Self::Enum { .. } => "enum",
|
|
117
|
+
Self::StringPatternMismatch { .. } => "string_pattern_mismatch",
|
|
118
|
+
Self::EmailFormat => "string_pattern_mismatch",
|
|
119
|
+
Self::UuidFormat => "uuid_parsing",
|
|
120
|
+
Self::DatetimeFormat => "datetime_parsing",
|
|
121
|
+
Self::DateFormat => "date_parsing",
|
|
122
|
+
Self::FormatError => "format_error",
|
|
123
|
+
Self::TypeMismatch { .. } => "type_error",
|
|
124
|
+
Self::Missing => "missing",
|
|
125
|
+
Self::AdditionalProperties { .. } => "validation_error",
|
|
126
|
+
Self::TooFewItems { .. } => "too_short",
|
|
127
|
+
Self::TooManyItems => "too_long",
|
|
128
|
+
Self::ValidationError => "validation_error",
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/// Get default message for this error condition
|
|
133
|
+
pub fn default_message(&self) -> &'static str {
|
|
134
|
+
match self {
|
|
135
|
+
Self::StringTooShort { .. } => "String is too short",
|
|
136
|
+
Self::StringTooLong { .. } => "String is too long",
|
|
137
|
+
Self::GreaterThan { .. } => "Input should be greater than the minimum",
|
|
138
|
+
Self::GreaterThanEqual { .. } => "Input should be greater than or equal to the minimum",
|
|
139
|
+
Self::LessThan { .. } => "Input should be less than the maximum",
|
|
140
|
+
Self::LessThanEqual { .. } => "Input should be less than or equal to the maximum",
|
|
141
|
+
Self::Enum { .. } => "Input should be one of the allowed values",
|
|
142
|
+
Self::StringPatternMismatch { .. } => "String does not match expected pattern",
|
|
143
|
+
Self::EmailFormat => "String should match email pattern",
|
|
144
|
+
Self::UuidFormat => "Input should be a valid UUID",
|
|
145
|
+
Self::DatetimeFormat => "Input should be a valid datetime",
|
|
146
|
+
Self::DateFormat => "Input should be a valid date",
|
|
147
|
+
Self::FormatError => "Invalid format",
|
|
148
|
+
Self::TypeMismatch { .. } => "Invalid type",
|
|
149
|
+
Self::Missing => "Field required",
|
|
150
|
+
Self::AdditionalProperties { .. } => "Additional properties are not allowed",
|
|
151
|
+
Self::TooFewItems { .. } => "List should have at least N items after validation",
|
|
152
|
+
Self::TooManyItems => "List should have at most N items after validation",
|
|
153
|
+
Self::ValidationError => "Validation error",
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// Maps validation conditions to error details with schema context
|
|
159
|
+
pub struct ErrorMapper;
|
|
160
|
+
|
|
161
|
+
impl ErrorMapper {
|
|
162
|
+
/// Map an error condition to its type, message, and context
|
|
163
|
+
pub fn map_error(
|
|
164
|
+
condition: &ErrorCondition,
|
|
165
|
+
schema: &Value,
|
|
166
|
+
schema_prop_path: &str,
|
|
167
|
+
generic_message: &str,
|
|
168
|
+
) -> (String, String, Option<Value>) {
|
|
169
|
+
match condition {
|
|
170
|
+
ErrorCondition::StringTooShort { .. } => {
|
|
171
|
+
if let Some(min_len) = schema
|
|
172
|
+
.pointer(&format!("{}/minLength", schema_prop_path))
|
|
173
|
+
.and_then(|v| v.as_u64())
|
|
174
|
+
{
|
|
175
|
+
let ctx = serde_json::json!({"min_length": min_len});
|
|
176
|
+
(
|
|
177
|
+
"string_too_short".to_string(),
|
|
178
|
+
format!("String should have at least {} characters", min_len),
|
|
179
|
+
Some(ctx),
|
|
180
|
+
)
|
|
181
|
+
} else {
|
|
182
|
+
("string_too_short".to_string(), "String is too short".to_string(), None)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
ErrorCondition::StringTooLong { .. } => {
|
|
186
|
+
if let Some(max_len) = schema
|
|
187
|
+
.pointer(&format!("{}/maxLength", schema_prop_path))
|
|
188
|
+
.and_then(|v| v.as_u64())
|
|
189
|
+
{
|
|
190
|
+
let ctx = serde_json::json!({"max_length": max_len});
|
|
191
|
+
(
|
|
192
|
+
"string_too_long".to_string(),
|
|
193
|
+
format!("String should have at most {} characters", max_len),
|
|
194
|
+
Some(ctx),
|
|
195
|
+
)
|
|
196
|
+
} else {
|
|
197
|
+
("string_too_long".to_string(), "String is too long".to_string(), None)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
ErrorCondition::GreaterThan { .. } => {
|
|
201
|
+
if let Some(min_val) = schema
|
|
202
|
+
.pointer(&format!("{}/exclusiveMinimum", schema_prop_path))
|
|
203
|
+
.and_then(|v| v.as_i64())
|
|
204
|
+
{
|
|
205
|
+
let ctx = serde_json::json!({"gt": min_val});
|
|
206
|
+
(
|
|
207
|
+
"greater_than".to_string(),
|
|
208
|
+
format!("Input should be greater than {}", min_val),
|
|
209
|
+
Some(ctx),
|
|
210
|
+
)
|
|
211
|
+
} else {
|
|
212
|
+
(
|
|
213
|
+
"greater_than".to_string(),
|
|
214
|
+
"Input should be greater than the minimum".to_string(),
|
|
215
|
+
None,
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
ErrorCondition::GreaterThanEqual { .. } => {
|
|
220
|
+
if let Some(min_val) = schema
|
|
221
|
+
.pointer(&format!("{}/minimum", schema_prop_path))
|
|
222
|
+
.and_then(|v| v.as_i64())
|
|
223
|
+
{
|
|
224
|
+
let ctx = serde_json::json!({"ge": min_val});
|
|
225
|
+
(
|
|
226
|
+
"greater_than_equal".to_string(),
|
|
227
|
+
format!("Input should be greater than or equal to {}", min_val),
|
|
228
|
+
Some(ctx),
|
|
229
|
+
)
|
|
230
|
+
} else {
|
|
231
|
+
(
|
|
232
|
+
"greater_than_equal".to_string(),
|
|
233
|
+
"Input should be greater than or equal to the minimum".to_string(),
|
|
234
|
+
None,
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
ErrorCondition::LessThan { .. } => {
|
|
239
|
+
if let Some(max_val) = schema
|
|
240
|
+
.pointer(&format!("{}/exclusiveMaximum", schema_prop_path))
|
|
241
|
+
.and_then(|v| v.as_i64())
|
|
242
|
+
{
|
|
243
|
+
let ctx = serde_json::json!({"lt": max_val});
|
|
244
|
+
(
|
|
245
|
+
"less_than".to_string(),
|
|
246
|
+
format!("Input should be less than {}", max_val),
|
|
247
|
+
Some(ctx),
|
|
248
|
+
)
|
|
249
|
+
} else {
|
|
250
|
+
(
|
|
251
|
+
"less_than".to_string(),
|
|
252
|
+
"Input should be less than the maximum".to_string(),
|
|
253
|
+
None,
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
ErrorCondition::LessThanEqual { .. } => {
|
|
258
|
+
if let Some(max_val) = schema
|
|
259
|
+
.pointer(&format!("{}/maximum", schema_prop_path))
|
|
260
|
+
.and_then(|v| v.as_i64())
|
|
261
|
+
{
|
|
262
|
+
let ctx = serde_json::json!({"le": max_val});
|
|
263
|
+
(
|
|
264
|
+
"less_than_equal".to_string(),
|
|
265
|
+
format!("Input should be less than or equal to {}", max_val),
|
|
266
|
+
Some(ctx),
|
|
267
|
+
)
|
|
268
|
+
} else {
|
|
269
|
+
(
|
|
270
|
+
"less_than_equal".to_string(),
|
|
271
|
+
"Input should be less than or equal to the maximum".to_string(),
|
|
272
|
+
None,
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
ErrorCondition::Enum { .. } => {
|
|
277
|
+
if let Some(enum_values) = schema
|
|
278
|
+
.pointer(&format!("{}/enum", schema_prop_path))
|
|
279
|
+
.and_then(|v| v.as_array())
|
|
280
|
+
{
|
|
281
|
+
let values: Vec<String> = enum_values
|
|
282
|
+
.iter()
|
|
283
|
+
.filter_map(|v| v.as_str().map(|s| format!("'{}'", s)))
|
|
284
|
+
.collect();
|
|
285
|
+
|
|
286
|
+
let msg = if values.len() > 1 {
|
|
287
|
+
let last = values.last().unwrap();
|
|
288
|
+
let rest = &values[..values.len() - 1];
|
|
289
|
+
format!("Input should be {} or {}", rest.join(", "), last)
|
|
290
|
+
} else if !values.is_empty() {
|
|
291
|
+
format!("Input should be {}", values[0])
|
|
292
|
+
} else {
|
|
293
|
+
"Input should be one of the allowed values".to_string()
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
let expected_str = if values.len() > 1 {
|
|
297
|
+
let last = values.last().unwrap();
|
|
298
|
+
let rest = &values[..values.len() - 1];
|
|
299
|
+
format!("{} or {}", rest.join(", "), last)
|
|
300
|
+
} else if !values.is_empty() {
|
|
301
|
+
values[0].clone()
|
|
302
|
+
} else {
|
|
303
|
+
"allowed values".to_string()
|
|
304
|
+
};
|
|
305
|
+
let ctx = serde_json::json!({"expected": expected_str});
|
|
306
|
+
("enum".to_string(), msg, Some(ctx))
|
|
307
|
+
} else {
|
|
308
|
+
(
|
|
309
|
+
"enum".to_string(),
|
|
310
|
+
"Input should be one of the allowed values".to_string(),
|
|
311
|
+
None,
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
ErrorCondition::StringPatternMismatch { .. } => {
|
|
316
|
+
if let Some(pattern) = schema
|
|
317
|
+
.pointer(&format!("{}/pattern", schema_prop_path))
|
|
318
|
+
.and_then(|v| v.as_str())
|
|
319
|
+
{
|
|
320
|
+
let ctx = serde_json::json!({"pattern": pattern});
|
|
321
|
+
let msg = format!("String should match pattern '{}'", pattern);
|
|
322
|
+
("string_pattern_mismatch".to_string(), msg, Some(ctx))
|
|
323
|
+
} else {
|
|
324
|
+
(
|
|
325
|
+
"string_pattern_mismatch".to_string(),
|
|
326
|
+
"String does not match expected pattern".to_string(),
|
|
327
|
+
None,
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
ErrorCondition::EmailFormat => {
|
|
332
|
+
let email_pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
|
|
333
|
+
let ctx = serde_json::json!({"pattern": email_pattern});
|
|
334
|
+
(
|
|
335
|
+
"string_pattern_mismatch".to_string(),
|
|
336
|
+
format!("String should match pattern '{}'", email_pattern),
|
|
337
|
+
Some(ctx),
|
|
338
|
+
)
|
|
339
|
+
}
|
|
340
|
+
ErrorCondition::UuidFormat => (
|
|
341
|
+
"uuid_parsing".to_string(),
|
|
342
|
+
"Input should be a valid UUID".to_string(),
|
|
343
|
+
None,
|
|
344
|
+
),
|
|
345
|
+
ErrorCondition::DatetimeFormat => (
|
|
346
|
+
"datetime_parsing".to_string(),
|
|
347
|
+
"Input should be a valid datetime".to_string(),
|
|
348
|
+
None,
|
|
349
|
+
),
|
|
350
|
+
ErrorCondition::DateFormat => (
|
|
351
|
+
"date_parsing".to_string(),
|
|
352
|
+
"Input should be a valid date".to_string(),
|
|
353
|
+
None,
|
|
354
|
+
),
|
|
355
|
+
ErrorCondition::FormatError => ("format_error".to_string(), generic_message.to_string(), None),
|
|
356
|
+
ErrorCondition::TypeMismatch { expected_type } => {
|
|
357
|
+
let (error_type, msg) = match expected_type.as_str() {
|
|
358
|
+
"integer" => (
|
|
359
|
+
"int_parsing".to_string(),
|
|
360
|
+
"Input should be a valid integer, unable to parse string as an integer".to_string(),
|
|
361
|
+
),
|
|
362
|
+
"number" => (
|
|
363
|
+
"float_parsing".to_string(),
|
|
364
|
+
"Input should be a valid number, unable to parse string as a number".to_string(),
|
|
365
|
+
),
|
|
366
|
+
"boolean" => (
|
|
367
|
+
"bool_parsing".to_string(),
|
|
368
|
+
"Input should be a valid boolean".to_string(),
|
|
369
|
+
),
|
|
370
|
+
"string" => ("string_type".to_string(), "Input should be a valid string".to_string()),
|
|
371
|
+
_ => (
|
|
372
|
+
"type_error".to_string(),
|
|
373
|
+
format!("Input should be a valid {}", expected_type),
|
|
374
|
+
),
|
|
375
|
+
};
|
|
376
|
+
(error_type, msg, None)
|
|
377
|
+
}
|
|
378
|
+
ErrorCondition::Missing => ("missing".to_string(), "Field required".to_string(), None),
|
|
379
|
+
ErrorCondition::AdditionalProperties { field } => {
|
|
380
|
+
let ctx = serde_json::json!({
|
|
381
|
+
"additional_properties": false,
|
|
382
|
+
"unexpected_field": field
|
|
383
|
+
});
|
|
384
|
+
(
|
|
385
|
+
"validation_error".to_string(),
|
|
386
|
+
"Additional properties are not allowed".to_string(),
|
|
387
|
+
Some(ctx),
|
|
388
|
+
)
|
|
389
|
+
}
|
|
390
|
+
ErrorCondition::TooFewItems { min_items } => {
|
|
391
|
+
let min = min_items.unwrap_or(1);
|
|
392
|
+
let ctx = serde_json::json!({
|
|
393
|
+
"min_length": min
|
|
394
|
+
});
|
|
395
|
+
(
|
|
396
|
+
"too_short".to_string(),
|
|
397
|
+
format!("List should have at least {} item after validation", min),
|
|
398
|
+
Some(ctx),
|
|
399
|
+
)
|
|
400
|
+
}
|
|
401
|
+
ErrorCondition::TooManyItems => {
|
|
402
|
+
let ctx = serde_json::json!({
|
|
403
|
+
"max_length": 1
|
|
404
|
+
});
|
|
405
|
+
(
|
|
406
|
+
"too_long".to_string(),
|
|
407
|
+
"List should have at most N items after validation".to_string(),
|
|
408
|
+
Some(ctx),
|
|
409
|
+
)
|
|
410
|
+
}
|
|
411
|
+
ErrorCondition::ValidationError => ("validation_error".to_string(), generic_message.to_string(), None),
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
#[cfg(test)]
|
|
417
|
+
mod tests {
|
|
418
|
+
use super::*;
|
|
419
|
+
use serde_json::json;
|
|
420
|
+
|
|
421
|
+
#[test]
|
|
422
|
+
fn test_string_too_short_detection() {
|
|
423
|
+
let condition = ErrorCondition::from_schema_error("some/path/minLength", "");
|
|
424
|
+
assert_eq!(condition, ErrorCondition::StringTooShort { min_length: None });
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
#[test]
|
|
428
|
+
fn test_string_too_long_detection() {
|
|
429
|
+
let condition = ErrorCondition::from_schema_error("some/path/maxLength", "");
|
|
430
|
+
assert_eq!(condition, ErrorCondition::StringTooLong { max_length: None });
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
#[test]
|
|
434
|
+
fn test_minimum_detection() {
|
|
435
|
+
let condition = ErrorCondition::from_schema_error("some/path/minimum", "");
|
|
436
|
+
assert_eq!(condition, ErrorCondition::GreaterThanEqual { value: None });
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
#[test]
|
|
440
|
+
fn test_exclusive_minimum_detection() {
|
|
441
|
+
let condition = ErrorCondition::from_schema_error("some/path/exclusiveMinimum", "");
|
|
442
|
+
assert_eq!(condition, ErrorCondition::GreaterThan { value: None });
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
#[test]
|
|
446
|
+
fn test_maximum_detection() {
|
|
447
|
+
let condition = ErrorCondition::from_schema_error("some/path/maximum", "");
|
|
448
|
+
assert_eq!(condition, ErrorCondition::LessThanEqual { value: None });
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
#[test]
|
|
452
|
+
fn test_exclusive_maximum_detection() {
|
|
453
|
+
let condition = ErrorCondition::from_schema_error("some/path/exclusiveMaximum", "");
|
|
454
|
+
assert_eq!(condition, ErrorCondition::LessThan { value: None });
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
#[test]
|
|
458
|
+
fn test_enum_detection() {
|
|
459
|
+
let condition = ErrorCondition::from_schema_error("some/path/enum", "");
|
|
460
|
+
assert_eq!(condition, ErrorCondition::Enum { values: None });
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
#[test]
|
|
464
|
+
fn test_pattern_detection() {
|
|
465
|
+
let condition = ErrorCondition::from_schema_error("some/path/pattern", "");
|
|
466
|
+
assert_eq!(condition, ErrorCondition::StringPatternMismatch { pattern: None });
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
#[test]
|
|
470
|
+
fn test_email_format_detection() {
|
|
471
|
+
let condition = ErrorCondition::from_schema_error("some/path/format", "email");
|
|
472
|
+
assert_eq!(condition, ErrorCondition::EmailFormat);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
#[test]
|
|
476
|
+
fn test_uuid_format_detection() {
|
|
477
|
+
let condition = ErrorCondition::from_schema_error("some/path/format", "uuid");
|
|
478
|
+
assert_eq!(condition, ErrorCondition::UuidFormat);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
#[test]
|
|
482
|
+
fn test_datetime_format_detection() {
|
|
483
|
+
let condition = ErrorCondition::from_schema_error("some/path/format", "date-time");
|
|
484
|
+
assert_eq!(condition, ErrorCondition::DatetimeFormat);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
#[test]
|
|
488
|
+
fn test_date_format_detection() {
|
|
489
|
+
let condition = ErrorCondition::from_schema_error("some/path/format", "date");
|
|
490
|
+
assert_eq!(condition, ErrorCondition::DateFormat);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
#[test]
|
|
494
|
+
fn test_type_error_detection() {
|
|
495
|
+
let condition = ErrorCondition::from_schema_error("some/path/type", "");
|
|
496
|
+
assert!(matches!(condition, ErrorCondition::TypeMismatch { .. }));
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
#[test]
|
|
500
|
+
fn test_missing_field_detection() {
|
|
501
|
+
let condition = ErrorCondition::from_schema_error("some/path/required", "");
|
|
502
|
+
assert_eq!(condition, ErrorCondition::Missing);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
#[test]
|
|
506
|
+
fn test_additional_properties_detection() {
|
|
507
|
+
let condition = ErrorCondition::from_schema_error("some/path/additionalProperties", "");
|
|
508
|
+
assert!(matches!(condition, ErrorCondition::AdditionalProperties { .. }));
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
#[test]
|
|
512
|
+
fn test_min_items_detection() {
|
|
513
|
+
let condition = ErrorCondition::from_schema_error("some/path/minItems", "");
|
|
514
|
+
assert!(matches!(condition, ErrorCondition::TooFewItems { .. }));
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
#[test]
|
|
518
|
+
fn test_max_items_detection() {
|
|
519
|
+
let condition = ErrorCondition::from_schema_error("some/path/maxItems", "");
|
|
520
|
+
assert_eq!(condition, ErrorCondition::TooManyItems);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
#[test]
|
|
524
|
+
fn test_error_type_codes() {
|
|
525
|
+
assert_eq!(
|
|
526
|
+
ErrorCondition::StringTooShort { min_length: None }.error_type(),
|
|
527
|
+
"string_too_short"
|
|
528
|
+
);
|
|
529
|
+
assert_eq!(
|
|
530
|
+
ErrorCondition::StringTooLong { max_length: None }.error_type(),
|
|
531
|
+
"string_too_long"
|
|
532
|
+
);
|
|
533
|
+
assert_eq!(ErrorCondition::GreaterThan { value: None }.error_type(), "greater_than");
|
|
534
|
+
assert_eq!(
|
|
535
|
+
ErrorCondition::GreaterThanEqual { value: None }.error_type(),
|
|
536
|
+
"greater_than_equal"
|
|
537
|
+
);
|
|
538
|
+
assert_eq!(ErrorCondition::LessThan { value: None }.error_type(), "less_than");
|
|
539
|
+
assert_eq!(
|
|
540
|
+
ErrorCondition::LessThanEqual { value: None }.error_type(),
|
|
541
|
+
"less_than_equal"
|
|
542
|
+
);
|
|
543
|
+
assert_eq!(ErrorCondition::Enum { values: None }.error_type(), "enum");
|
|
544
|
+
assert_eq!(
|
|
545
|
+
ErrorCondition::StringPatternMismatch { pattern: None }.error_type(),
|
|
546
|
+
"string_pattern_mismatch"
|
|
547
|
+
);
|
|
548
|
+
assert_eq!(ErrorCondition::EmailFormat.error_type(), "string_pattern_mismatch");
|
|
549
|
+
assert_eq!(ErrorCondition::UuidFormat.error_type(), "uuid_parsing");
|
|
550
|
+
assert_eq!(ErrorCondition::DatetimeFormat.error_type(), "datetime_parsing");
|
|
551
|
+
assert_eq!(ErrorCondition::DateFormat.error_type(), "date_parsing");
|
|
552
|
+
assert_eq!(ErrorCondition::FormatError.error_type(), "format_error");
|
|
553
|
+
assert_eq!(
|
|
554
|
+
ErrorCondition::TypeMismatch {
|
|
555
|
+
expected_type: "integer".to_string()
|
|
556
|
+
}
|
|
557
|
+
.error_type(),
|
|
558
|
+
"type_error"
|
|
559
|
+
);
|
|
560
|
+
assert_eq!(ErrorCondition::Missing.error_type(), "missing");
|
|
561
|
+
assert_eq!(
|
|
562
|
+
ErrorCondition::AdditionalProperties {
|
|
563
|
+
field: "extra".to_string()
|
|
564
|
+
}
|
|
565
|
+
.error_type(),
|
|
566
|
+
"validation_error"
|
|
567
|
+
);
|
|
568
|
+
assert_eq!(
|
|
569
|
+
ErrorCondition::TooFewItems { min_items: None }.error_type(),
|
|
570
|
+
"too_short"
|
|
571
|
+
);
|
|
572
|
+
assert_eq!(ErrorCondition::TooManyItems.error_type(), "too_long");
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
#[test]
|
|
576
|
+
fn test_mapper_string_length_constraints() {
|
|
577
|
+
let schema = json!({
|
|
578
|
+
"properties": {
|
|
579
|
+
"name": {
|
|
580
|
+
"type": "string",
|
|
581
|
+
"minLength": 5,
|
|
582
|
+
"maxLength": 20
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
let condition = ErrorCondition::StringTooShort { min_length: None };
|
|
588
|
+
let (error_type, msg, ctx_result) = ErrorMapper::map_error(&condition, &schema, "/properties/name", "");
|
|
589
|
+
assert_eq!(error_type, "string_too_short");
|
|
590
|
+
assert_eq!(msg, "String should have at least 5 characters");
|
|
591
|
+
assert_eq!(ctx_result, Some(json!({"min_length": 5})));
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
#[test]
|
|
595
|
+
fn test_mapper_numeric_constraints() {
|
|
596
|
+
let schema = json!({
|
|
597
|
+
"properties": {
|
|
598
|
+
"age": {
|
|
599
|
+
"type": "integer",
|
|
600
|
+
"minimum": 0,
|
|
601
|
+
"maximum": 150,
|
|
602
|
+
"exclusiveMinimum": -1,
|
|
603
|
+
"exclusiveMaximum": 151
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
let condition = ErrorCondition::GreaterThanEqual { value: None };
|
|
609
|
+
let (error_type, msg, ctx) = ErrorMapper::map_error(&condition, &schema, "/properties/age", "");
|
|
610
|
+
assert_eq!(error_type, "greater_than_equal");
|
|
611
|
+
assert_eq!(msg, "Input should be greater than or equal to 0");
|
|
612
|
+
assert_eq!(ctx, Some(json!({"ge": 0})));
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
#[test]
|
|
616
|
+
fn test_mapper_enum() {
|
|
617
|
+
let schema = json!({
|
|
618
|
+
"properties": {
|
|
619
|
+
"status": {
|
|
620
|
+
"type": "string",
|
|
621
|
+
"enum": ["active", "inactive", "pending"]
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
let condition = ErrorCondition::Enum { values: None };
|
|
627
|
+
let (error_type, msg, _ctx) = ErrorMapper::map_error(&condition, &schema, "/properties/status", "");
|
|
628
|
+
assert_eq!(error_type, "enum");
|
|
629
|
+
assert!(msg.contains("'active'"));
|
|
630
|
+
assert!(msg.contains("'inactive'"));
|
|
631
|
+
assert!(msg.contains("'pending'"));
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
#[test]
|
|
635
|
+
fn test_mapper_type_mismatch() {
|
|
636
|
+
let schema = json!({
|
|
637
|
+
"properties": {
|
|
638
|
+
"count": { "type": "integer" }
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
let condition = ErrorCondition::TypeMismatch {
|
|
643
|
+
expected_type: "integer".to_string(),
|
|
644
|
+
};
|
|
645
|
+
let (error_type, msg, _) = ErrorMapper::map_error(&condition, &schema, "/properties/count", "");
|
|
646
|
+
assert_eq!(error_type, "int_parsing");
|
|
647
|
+
assert!(msg.contains("integer"));
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
#[test]
|
|
651
|
+
fn test_mapper_email_format() {
|
|
652
|
+
let schema = json!({});
|
|
653
|
+
|
|
654
|
+
let condition = ErrorCondition::EmailFormat;
|
|
655
|
+
let (error_type, msg, ctx) = ErrorMapper::map_error(&condition, &schema, "", "");
|
|
656
|
+
assert_eq!(error_type, "string_pattern_mismatch");
|
|
657
|
+
assert!(msg.contains("@"));
|
|
658
|
+
assert!(ctx.is_some());
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
#[test]
|
|
662
|
+
fn test_mapper_uuid_format() {
|
|
663
|
+
let schema = json!({});
|
|
664
|
+
|
|
665
|
+
let condition = ErrorCondition::UuidFormat;
|
|
666
|
+
let (error_type, msg, _) = ErrorMapper::map_error(&condition, &schema, "", "");
|
|
667
|
+
assert_eq!(error_type, "uuid_parsing");
|
|
668
|
+
assert_eq!(msg, "Input should be a valid UUID");
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
#[test]
|
|
672
|
+
fn test_mapper_additional_properties() {
|
|
673
|
+
let schema = json!({});
|
|
674
|
+
|
|
675
|
+
let condition = ErrorCondition::AdditionalProperties {
|
|
676
|
+
field: "extra_field".to_string(),
|
|
677
|
+
};
|
|
678
|
+
let (error_type, msg, ctx) = ErrorMapper::map_error(&condition, &schema, "", "");
|
|
679
|
+
assert_eq!(error_type, "validation_error");
|
|
680
|
+
assert_eq!(msg, "Additional properties are not allowed");
|
|
681
|
+
assert_eq!(
|
|
682
|
+
ctx,
|
|
683
|
+
Some(json!({
|
|
684
|
+
"additional_properties": false,
|
|
685
|
+
"unexpected_field": "extra_field"
|
|
686
|
+
}))
|
|
687
|
+
);
|
|
688
|
+
}
|
|
689
|
+
}
|