JSON Schema सत्यापन क्या है?
JSON Schema सत्यापन वह प्रक्रिया है जिसमें जाँचा जाता है कि कोई JSON दस्तावेज़ एक JSON Schema में परिभाषित संरचनात्मक और मान-संबंधी बाधाओं के अनुरूप है या नहीं। Schema स्वयं एक JSON दस्तावेज़ होता है जो आपके डेटा की अपेक्षित संरचना का वर्णन करता है: कौन सी properties आवश्यक हैं, उनके प्रकार क्या होने चाहिए, संख्याओं की अनुमत सीमाएँ, string patterns, array की लंबाइयाँ, और अन्य बाधाएँ। JSON Schema विशिष्टता json-schema.org पर maintain होती है और IETF drafts की श्रृंखला के रूप में प्रकाशित होती है — Draft 7 और Draft 2020-12 सबसे व्यापक रूप से अपनाए गए हैं।
जहाँ सामान्य JSON सत्यापन केवल सिंटैक्स जाँचता है (क्या brackets सही हैं? क्या strings quoted हैं?), schema सत्यापन इससे आगे जाता है। यह ऐसे प्रश्नों के उत्तर देता है जैसे: क्या इस API के प्रतिक्रिया में एक "id" फ़ील्ड है जो हमेशा integer होती है? क्या "status" फ़ील्ड तीन अनुमत मानों में से एक है? क्या nested ऑब्जेक्ट सही आकार के हैं? इस प्रकार की संरचनात्मक जाँच उन त्रुटियों को पकड़ती है जिन्हें सिंटैक्स जाँच पूरी तरह चूक जाती है — क्योंकि सिंटैक्स की दृष्टि से वैध JSON आपके एप्लिकेशन के लिए अर्थगत रूप से गलत हो सकता है।
JSON Schema का उपयोग OpenAPI/Swagger परिभाषाओं, कॉन्फ़िगरेशन फ़ाइल सत्यापन, फ़ॉर्म सत्यापन, डेटाबेस document बाधाओं और स्वचालित परीक्षण में किया जाता है। Ajv (JavaScript), jsonschema (Python), और check-jsonschema (CLI) जैसे tools विशिष्टता को लागू करते हैं ताकि आप प्रोग्रामेटिक रूप से डेटा सत्यापित कर सकें। यह ऑनलाइन सत्यापक आपको schema और डेटा दस्तावेज़ दोनों चिपकाने देता है और कोई लाइब्रेरी इंस्टॉल किए बिना तुरंत अनुरूपता जाँचता है।
ऑनलाइन JSON Schema Validator का उपयोग क्यों करें?
Schemas लिखना और सत्यापन त्रुटियों को हाथ से डीबग करना धीमा है। एक ऑनलाइन JSON Schema Validator आपको तत्काल प्रतिक्रिया देता है कि आपका डेटा आपके schema से मेल खाता है या नहीं, साथ ही स्पष्ट त्रुटि संदेश भी जो सटीक वह property बताते हैं जो विफल हुई।
JSON Schema सत्यापन के उपयोग के मामले
JSON Schema Keywords संदर्भ
एक JSON Schema keywords से बना होता है जो प्रत्येक सत्यापित डेटा पर एक बाधा लागू करता है। नीचे दी गई तालिका Draft 7 और बाद के संस्करणों में सबसे अधिक उपयोग किए जाने वाले keywords सूचीबद्ध करती है। प्रत्येक keyword को उसी schema ऑब्जेक्ट में अन्य keywords के साथ जोड़ा जा सकता है।
| Keyword | उद्देश्य | उदाहरण |
|---|---|---|
| type | Restricts the data type | "type": "string" |
| properties | Defines expected object keys and their schemas | "properties": { "name": { "type": "string" } } |
| required | Lists mandatory properties | "required": ["id", "name"] |
| items | Schema for array elements | "items": { "type": "number" } |
| enum | Restricts value to a fixed set | "enum": ["active", "inactive"] |
| pattern | Regex constraint on strings | "pattern": "^[A-Z]{2}\\d{4}$" |
| minimum / maximum | Numeric range bounds | "minimum": 0, "maximum": 100 |
| minLength / maxLength | String length bounds | "minLength": 1, "maxLength": 255 |
| $ref | Reuses another schema by URI | "$ref": "#/$defs/address" |
| additionalProperties | Controls extra keys in objects | "additionalProperties": false |
| anyOf / oneOf / allOf | Combines multiple schemas logically | "anyOf": [{ "type": "string" }, { "type": "null" }] |
| if / then / else | Conditional schema application | "if": { "properties": { "type": { "const": "email" } } } |
JSON Schema Draft तुलना: Draft 7 बनाम 2019-09 बनाम 2020-12
JSON Schema कई draft संस्करणों से होकर गुज़रा है। Draft 7 (2018 में प्रकाशित) tooling में सबसे व्यापक रूप से समर्थित है। Draft 2019-09 ने $defs (definitions की जगह), unevaluatedProperties, और $recursiveRef प्रस्तुत किए। Draft 2020-12 (नवीनतम स्थिर रिलीज़) ने $recursiveRef को $dynamicRef से बदला और tuple सत्यापन के लिए prefixItems प्रस्तुत किया। draft चुनते समय, जाँचें कि आपकी validation library उसे समर्थित करती है। Ajv तीनों drafts का समर्थन करता है। Python की jsonschema library संस्करण 4.0 से 2020-12 तक का समर्थन करती है।
| विशेषता | Draft 7 | Draft 2019-09 | Draft 2020-12 |
|---|---|---|---|
| $schema URI | draft-07/schema# | 2019-09/schema | 2020-12/schema |
| if / then / else | Yes | Yes | Yes |
| $defs (definitions) | definitions | $defs | $defs |
| $ref alongside keys | No (sibling ignored) | Yes | Yes |
| $dynamicRef | No | No ($recursiveRef) | Yes |
| prefixItems | No (use items array) | No (use items array) | Yes |
| unevaluatedProperties | No | Yes | Yes |
| $vocabulary | No | Yes | Yes |
Code उदाहरण
ये उदाहरण दिखाते हैं कि JSON को प्रोग्रामेटिक रूप से किसी schema के विरुद्ध कैसे सत्यापित करें। प्रत्येक अपने language ecosystem की एक popular लाइब्रेरी का उपयोग करता है।
import Ajv from 'ajv';
const ajv = new Ajv();
const schema = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
age: { type: 'integer', minimum: 0 },
email: { type: 'string', format: 'email' }
},
required: ['name', 'email'],
additionalProperties: false
};
const data = { name: 'Alice', age: 30, email: 'alice@example.com' };
const validate = ajv.compile(schema);
const valid = validate(data);
console.log(valid); // → true
console.log(validate.errors); // → null
// Invalid data — missing required "email"
validate({ name: 'Bob', age: 25 });
console.log(validate.errors);
// → [{ instancePath: '', keyword: 'required', params: { missingProperty: 'email' } }]from jsonschema import validate, ValidationError
schema = {
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 1},
"age": {"type": "integer", "minimum": 0},
"tags": {
"type": "array",
"items": {"type": "string"},
"uniqueItems": True
}
},
"required": ["name"]
}
# Valid data
validate(instance={"name": "Alice", "age": 30, "tags": ["admin"]}, schema=schema)
# → No exception raised
# Invalid data — wrong type for "age"
try:
validate(instance={"name": "Alice", "age": "thirty"}, schema=schema)
except ValidationError as e:
print(e.message)
# → 'thirty' is not of type 'integer'
print(e.json_path)
# → $.agepackage main
import (
"fmt"
"strings"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func main() {
schemaJSON := `{
"type": "object",
"properties": {
"id": { "type": "integer" },
"status": { "enum": ["active", "inactive"] }
},
"required": ["id", "status"]
}`
compiler := jsonschema.NewCompiler()
compiler.AddResource("schema.json", strings.NewReader(schemaJSON))
schema, _ := compiler.Compile("schema.json")
// Valid data
data := map[string]interface{}{"id": 1, "status": "active"}
err := schema.Validate(data)
fmt.Println(err) // → <nil>
// Invalid — missing "status"
bad := map[string]interface{}{"id": 2}
err = schema.Validate(bad)
fmt.Println(err) // → validation failed: missing properties: 'status'
}# Install via pip pip install check-jsonschema # Validate a file against a schema check-jsonschema --schemafile schema.json data.json # → ok -- validation done # Validate against a remote schema (e.g., GitHub Actions workflow) check-jsonschema --builtin-schema vendor.github-workflows my-workflow.yml # Validate multiple files at once check-jsonschema --schemafile schema.json file1.json file2.json file3.json