我正在使用json-schema-faker我将它与 Faker 一起使用这使我能够获得随机的“真实”格式值,例如电子邮件、图像网址等。
这个方案效果很好:
{
"type": "object",
"properties": {
"myPattern":{
"type": "string",
"pattern": "pattern1||pattern2"
},
"image": {
"type": "string",
"faker": "image.city"
}
},
"required": [
"myPattern",
"image"
]
}
但我真正想要的是faker
将获得像属性myPattern
所获得的模式。
我尝试了一些变体,但没有任何效果。
我尝试过的一些语法:
“faker”:“image.city||image.food”
"faker": {
"fake": {
"pattern": "image.city||image.food"
}
}
-
"faker": {
"pattern": "image.city||image.food"
}
最佳答案
当您使用时:
"faker": {
"pattern": "image.city||image.food"
}
后面执行的代码将类似于:faker.pattern("image.city||image.food")
要解决您的具体情况,您需要将 faker 调用的可能变体包装为 oneOf
:
{
"type": "object",
"properties": {
"myPattern":{
"type": "string",
"pattern": "pattern1||pattern2"
},
"image": {
"type": "string",
"oneOf": [
{ "faker": "image.city" },
{ "faker": "image.food" }
]
}
},
"required": [
"myPattern",
"image"
]
}
现在似乎可以正常工作:http://json-schema-faker.js.org/#gist/9c5cb08965aeeb46b11ca6856251aa80/0.4.3
关于带有假模式的 json schema faker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46526997/