如果我有一组项目并且这些项目共享一些属性并且也有不同的属性。
- 有没有办法用不同的属性扩展/修补公共属性?
- 我可以使用
$switch
内部对象属性吗?我知道它在根级别外部使用,但是有没有办法在属性内部执行条件?
我试图为此类问题找到任何解决方案,但到目前为止似乎无法解决任何问题。
如果我有一组项目并且这些项目共享一些属性并且也有不同的属性。
$switch
内部对象属性吗?我知道它在根级别外部使用,但是有没有办法在属性内部执行条件?我试图为此类问题找到任何解决方案,但到目前为止似乎无法解决任何问题。
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.
关于您可以在properties
关键字中使用的内容:
“属性”的值必须是一个对象。此对象的每个值都
必须是有效的 JSON 模式。
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-00#section-6.5.4
$switch
但是,它不是 JSON Schema 规范的一部分,因此虽然它在使用 ajv 时可能有效,但它可能不适用于其他任何人。
此外,没有办法用不同的属性“扩展/修补”公共属性。allOf
如果您使用一个对象使用一个对象,$ref
而另一个对象使用您的扩展模式,那么您可以扩展属性。