Skip to content

Commit d19d2ed

Browse files
committed
[Chore]
1 parent 267c0d4 commit d19d2ed

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

Systems/2024-11-07-VSCode配置系统 2:JSON Schema.md renamed to Systems/2024-11-07-[CN] VSCode系列「系统篇」:配置系统2 - JSON Schema.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
layout: post
3+
title: "[CN] VSCode系列「系统篇」:配置系统2 - JSON Schema"
34
categories: [VSCode, Systems]
45
---
56

@@ -100,10 +101,15 @@ export interface IJSONSchema {
100101
由于没有使用复杂的类型系统,VSCode 的 `IJSONSchema` 将所有的 schema 字段都放在一起,而不是基于类型进行分离。这种方式虽然使得接口定义较为简单,但也导致接口字段较为冗长。
101102

102103
### VSCode的验证过程
103-
`IJSONSchema` 接口中,虽然定义了大量的字段以方便开发者使用统一格式进行数据描述,但具体的runtime的验证过程在VSCode中我并没有找到哪里被明确规定。
104+
> 在typescript中光定义一个Interface当然是无法起到数据验证功能的,因为TypeScript其中的类型在编译期间会进行类型擦除,而runtime时则没有任何runtime手段进行验证数据是否符合schema。
105+
> 因此VSCode也会有相关的runtime代码来检查runtime读取的json数据是否匹配对应的schema。
106+
107+
不过在 `IJSONSchema` 接口中,虽然定义了大量的字段以方便开发者使用统一格式进行数据描述,但具体的runtime的验证过程在VSCode中我并没有找到哪里被明确规定。
104108

105109
我猜测实际的验证是由不同的组件各自负责处理的,这意味着 `IJSONSchema` 并没有提供一个集中化的验证过程,而是依赖于具体实现进行验证。
106110

111+
> 如果在你的项目中,我个人是建议弄一套集中化的验证过程,而不是让各个系统自己负责手动监测。除非有非常特殊的需求/性能敏感的场景。
112+
107113
我唯一能找到的相关验证文件是:
108114
1. `src\vs\workbench\services\preferences\common\preferencesValidation.ts`
109115
貌似是用来验证preference configuration时会检查`IJSONSchema`和给定的数据是否匹配。
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)