1

我正在使用json-schema-faker并且我将它与Faker一起使用,它允许我获取随机的“真实”格式值,如电子邮件、图像 url 等。
这个方案运行良好:

{
  "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"
  }
4

1 回答 1

1

当你使用:

"faker": {
  "pattern": "image.city||image.food"
}

后面执行的代码将与此类似:faker.pattern("image.city||image.food")

要解决您的特定情况,您需要将可能的伪造调用变体包装为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

于 2018-02-08T05:59:28.837 回答