spikard 0.12.0 → 0.15.2

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