在 json-schema faker 中生成固定值

Generate fixed value in json-schema faker

我试图在一个对象上生成一个类型 属性,它有一个默认值,我阅读了文档,没有发现任何与给 属性.[=15 赋予固定值相关的内容=]

您可以在此处的测试器中查看 link:Json-Schema-faker

这是我放入测试 faker 的代码:

{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "default": "testModule"
    },
    "name": {
      "type": "string",
      "faker": "name.findName"
    },
    "email": {
      "type": "string",
      "faker": "internet.email"
    }
  },
  "required": [
    "type",
    "name",
    "email"
  ]
}

我得到的结果:

{
  "type": "elit et aliqua",
  "name": "velit al",
  "email": "dolor ea in"
}

我想要的结果:

{
  "type": "TestModule",
  "name": "velit al",
  "email": "dolor ea in"
}

感谢您的帮助!

浸信会

我发现生成此类结果的唯一方法是使用具有单个值的枚举。

你的情况:

{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "testModule"
      ]
    },
    "name": {
      "type": "string",
      "faker": "name.findName"
    },
    "email": {
      "type": "string",
      "faker": "internet.email"
    }
  },
  "required": [
    "type",
    "name",
    "email"
  ]
}

希望对您有所帮助。

从 JSON-schema-faker 的 0.4.7 版开始,有选项 useDefaultValue(但默认禁用)正是这样做的:如果 default 字段带有提供了非空值,JSF 将在生成的 JSON 中使用该值,而不是生成随机值。

https://github.com/json-schema-faker/json-schema-faker/blob/v0.4.7/lib/index.js#L930-L932

自版本 0.5.0-rc.17 起已正确记录:https://github.com/json-schema-faker/json-schema-faker/tree/v0.5.0-rc17/docs#available-options

useDefaultValue — If enabled, it will return the default value if present (default: false)

为了完整性,还有 useExamplesValue 选项,它同样在提供的 examples 中随机选择一个值,如果有的话:

useExamplesValue — If enabled, it will return a random value from examples if they're present (default: false)

在 JSF 演示网站中,您可以使用左上角的“选项”按钮激活这些选项。