|
| 1 | + # Chalo JSON Schema Pdhe |
| 2 | + |
| 3 | + This repository contains a **comprehensive JSON Schema** designed purely for **educational purposes**. The schema demonstrates nearly every major keyword from the **Draft-07** specification, providing a hands-on resource for understanding how to define, structure, and validate complex JSON data. |
| 4 | + |
| 5 | + --- |
| 6 | + |
| 7 | + ## JSON |
| 8 | + |
| 9 | + JSON Schema is a vocabulary that allows you to **annotate** and **validate** JSON documents. It acts as a blueprint, ensuring that a piece of JSON data adheres to a specified structure, data types, and set of constraints. |
| 10 | + |
| 11 | + ### 🧱 Building Blocks |
| 12 | + |
| 13 | + * A schema is itself a **JSON Object** defined by curly braces (`{}`). |
| 14 | + * It operates on a **key-value pair** basis. |
| 15 | + * **Keywords** (like `$schema`, `type`, `properties`) are used as **keys** to apply validation rules. |
| 16 | + |
| 17 | + --- |
| 18 | + |
| 19 | + ## Keywords |
| 20 | + |
| 21 | + These keywords define the context and identity of the schema: |
| 22 | + |
| 23 | + | Keyword | Type | Purpose | Example | |
| 24 | + | :--- | :--- | :--- | :--- | |
| 25 | + | **`$schema`** | `string` | **Defines the JSON Schema version** (or "dialect") being used. | `"http://json-schema.org/draft-07/schema#"` | |
| 26 | + | **`$id`** | `string` | A **unique identifier (URI)** for the schema. | `"https://example.com/full-manifest.schema.json"` | |
| 27 | + | **`title`** | `string` | A **short, descriptive title** for the schema. | `"This is my first schema"` | |
| 28 | + | **`description`** | `string` | A **detailed explanation** of the schema's purpose. | `"A complex schema..."` | |
| 29 | + | **`$comment`** | `string` | Notes for schema maintainers (**ignored** by validators). | `"This is my schema..."` | |
| 30 | + |
| 31 | + The most crucial validation keyword is **`type`**, which defines the expected data type of the JSON being validated. |
| 32 | + |
| 33 | + * **Syntax:** `"type": "object"` or `"type": ["string", "null"]` |
| 34 | + * **Note:** In our full example, the root instance is explicitly set to `"type": "object"`. |
| 35 | + |
| 36 | + --- |
| 37 | + |
| 38 | + ## Data types |
| 39 | + |
| 40 | + JSON Schema uses validation keywords specific to the type being defined. |
| 41 | + |
| 42 | + ### 1. Object Keywords (`type: "object"`) |
| 43 | + |
| 44 | + | Keyword | Purpose | Example | |
| 45 | + | :--- | :--- | :--- | |
| 46 | + | **`properties`** | Defines the expected fields (keys) in the object, where each value is its own schema. | See `system_id` in the full schema. | |
| 47 | + | **`required`** | An **array of strings** listing the properties that **must** be present. | `"required": ["system_id", "status_check"]` | |
| 48 | + | **`min/maxProperties`**| Restricts the minimum/maximum number of allowed properties. | `"minProperties": 1` | |
| 49 | + | **`additionalProperties`**| If `false`, prohibits any keys not listed in `properties` or `patternProperties`. | `"additionalProperties": false` | |
| 50 | + | **`patternProperties`** | Allows property names to be validated using **Regular Expressions**. | `"^flag_\\d+$"` (matches keys like `flag_1`, `flag_2`, etc.) | |
| 51 | + |
| 52 | + ### 2. Array Keywords (`type: "array"`) |
| 53 | + |
| 54 | + | Keyword | Purpose | Example | |
| 55 | + | :--- | :--- | :--- | |
| 56 | + | **`items`** | The schema that **all** elements in the array must conform to. | `"items": {"type": "string"}` | |
| 57 | + | **`min/maxItems`** | Restricts the size (length) of the array. | `"maxItems": 5` | |
| 58 | + | **`uniqueItems`** | If `true`, every item in the array must be unique. | `"uniqueItems": true` | |
| 59 | + | **`contains`** | Requires that **at least one** item in the array matches the sub-schema. | `{"const": "HIGH_PRIORITY"}` | |
| 60 | + |
| 61 | + ### 3. String Keywords (`type: "string"`) |
| 62 | + |
| 63 | + | Keyword | Purpose | Example | |
| 64 | + | :--- | :--- | :--- | |
| 65 | + | **`minLength/maxLength`**| Restricts the length of the string. | `"minLength": 8` | |
| 66 | + | **`pattern`** | A **regular expression** the string value must match. | `"pattern": "^[a-f0-9]{8,36}$"` | |
| 67 | + | **`format`** | Defines a **semantic constraint** (e.g., date, email, URI, UUID). | `"format": "uuid"` | |
| 68 | + |
| 69 | + ### 4. Numeric Keywords (`type: "number"` or `"integer"`) |
| 70 | + |
| 71 | + | Keyword | Purpose | Example | |
| 72 | + | :--- | :--- | :--- | |
| 73 | + | **`minimum/maximum`**| The inclusive lower/upper bound. | `"minimum": 1` | |
| 74 | + | **`exclusiveMinimum/Maximum`**| The exclusive lower/upper bound (value must be strictly greater/less than). | `"exclusiveMaximum": 11` | |
| 75 | + | **`multipleOf`** | The number must be evenly divisible by this value. | `"multipleOf": 1` | |
| 76 | + |
| 77 | + --- |
| 78 | + |
| 79 | + ## Conditional Structures Present in JSON |
| 80 | + |
| 81 | + These keywords allow for complex rules and schema reuse. |
| 82 | + |
| 83 | + ### Schema Composition |
| 84 | + |
| 85 | + These keywords combine multiple sub-schemas: |
| 86 | + |
| 87 | + * **`allOf`**: The data must satisfy **ALL** listed schemas. |
| 88 | + * **`anyOf`**: The data must satisfy **AT LEAST ONE** listed schema. |
| 89 | + * **`oneOf`**: The data must satisfy **EXACTLY ONE** listed schema. |
| 90 | + * **`not`**: The data must **NOT** satisfy the listed schema. |
| 91 | + |
| 92 | + ### Conditional Logic |
| 93 | + |
| 94 | + The **`if`** / **`then`** / **`else`** keywords apply validation based on a condition: |
| 95 | + |
| 96 | + * **`if`**: The condition (a sub-schema) to check. |
| 97 | + * **`then`**: The schema to apply if the `if` condition is **true**. |
| 98 | + * **`else`**: The schema to apply if the `if` condition is **false**. |
| 99 | + |
| 100 | + ### Schema Reuse |
| 101 | + |
| 102 | + * **`definitions`**: (or `$defs` in newer drafts) An area to define reusable sub-schemas. |
| 103 | + * **`$ref`**: Used to **reference** a schema defined elsewhere, typically within `definitions` (e.g., `#/definitions/Contact`). |
| 104 | + |
| 105 | + --- |
| 106 | + |
| 107 | + ## A detailed schema |
| 108 | + |
| 109 | + For your reference |
| 110 | + |
| 111 | + ```json |
| 112 | + { |
| 113 | + "$schema":"[http://json-schema.org/draft-07/schema#](http://json-schema.org/draft-07/schema#)", |
| 114 | + "$id":"[https://example.com/full-manifest.schema.json](https://example.com/full-manifest.schema.json)", |
| 115 | + "title": "This is my first schema", |
| 116 | + "description":"A complex schema which include complex object validation schema attributes as a key to properly validate things for prompt", |
| 117 | + "$comment": "This is my schema in which i am using all attributes present in json schema", |
| 118 | + |
| 119 | + "type":"object", // The root instance must be an object |
| 120 | + "default":{}, // default value if json is missing |
| 121 | + |
| 122 | + // --- Object-Specific Keywords --- |
| 123 | + "minProperties": 1, |
| 124 | + "maxProperties": 10, |
| 125 | + "required": ["system_id", "status_check"], |
| 126 | + "properties": { |
| 127 | + "system_id": { |
| 128 | + "type": "string", |
| 129 | + "description": "Unique identifier for the system.", |
| 130 | + "minLength": 8, |
| 131 | + "maxLength": 36, |
| 132 | + "pattern": "^[a-f0-9]{8,36}$", |
| 133 | + "format": "uuid" |
| 134 | + }, |
| 135 | + "version": { |
| 136 | + "type": ["number", "null"], |
| 137 | + "description": "The manifest version.", |
| 138 | + "const": 1.0 |
| 139 | + }, |
| 140 | + "status_check": { |
| 141 | + "type": "boolean", |
| 142 | + "readOnly": true, |
| 143 | + "examples": [true] |
| 144 | + }, |
| 145 | + "retry_attempts": { |
| 146 | + "type": "integer", |
| 147 | + "minimum": 1, |
| 148 | + "exclusiveMaximum": 11, |
| 149 | + "multipleOf": 1 |
| 150 | + }, |
| 151 | + "parameters": { |
| 152 | + "type": "array", |
| 153 | + "description": "A list of configuration items.", |
| 154 | + "minItems": 1, |
| 155 | + "maxItems": 5, |
| 156 | + "uniqueItems": true, |
| 157 | + "items": { "type": "string" }, |
| 158 | + "contains": { "const": "HIGH_PRIORITY" } |
| 159 | + }, |
| 160 | + "contact_info": { |
| 161 | + "$ref": "#/definitions/Contact" |
| 162 | + } |
| 163 | + }, |
| 164 | + |
| 165 | + "patternProperties": { |
| 166 | + "^flag_\\d+$": { "type": "boolean" } |
| 167 | + }, |
| 168 | + |
| 169 | + "additionalProperties": false, |
| 170 | + |
| 171 | + // --- Composition Keywords --- |
| 172 | + "allOf": [ {"minProperties": 1} ], |
| 173 | + "anyOf": [ |
| 174 | + {"properties": {"system_id": {"format": "uuid"}}}, |
| 175 | + {"properties": {"system_id": {"format": "hostname"}}} |
| 176 | + ], |
| 177 | + "oneOf": [ |
| 178 | + {"required": ["version"]}, |
| 179 | + {"not": {"required": ["version"]}} |
| 180 | + ], |
| 181 | + "not": { "properties": {"retry_attempts": {"minimum": 100}} }, |
| 182 | + |
| 183 | + // --- Conditional Keywords --- |
| 184 | + "if": { "properties": {"status_check": {"const": true}} }, |
| 185 | + "then": { |
| 186 | + "properties": {"contact_info": {"required": ["email"]}}, |
| 187 | + "required": ["contact_info"] |
| 188 | + }, |
| 189 | + "else": { "properties": {"contact_info": {"required": []}} }, |
| 190 | + |
| 191 | + // --- Structural/Reuse Keywords --- |
| 192 | + "definitions": { |
| 193 | + "Contact": { |
| 194 | + "type": "object", |
| 195 | + "description": "A reusable schema for contact details.", |
| 196 | + "properties": { |
| 197 | + "name": {"type": "string"}, |
| 198 | + "email": {"type": "string", "format": "email", "writeOnly": true}, |
| 199 | + "phone": {"type": ["string", "null"]} |
| 200 | + }, |
| 201 | + "dependentRequired": { "name": ["email"] }, |
| 202 | + "dependentSchemas": { |
| 203 | + "email": { |
| 204 | + "properties": {"phone": {"minLength": 10}}, |
| 205 | + "required": ["phone"] |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + } |
0 commit comments