एकसमान API Validation Errors: RFC 9457 और Field Error Contract
Validation errors को लगातार एक जैसे बताने के लिए व्यापक failure class हेतु HTTP status और सुधार योग्य field जानकारी वाला स्थिर machine-readable body दें। RFC 9457 Problem Details एक साझा बाहरी रूप दे सकता है, जिसमें field errors API-defined extensions हों; यह मानक विकल्प है, हर API की बाध्यता नहीं।
Validation errors को लगातार एक जैसे बताने के लिए व्यापक failure class हेतु HTTP status और सुधार योग्य field जानकारी वाला स्थिर machine-readable body दें। RFC 9457 Problem Details एक साझा बाहरी रूप दे सकता है, जिसमें field errors API-defined extensions हों; यह मानक विकल्प है, हर API की बाध्यता नहीं।
यह लेख HTTP JSON API के input validation failures पर है। RFC 9457 (2023), RFC 7807 का स्थान लेता है। 422 का अर्थ है कि server content type और syntax समझता है, पर निर्देशों को संसाधित नहीं कर सकता। मौजूदा API का documented status बिना compatible बदलाव की योजना के न बदलें।
Error contract तय करें
**Failure के प्रकार के अनुसार status चुनें।** unreadable/malformed content, syntax-valid लेकिन semantic रूप से invalid input, authentication/authorization failure और resource conflict को अलग रखें। Semantic रूप से असंसाध्य content के लिए 422 उपयुक्त हो सकता है। 400 या 422 का चुनाव API contract का निर्णय है; एक सुसंगत नीति लिखें।
**स्थिर रूप रखें।** `application/problem+json` का उपयोग करें। `type` समस्या पहचानता है, `title` छोटा स्थिर नाम है, `status` वास्तविक HTTP status से मेल खाता है, `detail` इस घटना की व्याख्या करता है, और `instance` घटना पहचान सकता है। Client को `detail` के मुक्त पाठ को parse नहीं करना चाहिए; branching के लिए `type` या documented fields इस्तेमाल करें।
**Field errors को extension के रूप में परिभाषित करें।** RFC 9457 validation-error सूची का उदाहरण देता है, पर नाम और संरचना हर API तय करती है। यह synthetic उदाहरण JSON Pointer और स्थिर codes उपयोग करता है:
{"type":"https://api.example.com/problems/validation-error",
"title":"कुछ मान अमान्य हैं","status":422,
"detail":"एक या अधिक fields सुधारने होंगे.",
"instance":"/problems/occurrences/01JEXAMPLE",
"errors":[{"pointer":"#/email","code":"invalid_format"},
{"pointer":"#/age","code":"must_be_at_least","minimum":18}]}Content-Type: application/problem+json दें। JSON Pointer या अन्य location रूप, तथा अनेक errors का रूप तय करें; locations वास्तविक request shape से मेल खाएँ।
**Endpoints को document और verify करें।** OpenAPI में 4xx status, media type, schema और extensions लिखें। missing field, गलत format, सीमा से बाहर मान और अनेक errors शामिल करें। status, Content-Type, body shape, location और स्थिर code जाँचें; पुष्टि करें कि framework defaults सभी routes पर समान contract देते हैं।
स्थिरता और सुरक्षा
Codes स्थिर रखें; title और detail display text हैं। सुधार का तरीका बताएँ, लेकिन stack trace, SQL, internal paths, secrets या पूरा submitted data न लौटाएँ। Request ID के माध्यम से निदान को server logs से जोड़ें और संवेदनशील मान redact करें। Accept-Language पाठ का अनुवाद कर सकता है, जबकि status, code, location और conditions समान रहें। जब status या मौजूदा resource format पर्याप्त हो तो Problem Details न थोपें; प्रकाशित contract बदलने से पहले compatibility जाँचें।
अच्छा error contract client को status और स्थिर code से recovery चुनने देता है। Failure categories तय करें, साझा body और field-error extension परिभाषित करें, फिर OpenAPI और tests को उसी से मिलाएँ।\n\n## References\n- RFC 9457: Problem Details for HTTP APIs\n- RFC 9110: HTTP Semantics\n- OpenAPI Specification 3.2.1