为什么布尔人有效的JSON模式?

发布于 2025-02-10 02:18:34 字数 413 浏览 1 评论 0原文

我正在看json schema的 meta schemas ,并根据> >核心/验证方言meta-schema json truefalse是有效的JSON Schemas。这是什么意思?

您会看到他们正确验证在这里

I'm looking at JSON schema's meta schemas and according to the Core/Validation Dialect meta-schema the JSON true and false are valid JSON schemas. What is the point of this?

You can see that they validate properly here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

究竟谁懂我的在乎 2025-02-17 02:18:34

的确,一个布尔式架构的全部内容并不是很有用,除了作为占位符(“将来,我们将具有更定义的数据结构;与此同时,所有值都是可以接受的”)。但是模式也可以包含模式,然后我们可以开始表达更有意义的事情。

考虑这样描述数据:

  • 数据的最高级别必须是对象。
  • 该对象必须包括属性“ alpha”和“ beta”。
  • “ alpha”的值必须是字符串。
  • “ beta”的价值可以是任何东西。
  • 可能存在属性“伽马”,其价值可以是任何东西。
  • 不得出现“ Epsilon”属性。
  • 如果存在其他属性,则必须是数字。

我们将以这样的方式定义JSON架构:

{
  "type": "object",
  "required": ["alpha", "beta"],
  "properties": {
    "alpha": {
      "type": "string"
    },
    "beta": true,
    "gamma": true,
    "epsilon": false,
  },
  "additionalProperties": {
    "type": "number"
  }
}

如您所见,在“属性”和“额外的Properties”关键字下,我们有几个子查玛。某些亚schemas包含更多关键字(在这种情况下为“类型”),但是某些亚种只是普通的旧布尔值。他们传达了一些意义:“这种情况总是有效的”或“这种情况总是无效的”。

It's true that a boolean schema, in its entirety, is not terribly useful, except perhaps as a placeholder ("in the future, we will have a more defined data structure; in the meantime, all values are acceptable"). But schemas can also contain schemas, and then we can start to express more meaningful things.

Consider describing data thusly:

  • the top level of the data must be an object.
  • the object must include the properties "alpha" and "beta".
  • the value of "alpha" must be a string.
  • the value of "beta" can be anything.
  • the property "gamma" may exist, and its value can be anything.
  • the property "epsilon" must not be present.
  • if there any other properties present, they must be numbers.

We would define the JSON schema like this:

{
  "type": "object",
  "required": ["alpha", "beta"],
  "properties": {
    "alpha": {
      "type": "string"
    },
    "beta": true,
    "gamma": true,
    "epsilon": false,
  },
  "additionalProperties": {
    "type": "number"
  }
}

Now, as you can see, under the "properties" and "additionalProperties" keywords we have several subschemas. Some of the subschemas contain more keywords ("type" in this case), but some of the subschemas are just plain old booleans. They're conveying something of meaning: "this condition is always valid" or "this condition is always invalid".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文