rustapi_openapi/
schemas.rs1use serde::{Deserialize, Serialize};
6use utoipa::ToSchema;
7
8#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
10pub struct ErrorSchema {
11 pub error: ErrorBodySchema,
13 #[serde(skip_serializing_if = "Option::is_none")]
15 pub request_id: Option<String>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
20pub struct ErrorBodySchema {
21 #[serde(rename = "type")]
23 pub error_type: String,
24 pub message: String,
26 #[serde(skip_serializing_if = "Option::is_none")]
28 pub fields: Option<Vec<FieldErrorSchema>>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
33pub struct FieldErrorSchema {
34 pub field: String,
36 pub code: String,
38 pub message: String,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
44pub struct ValidationErrorSchema {
45 pub error: ValidationErrorBodySchema,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
51pub struct ValidationErrorBodySchema {
52 #[serde(rename = "type")]
54 pub error_type: String,
55 pub message: String,
57 pub fields: Vec<FieldErrorSchema>,
59}
60
61impl ValidationErrorSchema {
62 pub fn example() -> Self {
64 Self {
65 error: ValidationErrorBodySchema {
66 error_type: "validation_error".to_string(),
67 message: "Request validation failed".to_string(),
68 fields: vec![
69 FieldErrorSchema {
70 field: "email".to_string(),
71 code: "email".to_string(),
72 message: "Invalid email format".to_string(),
73 },
74 ],
75 },
76 }
77 }
78}
79
80impl ErrorSchema {
81 pub fn not_found_example() -> Self {
83 Self {
84 error: ErrorBodySchema {
85 error_type: "not_found".to_string(),
86 message: "Resource not found".to_string(),
87 fields: None,
88 },
89 request_id: None,
90 }
91 }
92
93 pub fn internal_error_example() -> Self {
95 Self {
96 error: ErrorBodySchema {
97 error_type: "internal_error".to_string(),
98 message: "An internal error occurred".to_string(),
99 fields: None,
100 },
101 request_id: None,
102 }
103 }
104
105 pub fn bad_request_example() -> Self {
107 Self {
108 error: ErrorBodySchema {
109 error_type: "bad_request".to_string(),
110 message: "Invalid request".to_string(),
111 fields: None,
112 },
113 request_id: None,
114 }
115 }
116}