Appearance
09.05 Validation Errors
📌 At a Glance
| รายการ | รายละเอียด |
|---|---|
| Topic | Validation Errors |
| Difficulty | ⭐⭐ Beginner |
| Reading Time | 10 นาที |
| Target | Developer, System Integrator |
| Related APIs | POST /order-checkings |
🎯 Learning Objectives
หลังจากศึกษาหัวข้อนี้แล้ว ผู้อ่านจะสามารถ
- เข้าใจ Validation Errors ของ SISAHYGO API
- วิเคราะห์สาเหตุของข้อมูลที่ไม่ผ่านการตรวจสอบ
- แก้ไข Request ก่อนส่งใหม่
- ออกแบบ Client-side Validation เพื่อลดข้อผิดพลาด
Overview
Validation Error คือข้อผิดพลาดที่เกิดจากข้อมูลใน Request ไม่เป็นไปตามเงื่อนไขที่ระบบกำหนด
SISAHYGO จะตรวจสอบข้อมูลก่อนดำเนินการสร้างรายการ หากข้อมูลไม่ถูกต้อง ระบบจะหยุดการประมวลผลและตอบกลับด้วย
http
HTTP/1.1 422 Unprocessable Entityพร้อมรายละเอียดของ Field ที่ต้องแก้ไข
Validation Errors เป็นข้อผิดพลาดที่พบได้บ่อยที่สุดในการเชื่อมต่อ API และสามารถป้องกันได้โดยการตรวจสอบข้อมูลใน Client System ก่อนส่ง Request
Figure 9-5 Validation Errors

Figure 9-5 แสดงกระบวนการตรวจสอบข้อมูล การตอบกลับ Validation Error และการแก้ไขข้อมูลก่อนส่ง Request ใหม่
Validation Workflow
text
Client System
│
▼
Create JSON Request
│
▼
POST /order-checkings
│
▼
Validation
│
├────────► Passed
│ │
│ ▼
│ Create Order
│
▼
422 Validation Error
│
▼
Read Error Response
│
▼
Correct Data
│
▼
Submit AgainTypical Validation Errors
| Field | Validation |
|---|---|
| customer_rec_id | ต้องมีอยู่ในระบบ |
| product_id | ต้องมีอยู่ใน Product Master |
| unit_id | ต้องมีอยู่ใน Unit Master |
| amount | ต้องมากกว่า 0 |
| items | ต้องมีอย่างน้อย 1 รายการ |
| client_reference_no | ห้ามเป็นค่าว่าง และควรไม่ซ้ำ |
Example : Invalid Receiver
Request
json
{
"customer_rec_id": 99999999
}Response
http
HTTP/1.1 422 Unprocessable Entityjson
{
"success": false,
"message": "Validation failed.",
"details": [
{
"field": "customer_rec_id",
"message": "The selected receiver does not exist."
}
]
}Example : Invalid Product
json
{
"product_id": 999999
}Response
json
{
"success": false,
"message": "Validation failed.",
"details": [
{
"field": "items.0.product_id",
"message": "The selected product does not exist."
}
]
}Example : Invalid Quantity
json
{
"amount": 0
}Response
json
{
"success": false,
"message": "Validation failed.",
"details": [
{
"field": "items.0.amount",
"message": "Amount must be greater than zero."
}
]
}Common Validation Rules
| Validation | Description |
|---|---|
| Required | ต้องมีข้อมูล |
| Exists | ต้องมีข้อมูลใน Master Data |
| Numeric | ต้องเป็นตัวเลข |
| Greater Than Zero | ต้องมีค่ามากกว่า 0 |
| Array | ต้องเป็น Array |
| Unique | ต้องไม่ซ้ำ (เมื่อระบบกำหนด) |
Recommended Client-side Validation
ก่อนเรียก API ควรตรวจสอบ
- Receiver
- Product
- Unit
- Quantity
- Required Fields
- JSON Format
เพื่อลดการเกิด Validation Errors
Recommended Workflow
text
Load Master Data
│
▼
Validate Client Data
│
▼
Generate JSON
│
▼
POST API
│
▼
422 ?
│
├── No
│
▼
Success
│
└── Yes
│
▼
Display Error
│
▼
Correct Data
│
▼
Submit AgainBest Practices
- ใช้ Master Data ล่าสุด
- ตรวจสอบข้อมูลก่อนสร้าง JSON
- Validate ทั้ง Client และ Server
- แสดง Error ตาม Field ที่ผิด
- Highlight ช่องที่ต้องแก้ไข
- ไม่ Retry จนกว่าจะมีการแก้ไขข้อมูล
Related Sections
- 09.02 HTTP Status Codes
- 09.03 Standard Error Response
- 09.04 Authentication
Bulk Validation Behavior
ในกรณี Bulk Order Checking ระบบจะตรวจสอบข้อมูลแต่ละ Order แยกจากกัน ผลลัพธ์ของแต่ละรายการจะถูกส่งกลับใน results[]
ดังนั้น Request หนึ่งรายการอาจมีทั้งรายการที่สำเร็จและไม่สำเร็จ ซึ่งเรียกว่า Partial Success
Summary
Validation Errors เป็นข้อผิดพลาดที่เกิดจากข้อมูลใน Request ไม่ถูกต้องหรือไม่เป็นไปตามเงื่อนไขของระบบ การตรวจสอบข้อมูลใน Client System ก่อนส่ง API การใช้ Master Data ที่เป็นปัจจุบัน และการแสดงรายละเอียดของ Field ที่ผิดพลาดอย่างชัดเจน จะช่วยลดจำนวน Validation Errors และเพิ่มประสิทธิภาพของการเชื่อมต่อกับ SISAHYGO API
Next Step
➡️ 09.06 Error Handling Best Practices
