|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "[EN] VSCode Series: Configuration System 2 - JSON Schema" |
| 4 | +categories: [VSCode, Systems] |
| 5 | +--- |
| 6 | + |
| 7 | +# `IJSONSchema` Interface |
| 8 | + |
| 9 | +> Reference: [JSON Schema Official Website](https://json-schema.org/) |
| 10 | +
|
| 11 | +`IJSONSchema` is a standard interface in Visual Studio Code used to describe the structure and content of JSON data. It provides a set of rules to constrain the format of JSON data. A `schema` can be understood as a collection of rules that define and validate whether incoming JSON data conforms to the expected format. Below is a simple example. |
| 12 | + |
| 13 | +### Data Example |
| 14 | +```typescript |
| 15 | +interface IPerson { |
| 16 | + name: string; |
| 17 | + age: number; |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +### `schema` Example |
| 22 | +```typescript |
| 23 | +let personSchema: IJSONSchema = { |
| 24 | + type: 'object', |
| 25 | + properties: { |
| 26 | + name: { |
| 27 | + type: 'string', |
| 28 | + minLength: 1 |
| 29 | + }, |
| 30 | + age: { |
| 31 | + type: 'number', |
| 32 | + minimum: 0 |
| 33 | + } |
| 34 | + }, |
| 35 | + required: ['name', 'age'] |
| 36 | +}; |
| 37 | +``` |
| 38 | + |
| 39 | +### Valid Data Example |
| 40 | +```typescript |
| 41 | +let userData = { |
| 42 | + "name": "John Doe", |
| 43 | + "age": 30, |
| 44 | + "email": "john@example.com", |
| 45 | + "address": { |
| 46 | + "street": "123 Main St", |
| 47 | + "city": "Springfield", |
| 48 | + "country": "USA" |
| 49 | + }, |
| 50 | + "hobbies": ["Reading", "Traveling", "Swimming"] |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +In VSCode, the `IJSONSchema` interface definition can be found in the `vscode/src/vs/base/common/jsonSchema.ts` file. Here’s a snippet of the interface: |
| 55 | + |
| 56 | +```typescript |
| 57 | +export interface IJSONSchema { |
| 58 | + id?: string; |
| 59 | + $id?: string; |
| 60 | + $schema?: string; |
| 61 | + type?: JSONSchemaType | JSONSchemaType[]; |
| 62 | + title?: string; |
| 63 | + default?: any; |
| 64 | + definitions?: IJSONSchemaMap; |
| 65 | + description?: string; |
| 66 | + properties?: IJSONSchemaMap; |
| 67 | + patternProperties?: IJSONSchemaMap; |
| 68 | + additionalProperties?: boolean | IJSONSchema; |
| 69 | + minProperties?: number; |
| 70 | + maxProperties?: number; |
| 71 | + dependencies?: IJSONSchemaMap | { [prop: string]: string[]; }; |
| 72 | + items?: IJSONSchema | IJSONSchema[]; |
| 73 | + minItems?: number; |
| 74 | + maxItems?: number; |
| 75 | + uniqueItems?: boolean; |
| 76 | + additionalItems?: boolean | IJSONSchema; |
| 77 | + pattern?: string; |
| 78 | + minLength?: number; |
| 79 | + maxLength?: number; |
| 80 | + minimum?: number; |
| 81 | + maximum?: number; |
| 82 | + exclusiveMinimum?: boolean | number; |
| 83 | + exclusiveMaximum?: boolean | number; |
| 84 | + multipleOf?: number; |
| 85 | + required?: string[]; |
| 86 | + $ref?: string; |
| 87 | + anyOf?: IJSONSchema[]; |
| 88 | + allOf?: IJSONSchema[]; |
| 89 | + oneOf?: IJSONSchema[]; |
| 90 | + not?: IJSONSchema; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +The `IJSONSchema` interface is complex because it consolidates attributes for all types. Different data types correspond to different constraints: |
| 95 | + |
| 96 | +- When `type: 'number'`, attributes like `minimum` and `maximum` are used to limit the range of values. |
| 97 | +- When `type: 'string'`, attributes like `minLength` and `maxLength` constrain the length of the string. |
| 98 | +- When `type: 'array'`, attributes like `minItems` and `maxItems` specify the number of items. |
| 99 | + |
| 100 | +Since it doesn’t use an elaborate type system, VSCode’s `IJSONSchema` combines all schema fields into a single structure rather than segregating them by type. While this approach simplifies the interface definition, it makes the interface more verbose. |
| 101 | + |
| 102 | +### Validation Process in VSCode |
| 103 | +> In TypeScript, defining an interface alone does not enable data validation, as TypeScript's type system is erased during compilation, leaving no runtime means to validate data against the schema. |
| 104 | +> Therefore, VSCode includes runtime code to verify that JSON data conforms to the schema. |
| 105 | +
|
| 106 | +Although the `IJSONSchema` interface includes many fields to help developers describe data in a unified format, the specific runtime validation process is not clearly documented in VSCode. |
| 107 | + |
| 108 | +I speculate that validation is handled by different components independently. This means `IJSONSchema` does not provide a centralized validation process but relies on specific implementations to perform the validation. |
| 109 | + |
| 110 | +> In your own project, I recommend implementing a centralized validation process rather than having different systems handle validation manually—unless you have highly specific requirements or performance-sensitive scenarios. |
| 111 | +
|
| 112 | +The only related validation file I found is: |
| 113 | +1. `src/vs/workbench/services/preferences/common/preferencesValidation.ts` |
| 114 | + It seems to validate `IJSONSchema` against provided data when checking preference configurations. |
0 commit comments