1

如果我有一组项目并且这些项目共享一些属性并且也有不同的属性。

  1. 有没有办法用不同的属性扩展/修补公共属性?
  2. 我可以使用$switch内部对象属性吗?我知道它在根级别外部使用,但是有没有办法在属性内部执行条件?

我试图为此类问题找到任何解决方案,但到目前为止似乎无法解决任何问题。

4

2 回答 2

0

What I finally managed to do is as follows:

items: {
    type: 'object',
    oneOf: [
        { $ref: '#/definitions/button' },
        { $ref: '#/definitions/select' },
    ],

    properties: {
        name: {
            $ref:`${DEFINITIONS_SCHEMA}#/definitions/non_empty_string`,
           // Normal object properties
        },
    }
}

The properties contain the normal object properties. The definitions in either button or select begins with properties as follow:

button: {
    properties:{
    }
}

When the schema is complied, the would be now two properties object inside of items. AJV merges them into a single properties object, appearing as if all were written together from the beginning.

I have written tests to confirm the previous.

于 2018-04-10T10:33:21.103 回答
0

关于您可以在properties关键字中使用的内容:

“属性”的值必须是一个对象。此对象的每个值都
必须是有效的 JSON 模式。

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-00#section-6.5.4

$switch但是,它不是 JSON Schema 规范的一部分,因此虽然它在使用 ajv 时可能有效,但它可能不适用于其他任何人。

此外,没有办法用不同的属性“扩展/修补”公共属性。allOf如果您使用一个对象使用一个对象,$ref而另一个对象使用您的扩展模式,那么您可以扩展属性。

于 2018-04-09T08:16:31.403 回答