Supertest · NestJS Validation และ Error
Route Param Validation
ตรวจ identifier ที่ไม่ถูกต้องตั้งแต่ HTTP boundary
ตรวจ route param ก่อนลง service
ถ้า `id` ใน contract เป็นตัวเลข route `/todos/not-a-number` ควรหยุดที่ HTTP boundary และคืน 400 ไม่ควรส่ง string ผิดชนิดลง repository แล้วรอ database เป็นผู้ fail
invalid route parameterTS
แยก invalid id กับ missing id
| input | ชั้นที่ควร fail | status |
|---|---|---|
| not-a-number | route/pipe boundary | 400 |
| 9999 ที่รูปแบบถูกแต่ไม่มี record | service/resource lookup | 404 |
| 7 ที่มี record | อ่าน resource สำเร็จ | 200 |
กฎสำคัญ
meaning
status บอกเหตุผลต่างกัน
400 แปลว่า input ใช้ไม่ได้ ส่วน 404 แปลว่า input ใช้ได้แต่ resource ไม่อยู่
diagnostic
test ชื่อให้บอก branch
ชื่อ `returns 400 for invalid id` ดีกว่า `handles bad input` เพราะอ่าน failure แล้วรู้ boundary ทันที