json架构faker与假模式

时间:2017-10-02 13:59:40

标签: json faker json-schema-faker

我正在使用JLS14.14.2,并且我将其与json-schema-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"
  }

1 个答案:

答案 0 :(得分:1)

使用时:

"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