1

我正在尝试使用 genson python 库来构建一个 json 模式,然后将在前端使用它来生成动态表单。在这种情况下,我希望前端根据模式值创建一个单选按钮。但我对布尔类型有疑问。例如,这就是我的 json 数据的样子

configuration = {
            "displayType":  {"Opaque":True, "Border":False}
        } #Out of the two options here, only one of them can be true.

这就是我从中创建模式的方式。

 builder = SchemaBuilder(schema_uri="https://json-schema.org/draft/2020-12/schema")
 builder.add_object(configuration)
 schema = builder.to_schema()

生成的架构如下所示,

{
    'type': 'object',
    'properties': {
        'Opaque': {
            'type': 'boolean'
        },
        'Border': {
            'type': 'boolean'
        }
    },
    'required': ['Border', 'Opaque']
}

如上在必填字段中所见,我需要了解有关彼此之间相互关系的信息,但我只有一个必填字段。谁能帮我相应地修改json数据?

4

1 回答 1

0

您可以将其添加到您的架构中:

'not': {
    'properties': {
        'Opaque': { 'const': true },
        'Border': { 'const': true }
    }
}

在伪代码中,即:“如果 Opaque 为真且 Border 为真,则验证为假。”

或者,因为您需要同时设置 Opaque 和 Border,并且它们只能为 true 或 false,您可以使用 if/then/else,如:“如果 Opaque 为 true,则 Border 为 false,否则 Border 为 true” :

'if': {
    'properties': {
        'Opaque': { const: true }
    }
},
'then': {
    'properties': {
        'Border': { const: false }
    }
},
'else': {
    'properties': {
        'Border': { const: true }
    }
}

您可以在此处阅读有关向架构添加条件的更多信息:https ://json-schema.org/understanding-json-schema/reference/conditionals.html

于 2022-03-02T20:17:24.430 回答