【发布时间】:2019-10-01 07:24:23
【问题描述】:
我有一个文件夹“模式”,其中包含不同的 JSON 文件来存储不同的模式。
例如,
/schemas/apple-schema.json
{
"$schema": "http://json-schema.org/draft-06/schema",
"type": "object",
"properties": {
"apple_name": {
"type": "string"
},
"id": {
"type": "integer"
},
"apple_weight": {
"type": "number"
},
"timestamp": {
"type": "string",
"format": "date-time"
},
"required": ["id"]
}
}
/schemas/mango-schema.json
{
"$schema": "http://json-schema.org/draft-06/schema",
"type": "object",
"properties": {
"mango_name": {
"type": "string"
},
"id": {
"type": "integer"
},
"mango_mature": {
"type": "number"
},
"mango_age": {
"type": "number"
},
"mango_timestamp": {
"type": "string",
"format": "date-time"
},
"required": ["id"]
}
}
不同的模式有不同的键。我要验证的内容如下:
所有模式中的键(例如 apple_name、id、timestamp、mango_name、mango_mature、mango_age 等)遵循相同的命名约定(带有下划线的小写:“xxx”或“xxx_yyy”)。
名称中包含“时间戳”的任何键都应采用“日期时间”格式
任何架构都应该存在键“id”。 (所有模式都需要密钥“id”)
是否可以编写一个导入所有 JSON 模式并处理验证的单元测试?
【问题讨论】:
标签: javascript json typescript unit-testing validation