Rog*_*llo 8 schema dependencies json schematron
这是一个JSON实例,显示会议的开始时间和结束时间:
{
"start time": "2015-02-19T08:00:00Z",
"end time": "2015-02-19T09:00:00Z"
}
Run Code Online (Sandbox Code Playgroud)
我可以使用JSON Schema指定该实例的结构:实例必须包含具有"start time"属性和"end time"属性的对象,并且每个属性必须是日期时格式化的字符串.请参阅下面的JSON模式.但我无法指明的是:会议必须在会议结束前开始.也就是说,"开始时间"的值必须小于"结束时间"的值.有些人将这种数据依赖性称为共同约束.在XML世界中,有一种用于表达共同约束的精彩,简单的技术:Schematron.我想知道JSON世界中是否有相同的技术?你会用什么来声明性地描述"开始时间"和"结束时间"之间的关系?(注意:用某种编程语言编写代码并不是我所说的"声明性地描述关系".我正在寻找一种声明方法来描述JSON文档中存在的数据依赖关系,而不是程序代码.)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"meeting": {
"type": "object",
"properties": {
"start time": { "type": "string", "format": "date-time"},
"end time": { "type": "string", "format": "date-time"}
},
"required": [ "start time", "end time" ],
"additionalProperties": false
}
},
"$ref": "#/definitions/meeting"
}
Run Code Online (Sandbox Code Playgroud)
是的。有一个基于Schematron 的JSON 语义验证器,位于: https: //www.npmjs.com/package/jsontron
它实现了Schematron 的“模式”、“阶段”、“规则”、“断言”和报告功能。
以下是通过验证器运行开始时间和结束时间的原始示例:
good_time.json文件内容:
{
"starttime": "2015-02-19T08:00:00Z",
"endtime": "2015-02-19T09:00:00Z"
}
Run Code Online (Sandbox Code Playgroud)
bad_time.json文件内容:
{
"starttime": "2015-02-19T09:00:00Z",
"endtime": "2015-02-19T08:00:00Z"
}
Run Code Online (Sandbox Code Playgroud)
Schematron 规则文件meet-times-rules.json片段:
"rule":[
{
"context": "$",
"assert":[
{
"id":"start_stop_meeting_chec",
"test":"jp.query(contextNode, '$..starttime') < jp.query(contextNode, '$..endtime')",
"message": "Meeting cannot end before it starts"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
当使用正确的示例运行时:
$jsontron\bin>node JSONValidator -i ./good_time.json -r ./meeting-times-rules.json
Run Code Online (Sandbox Code Playgroud)
输出是:
Starting Semantic Validation .........
Parsing Pattern: Meetingtimings
1 Pattern(s) Requested. 1 Pattern(s) Processed. 0 Pattern(s) Ignored.
**** THIS INSTANCE IS SEMANTICALLY VALID ****
Completed Semantic Validation .........
Run Code Online (Sandbox Code Playgroud)
当运行错误数据示例时。输出是:
$jsontron\bin>node JSONValidator -i ./bad_time.json -r ./meeting-times-rules.json
Starting Semantic Validation .........
Parsing Pattern: Meetingtimings
1 Pattern(s) Requested. 1 Pattern(s) Processed. 0 Pattern(s) Ignored.
**** THIS INSTANCE CONTAINS SEMANTIC VALIDATION ISSUES. PLEASE SEE FULL REPORT BY ENABLING DEBUG WITH -d OPTION ****
Completed Semantic Validation .........
Run Code Online (Sandbox Code Playgroud)
带有调试选项的消息是:
...validation failed...
message: 'Meeting cannot end before it starts'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1263 次 |
| 最近记录: |