Ajvでネストしたオブジェクトと配列のschemaをつくる時に迷ったのでメモしておきます。
こういうschemaにしたい。
1
2
3
4
5
6
7
8
|
{
"id": 123,
"info": {
"name": "taro",
"age": 20
},
"hobby": ["game", "sports"]
}
|
わかれば単純! こうする。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
interface Data {
id: number;
info: object;
hobby: Array<string>;
}
const schema: JSONSchemaType<Data> = {
type: "object",
properties: {
id: {type: "number"},
info: {
type: "object",
properties: {
name: {type: "string"},
age: {type: "number"}
}
},
hobby: {
type: "array",
items: {
type: "string"
}
}
},
}
|
See Also