【发布时间】:2019-10-04 06:45:40
【问题描述】:
您好,我正在尝试从 JSON 架构创建 JSON 实例。
欢迎打字稿回答,但感谢任何答案。
在 sample-schema.json 文件下方。我想创建一个函数来将模式转换为模式实例。
例如:sample-schema.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"address": {
"type": "object",
"properties": {
"street_1": {
"type": "string"
},
"street_2": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string",
"enum": [
"AL",
"AK",
"AS"
]
},
"zip_code": {
"type": "string"
}
}
},
"birthday": {
"type": "string"
},
"notes": {
"type": "string"
}
},
"required": [
"last_name"
]
}
期望的输出:
{
"first_name": "",
"last_name": "",
"last_name": "",
"address": {
"street_1": "",
"street_2": "",
"city": "",
"state": "",
"zip_code": ""
},
"birthday": "",
"notes": ""
}
在调用函数时,通过传递它必须生成 JSON 实例的模式。我在 Google 上搜索,但所有结果都是从 JSON 到 JSON 模式。
【问题讨论】:
-
粗略浏览一下互联网似乎表明,如果您想通过 JavaScript 编写一些东西,您可能需要自己编写一些东西。我找到了一个可以做到这一点的 Java 工具,但没有你可以自己引入的 JavaScript 库。
-
如果您有任何来源,请提供给我。任何语言都会尝试修改为我的风格。
标签: javascript typescript jsonschema