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![FieldErrorSchema {
69 field: "email".to_string(),
70 code: "email".to_string(),
71 message: "Invalid email format".to_string(),
72 }],
73 },
74 }
75 }
76}
77
78impl ErrorSchema {
79 pub fn not_found_example() -> Self {
81 Self {
82 error: ErrorBodySchema {
83 error_type: "not_found".to_string(),
84 message: "Resource not found".to_string(),
85 fields: None,
86 },
87 request_id: None,
88 }
89 }
90
91 pub fn internal_error_example() -> Self {
93 Self {
94 error: ErrorBodySchema {
95 error_type: "internal_error".to_string(),
96 message: "An internal error occurred".to_string(),
97 fields: None,
98 },
99 request_id: None,
100 }
101 }
102
103 pub fn bad_request_example() -> Self {
105 Self {
106 error: ErrorBodySchema {
107 error_type: "bad_request".to_string(),
108 message: "Invalid request".to_string(),
109 fields: None,
110 },
111 request_id: None,
112 }
113 }
114}