API contracts को document करने, JSON payloads validate करने, और tests/demos के लिए realistic mock data generate करने के लिए JSON Schema का उपयोग करें।
स्टेप 1 – एक real sample से शुरू करें
- पहले formatter में real API response, request payload, या config JSON paste करें।
- Schema drift से बचने के लिए प्रति endpoint एक canonical JSON example रखें।
स्टेप 2 – baseline schema generate करें
- Schema Generator से types, required fields और nested structure infer करें।
- Production के लिए descriptions, formats और constraints (min/max, patterns) refine करें।
स्टेप 3 – schema के विरुद्ध JSON validate करें
- Schema और real payloads को Schema Validator में paste करें।
- Errors fix करें—या तो JSON sample (bugs) अपडेट करें या schema (contract changes) अपडेट करें।
स्टेप 4 – testing के लिए mock data generate करें
- Mock Generator खोलें और schema के अनुसार realistic sample payloads बनाएं।
- Seed + batch size से test data को reproducible और scalable बनाएं।
स्टेप 5 – share और reuse करें
- Schemas को version control में रखें और API docs में link करें।
- Stable JSON samples से typed code (TypeScript/Java/etc.) generate करें।
JSON Schema features के बारे में महत्वपूर्ण नोट
- कुछ schemas advanced keywords पर निर्भर करते हैं जैसे
$ref, anyOf, oneOf, और allOf. - Validators drafts और keywords को अलग-अलग स्तर तक support करते हैं; strict compliance के लिए CI में full JSON Schema validator चलाएँ।
उदाहरण: JSON → JSON Schema (simplified)
// JSON input
{
"id": 1,
"name": "Maeve Winters",
"active": true,
"tags": ["developer", "backend"]
}
// Generated schema (example)
{
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"active": { "type": "boolean" },
"tags": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["id", "name", "active", "tags"]
}