【问题标题】:Using jsonPath property in json schema returns a random string instead of the value in the given path在 json 模式中使用 jsonPath 属性返回一个随机字符串而不是给定路径中的值
【发布时间】:2019-07-25 23:32:01
【问题描述】:

从我的previous unresolved issue 继续,我正在使用json-schema-fakerjson-server,我目前正在尝试重用我的 json 架构中的一些数据。

我在 Github 的 this 页面中找到了属性:jsonPath,它准确地解释了我想要做什么以及我的问题是什么。

我正在尝试从生成这些数据的 JavaScript 应用程序以及我使用的库 json-schema-faker site 测试此属性。使用上述任何一种方式使用此属性,返回一个随机字符串而不是我所指的 id ("jsonPath": "$..properties.test.items.properties.id")

使用this 站点进行验证不会导致任何问题,并且使用jsonPath 可以正确获取我尝试重复使用的值。

我应该在我的 JavaScript 代码/模拟数据生成器中导入什么,或者我想要做的事情可能不受 json-schema 版本的支持?

我尝试使用的一些路径是:

$..id $..test.items.properties.id $..test.id

这是我的 json 架构:

{
  "title": "teest",
  "type": "object",
  "required": [
    "test"
  ],
  "properties": {
    "test": {
      "type": "array",
      "minItems": 1,
      "maxItems": 3,
      "uniqueItems": true,
      "items": {
        "type": "object",
        "required": [
          "id",
          "samples"
        ],
        "properties": {
          "id": {
            "type": "string",
            "enum": [
              "1,",
              "2",
              "3"
            ]
          },
          "samples": {
            "type": "array",
            "uniqueItems": true,
            "items": {
              "type": "object",
              "properties": {
                "test2": {
                  "type": "string",
                  "jsonPath": "$..properties.test.items.properties.id"
                }
              }
            }
          }
        }
      }
    }
  }
}

如果我在网上找不到任何帮助,我将不胜感激,而且我真的不想使用硬编码值来模拟这些数据。

【问题讨论】:

  • 我不确定jsonPath 在这个库的上下文中做了什么。它不是标准的 JSON Schema 关键字。您是否尝试过在 json-schema-faker GitHub 存储库上提出问题?您肯定会在那里找到更多了解该图书馆的人。
  • 我想我应该重新打开我在此处发布的 github 问题,或者按照您的建议打开一个新问题。感谢您的回复。

标签: javascript json mocking jsonschema faker


【解决方案1】:

也许您已经找到了问题的解决方案,但如果其他人想知道解决方案:

删除jsonPath之前的type,而不是拥有

...
"items": {
    "type": "object",
    "properties": {
        "test2": {
            "type": "string", // Remove this line
            "jsonPath": "$..properties.test.items.properties.id"
        }
    }
}
...

将架构更改为

...
"items": {
    "type": "object",
    "properties": {
        "test2": {
            "jsonPath": "$..properties.test.items.properties.id"
        }
    }
}
...

json 模式伪造库不知道您尝试引用的对象的类型。因此,如果您提供一个类型,则 json schmea faker 将始终使用指定的随机生成的类型覆盖您的相对值。 我也为此苦苦挣扎,因为没有提供该库的文档(大部分情况下)并且不幸的是不够详细。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2020-09-29
    • 2011-10-24
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多