feat: enhance /payer route with email format validation and update HttpError class to use ErrorDescEnum
This commit is contained in:
		
							parent
							
								
									6c6936097e
								
							
						
					
					
						commit
						be9a4c21ee
					
				|  | @ -19,9 +19,11 @@ app.use(route.post('/payer', async (ctx) => { | |||
| 	// TODO: 请求头验证 bearer token
 | ||||
| 	 | ||||
| 	const { name, address, email, abn } = ctx.request.body | ||||
| 	if (!name || !address || !email) { | ||||
| 		ctx.throw(400, 'required_fields_missing') | ||||
| 	} | ||||
| 	if (!name || !address || !email) ctx.throw(400, 'required_fields_missing') | ||||
| 	 | ||||
| 	// 验证邮箱格式
 | ||||
| 	const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ | ||||
| 	if (!emailRegex.test(email)) ctx.throw(400, 'invalid_field_format') | ||||
| 
 | ||||
| 	try { | ||||
| 		const payer = await func.createPayer(name, address, email, abn) | ||||
|  |  | |||
							
								
								
									
										20
									
								
								backend/src/types/HttpError.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										20
									
								
								backend/src/types/HttpError.d.ts
									
									
									
									
										vendored
									
									
								
							|  | @ -8,6 +8,24 @@ class HttpError extends Error { | |||
| 	 * @param message 错误的消息。 | ||||
| 	 * @param status 错误的状态码。 | ||||
| 	 */ | ||||
| 	constructor(message: 'required_fields_missing' | 'unknown_issues', status: number); | ||||
| 	constructor(message: ErrorDescEnum, status: number); | ||||
| 	status: 400 | 401 | 403 | 404 | 500; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * 错误描述的枚举。 | ||||
|  */ | ||||
| enum ErrorDescEnum { | ||||
| 	/** | ||||
| 	 * 必填字段缺失。通常与 HTTP 状态码 400 Bad Request 一起使用。 | ||||
| 	 */ | ||||
| 	required_fields_missing = 'required_fields_missing', | ||||
| 	/** | ||||
| 	 * 字段格式不正确。通常与 HTTP 状态码 400 Bad Request 一起使用。 | ||||
| 	 */ | ||||
| 	invalid_field_format = 'invalid_field_format', | ||||
| 	/** | ||||
| 	 * 未知错误。通常与 HTTP 状态码 500 Internal Server Error 一起使用。 | ||||
| 	 */ | ||||
| 	unknown_issues = 'unknown_issues' | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user