Thuta Learning
API Integration & Webhooks
BasicWeb Developmentintermediate

Response Validation

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Response Validation concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram ကို ဖတ်ပြီး request/response (သို့) event flow ဘယ်လိုစီးဆင်းသလဲ ခြေရာခံနိုင်ရန်
  • ကိုယ့် ကိုယ်ပိုင် API integration အတွက် ဘယ်လို အသုံးချသင့်သလဲ ရှင်းပြနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Status code 200 ဆိုတာ HTTP layer အဆင့်မှာ အလုပ်ဖြစ်တယ်ဆိုတာကိုပဲ ပြောပါတယ် — server က request ကို လက်ခံပြီး protocol level မှာ အောင်မြင်စွာ response ပြန်ပေးခဲ့တယ်ဆိုတာပါ။ Response ထဲက JSON body မှာ သင့် code မျှော်လင့်ထားတဲ့ field/type တွေ တကယ်ပါလားဆိုတာကို ဘာမှ မပြောပါဘူး။

ဒါက response validation ဖြေရှင်းပေးတဲ့ gap ပါ၊ provider ရဲ့ ယခုလက်ရှိ well-behaved API ကို build လုပ်နေတုန်း အလွယ်တကူ လျစ်လျူရှုမိတတ်ပါတယ်။

  • Field name ကို ပြောင်းလဲလိုက်ခြင်း
  • Number ကို string အဖြစ် တိတ်တဆိတ် ပြောင်းလိုက်ခြင်း
  • အမြဲရှိနေတဲ့ value က `null` ပြန်စပြုခြင်း
  • Nested object ထဲ required field အသစ် ပေါ်လာခြင်း

သင့် code က `response.data.customer.email` ကို မစစ်ဘဲ ဖတ်လိုက်ပြီး chain ထဲက link တစ်ခုခု ပျောက်နေရင် (သို့) type မှားနေရင် runtime crash ဖြစ်နိုင်ပါတယ်၊ ဒါမှမဟုတ် ပိုဆိုးတာက silently wrong behavior ဖြစ်ပြီး နောက်မှ corrupted data အဖြစ် ပေါ်လာတတ်ပါတယ်။

အခြေခံ ဆုံးဖြတ်ချက်

သုံးမယ့် field တစ်ခုစီ ရှိနေမရှိနေနဲ့ type ကိုက်ညီမကိုက်ညီကို application ထဲ ထပ်ပို့ခင် စစ်ဆေးပါ။ Fail ဖြစ်ရင် ဘေးကင်းစွာနဲ့ ရှင်းလင်းစွာ fail ဖြစ်စေပါ၊ malformed data ကို တိတ်တဆိတ် ဆက်လွှင့်မပစ်ပါနဲ့။

text
RESPONSE VALIDATION DECISION FLOW
---------------------------------
Response arrives (status 200)
        |
        v
Check required fields exist
Check each field's type matches
        |
        v
     Valid? ------ NO ---> Fail safely: clear error,
        |                  do not use the data
       YES
        |
        v
  Use the validated data
  in the rest of your app

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`validateResponse` က parsed response object တစ်ခုနဲ့ required field name တစ်ခုစီကို expected JavaScript type နဲ့ map လုပ်ထားတဲ့ shape description အသေးလေးတစ်ခုကို လက်ခံပါတယ်။ Shape ကို လိုက်ပတ်ပြီး field ရှိမရှိ၊ null မဟုတ်ဘူးလား၊ type ကိုက်ညီလားကို စစ်ပြီး ပထမတွေ့တာမှာ မရပ်ဘဲ problem အားလုံးကို စုစည်းပါတယ်။

Valid example ဟာ nested customer object ပါတဲ့ charge response တစ်ခုဖြစ်ပြီး shape နဲ့ အတိအကျ ကိုက်ညီလို့ function က `{ valid: true, data: ... }` ကို ပြန်ပေးပါတယ်၊ ဒီအတိုင်း သုံးလို့ ရပါပြီ။

Malformed example ကတော့ provider regression အစစ်တစ်ခုကို simulate လုပ်ထားပါတယ် — `amount` ဟာ string အဖြစ်၊ `status` ဟာ `null` အဖြစ် ရောက်လာပြီး `customer` ကတော့ လုံးဝ ပျောက်နေတယ်။ Function က throw (သို့) crash မဖြစ်ဘဲ problem သုံးခုစလုံးကို တစ်ခါတည်း list လုပ်ပြီး `{ valid: false, errors: [...] }` ကို ပြန်ပေးပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

javascript
const expectedShape = {
  id: "string",
  amount: "number",
  status: "string",
  customer: "object"
};

function validateResponse(response, shape) {
  const errors = [];
  for (const [field, type] of Object.entries(shape)) {
    if (!(field in response)) {
      errors.push(`missing field: ${field}`);
      continue;
    }
    const value = response[field];
    if (value === null) {
      errors.push(`field "${field}" is null, expected ${type}`);
      continue;
    }
    const actualType = Array.isArray(value) ? "array" : typeof value;
    if (actualType !== type) {
      errors.push(`field "${field}" is ${actualType}, expected ${type}`);
    }
  }
  return errors.length === 0 ? { valid: true, data: response } : { valid: false, errors };
}

const validResponse = { id: "ch_123", amount: 5000, status: "succeeded", customer: { id: "cus_1" } };
const malformedResponse = { id: "ch_124", amount: "5000", status: null };

console.log(validateResponse(validResponse, expectedShape));
console.log(validateResponse(malformedResponse, expectedShape));
You should see
validResponse -> { valid: true, data: { id: 'ch_123', amount: 5000, status: 'succeeded', customer: { id: 'cus_1' } } }
malformedResponse -> { valid: false, errors: [
  'field "amount" is string, expected number',
  'field "status" is null, expected string',
  'missing field: customer'
] }
(Error သုံးခုစလုံးကို တစ်ခါတည်း report လုပ်ပေးတာကို သတိပြုပါ — ပထမတွေ့တာနဲ့ ရပ်မသွားပါ)

၅ မိနစ် စမ်းကြည့်

`expectedShape` ကို ကူးပြီး field အသစ် `currency: "string"` ကို ထပ်ထည့်ပါ။ `validResponse` ထဲမှာ `currency` field မထည့်ဘဲ run ကြည့်ပြီး error output ဘယ်လို ပြောင်းလဲသွားလဲ ကြည့်ပါ။

သတိလေးတစ်ချက်

Nested field (`response.data.customer.email`) ကို link တစ်ခုစီ စစ်ဆေးခြင်းမရှိဘဲ တိုက်ရိုက် access လုပ်ခြင်း

Provider ရဲ့ API မပြောင်းလဲဘူးလို့ယူဆပြီး validation ကို လုံးဝ ကျော်သွားခြင်း

typeof - MDNAPI Integration & Webhooks

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Nested field (`response.data.customer.email`) ကို link တစ်ခုစီ စစ်ဆေးခြင်းမရှိဘဲ တိုက်ရိုက် access လုပ်ခြင်း
  • Provider ရဲ့ API မပြောင်းလဲဘူးလို့ယူဆပြီး validation ကို လုံးဝ ကျော်သွားခြင်း
  • API Tutorial (apiguide) ကို မလေ့လာရသေးရင် ဒီ course ကို စမလိုက်ခင် အရင် ပြီးအောင် လေ့လာထားသင့်ပါတယ် — ဒီ course က REST/HTTP/Auth အခြေခံတွေကို ထပ်မသင်ဘဲ webhook, testing, reliability, integration architecture တို့ကိုသာ ဆက်လက် တည်ဆောက်ပါတယ်။

လေ့ကျင့်ခန်း

`expectedShape` ကို ကူးပြီး field အသစ် `currency: "string"` ကို ထပ်ထည့်ပါ။ `validResponse` ထဲမှာ `currency` field မထည့်ဘဲ run ကြည့်ပြီး error output ဘယ်လို ပြောင်းလဲသွားလဲ ကြည့်ပါ။

You'll know it worked when: validResponse -> { valid: true, data: { id: 'ch_123', amount: 5000, status: 'succeeded', customer: { id: 'cus_1' } } } malformedResponse -> { valid: false, errors: [ 'field "amount" is string, expected number', 'field "status" is null, expected string', 'missing field: customer' ] } (Error သုံးခုစလုံးကို တစ်ခါတည်း report လုပ်ပေးတာကို သတိပြုပါ — ပထမတွေ့တာနဲ့ ရပ်မသွားပါ)

Response Validation | Thuta Learning