如何编写模式以约束其中一些属性与一个或多个子模式中的任何一个?

huangapple go评论65阅读模式
英文:

How to write a schema to constrain some of the properties with one/any of the sub-schemas?

问题

name: "range_1"
step: 1
start: 0
stop: 10

name: "range_2"
step: 1
center: 5
span: 5

能否像这样进行验证:

properties:
name:
type: "string"
stop:
type: number
oneOf:
- start:
type: number
step:
type: number
- center:
type: number
span:
type: number

目前我在Python中使用jsonschema,但它抱怨jsonschema.exceptions.SchemaError: <the array in oneOf> is not of type 'object', 'boolean'

只对namestep进行验证,或者对所有可能的键进行验证似乎都可以工作,但对我来说都不够理想。

英文:

Can I validate both

name: &quot;range_1&quot;
step: 1
start: 0
stop: 10

and

name: &quot;range_2&quot;
step: 1
center: 5
span: 5

with something like

properties:
    name:
        type: &quot;string&quot;
    stop:
        type: number
    oneOf:
    -   start:
            type: number
        step:
            type: number
    -   center:
            type: number
        span:
            type: number

For now I am using jsonschema in Python, but it complains jsonschema.exceptions.SchemaError: &lt;the array in oneOf&gt; is not of type &#39;object&#39;, &#39;boolean&#39;.

Validating against name and step only or validating against all possible keys apparently works but they both seem sub-optimal for me.

答案1

得分: 1

oneOf关键字移出properties对象,因为properties对象中的所有内容都被解释为数据中期望的值。

此外,最好添加一个required属性,使这些值成为必需值。最后,如果要确保不接受其他值,可以使用additionalProperties: false。不过,请注意,您必须在oneOf模式中再次重复“父级”属性。有关更多信息,我建议查看此示例

综合考虑,您可以使用以下模式(参见此处的实时示例):

---
properties:
  name:
    type: string
  step:
    type: number
oneOf:
- properties:
    name: true
    step: true
    start:
      type: number
    stop:
      type: number
  required:
  - start
  - stop
  additionalProperties: false
- properties:
    name: true
    step: true
    center:
      type: number
    span:
      type: number
  required:
  - center
  - span
  additionalProperties: false
英文:

You need to move the oneOf keyword out of the properties object as everything in the properties object is interpreted as an expected value in your data.

Additionally, it makes sense to add an required property to make the values mandatory. Finally, if you want to make sure that no other values are excepted, you can use additionalProperties: false. Note though, that you have to repeat the "parent" properties in the oneOf schemas again. For further reading I recommend this example.

Put all together, you could use the following schema (see live example here):

---
properties:
  name:
    type: string
  step:
    type: number
oneOf:
- properties:
    name: true
    step: true
    start:
      type: number
    stop:
      type: number
  required:
  - start
  - stop
  additionalProperties: false
- properties:
    name: true
    step: true
    center:
      type: number
    span:
      type: number
  required:
  - center
  - span
  additionalProperties: false

huangapple
  • 本文由 发表于 2023年2月8日 20:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定