[]
公共模式描述了 SpreadJS 的 JSON 数据格式,使 SpreadJS 的 JSON 数据清晰易懂,对人类可读文档和机器可读文档都很有用,并且还提供了完整的结构验证功能,这对于使用代码生成 SpreadJS 的 JSON 数据、进行自动化测试以及验证 JSON 数据都很有帮助。有关当前完整的模式列表,请参阅 SpreadJS Schema。
有关 JSON 模式的更多信息,请访问 json-schema.org 网站。
JSON 模式文档可用于检查 JSON 数据是否有效。你也可以根据 JSON 模式创建有效的 JSON 数据。
以下是一个 JSON 模式的基本示例:
{
"title": "LineBorder",
"description": "Indicates the color of the border line. Use a known color name or HEX style color value. The default value is black.",
"type": "object",
"properties": {
"color": {
"type": "string",
"default": "black"
},
"style": {
"$ref": "#/definitions/LineStyle",
"default": 0
}
}
}有关使用主题创建验证器的信息,请参阅此网站 http://json-schema.org/implementations.html。
你可以使用 SpreadJS 的 JSON 模式文档来验证 SpreadJS 的 JSON 数据。
以下是使用 json-schema-validator 验证 SpreadJS JSON 数据的步骤:
验证部分 JSON 数据。
例如,你有两个 “NameInfo” 的 JSON 数据,并且你想知道它们是否都是有效的 JSON 数据。
{
"name": "name1",
"row": 4,
"column": 3,
"formula": "=SUM(A1,A3)"
}{
"name": "name1",
"row": "4",
"column": 3,
"formula": "=SUM(A1,A3)"
}你可以按如下方式获取 “NameInfo” 的 JSON 模式文档:
{
"title": "NameInfo",
"description": "Represents a custom named expression that can be used by formulas.",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"row": {
"type": "integer",
"minimum": 0
},
"col": {
"type": "integer",
"minimum": 0
},
"formula": {
"type": "string"
},
"comment": {
"type": "string"
}
}
}使用 JSON 模式验证 JSON 数据。“jsonData2” 是无效的,其属性 “row” 的类型应该是 “integer”(整数)。
一个 JSON 模式引用另一个 JSON 模式。例如,以下 JSON 模式引用了另一个 JSON 模式 “LineStyle”。
{
"title": "LineBorder",
"description": "Indicates the color of the border line. Use a known color name or HEX style color value. The default value is black.",
"type": "object",
"properties": {
"color": {
"type": "string",
"default": "black"
},
"style": {
"$ref": "#/definitions/LineStyle",
"default": "LineStyle.empty"
}
}
}如果你想验证 “LineBorder” 的 JSON 数据,请使用完整的 “LineBorder” JSON 模式。
{
"title": "LineBorder",
"description": "Indicates the color of the border line. Use a known color name or HEX style color value. The default value is black.",
"type": "object",
"properties": {
"color": {
"type": "string",
"default": "black"
},
"style": {
"$ref": "#/definitions/LineStyle",
"default": "LineStyle.empty"
}
},
"definitions": {
"LineStyle": {
"title": "LineStyle",
"description": "Specifies the line drawing style for the border. empty:0,thin:1,medium:2,dashed:3,dotted:4,thick:5,double:6,hair:7,mediumDashed:8,dashDot:9,mediumDashDot:10,dashDotDot:11,mediumDashDotDot:12,slantedDashDot:13.",
"enum": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
]
}
}
}验证完整的 JSON 数据。你可以使用完整版本的 JSON 模式来验证 SpreadJS 的完整版本 JSON 数据。