JSON模式:使用另一个对象中定义的属性值

发布于 2025-01-29 14:55:18 字数 291 浏览 3 评论 0原文

我在JSON模式中有两个属性:
“ a”:{“ type”:“对象”,“属性”:{“ x”:{“ type”:“ boolean”}}}}}
“ b”:{“类型”:“对象”,“属性”:{...}}

b的逻辑中,我需要使用x 要设置b属性值和“必需”:[...]
如何在b中使用x的值?

I have two properties in JSON Schema:
"A": {"type": "object", "properties":{ "X":{"type":"boolean"}}}
"B": {"type": "object", "properties":{...}}

In the logic of B I need to use value of X to set B property values and "required":[...].
How do I use value of X within the B ?

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

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

发布评论

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

评论(2

美人如玉 2025-02-05 14:55:18

您需要在定义包含这两个属性的对象的模式级别上声明规则。然后,您可以使用depententsChemas依赖性重新定制,或如果/然后/else else 关键字来定义诸如“ if< gt;在属性A中的某物,然后<在属性b中”的关键字。

{
  "type": "object",
  "properties": {
    "A": {
      "type": "object",
      "properties":{ "X":{"type":"boolean"}}
    },
    "B": {
      "type": "object",
      "properties":{...}
    }
  },
  "if": {
    "required": ["A"],
    "properties": {
      "A": {
        "required": ["X"],
        "properties": { "X": { "const": true } }
      }
    }
  },
  "then": {
    "required": ["B"],
    "properties": {
      "B": {
        "required": ["Y"],
        "properties": { "Y": { "const": "/A/X is true" } }
      }
    }
  }
}

条件 “”。

You need to declare a rule at the schema level that defines the object that contains these two properties. You can then use the dependentSchemas, dependentRequired, or if/then/else keywords to define rules like "if <something> in property A, then <something> in property B".

https://json-schema.org/understanding-json-schema/reference/conditionals.html

For example:

{
  "type": "object",
  "properties": {
    "A": {
      "type": "object",
      "properties":{ "X":{"type":"boolean"}}
    },
    "B": {
      "type": "object",
      "properties":{...}
    }
  },
  "if": {
    "required": ["A"],
    "properties": {
      "A": {
        "required": ["X"],
        "properties": { "X": { "const": true } }
      }
    }
  },
  "then": {
    "required": ["B"],
    "properties": {
      "B": {
        "required": ["Y"],
        "properties": { "Y": { "const": "/A/X is true" } }
      }
    }
  }
}

This is saying "if property A exists, and has a subproperty X which is true, then there must be a property Y under B which is the string "/A/X is true"".

玩物 2025-02-05 14:55:18

谈到JSON模式的标准规范,您不能在其他位置“使用” JSON实例中的值以进行验证,例如,您要拿一个值并填写必需的与之关键字。

但是,您可以使用if-then-else来定义条件,该使用值来决定验证流中此时应随后应用哪些其他模式。

依赖性Chemas依赖性重新定价关键字集中在属性的存在上。

更多详细信息可用在这里

Speaking about the standard specification of JSON Schema in general, you can't "use" values from the JSON instance at some other place for validation in the meaning that, for example, you take a value and fill a required keyword with it.

However, you can define conditions using if-then-else that use values to decide what other schemas should be applied subsequently at this point in the validation flow.

The dependentSchemas and dependentRequired keywords are focused on the presence of properties.

More details are available here.

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