Skip to content

09.05 Validation Errors ​


📌 At a Glance ​

รายการรายละเอียด
TopicValidation Errors
Difficulty⭐⭐ Beginner
Reading Time10 นาที
TargetDeveloper, System Integrator
Related APIsPOST /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

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 Again

Typical Validation Errors ​

FieldValidation
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 Entity
json
{
    "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 ​

ValidationDescription
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 Again

Best 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

SISAHYGO API Integration Guide