【问题标题】:Update Field Based on another Field in Json Schema根据 Json Schema 中的另一个字段更新字段
【发布时间】:2021-08-10 00:30:47
【问题描述】:

如何根据 React Json Schema 中另一个字段的变化来更新一个字段?下面是我的架构对象。

为了简化用例,假设我将“国家/地区”下拉字段默认设置为“加拿大”,“货币”文本字段为空。如果国家/地区发生变化,货币文本字段应显示新元。

{
    type: 'object',
    properties: {
        country: {
            title: 'Country',
            type: 'string',
            enum: ['Canada', 'Singapore', ..., 'United States'],
            default: 'Canada',
        },
        currency: {
            type: 'string',
            default: '',
        },
    },
    dependencies: {
        country: {
            properties: {
                currency: {
                    const: 'SG',
                },
            },
        },
    },
}

【问题讨论】:

    标签: jsonschema react-jsonschema-forms


    【解决方案1】:

    在伪代码中,“如果属性国家存在且其值为加拿大,则货币为加元;如果属性国家存在且其值为 SG...”

    https://json-schema.org/understanding-json-schema/reference/conditionals.html#if-then-else

    因此:

    {
      "type": "object",
      "required": [ "countries", "currency" ],
      "allOf": [
        {
          "if": { "properties": { "countries": { "const": "Canada" } },
          "else": { "properties": { "currency": { "const": "CAD" } } },
        },
        {
          "if": { "properties": { "countries": { "const": "Singapore" } },
          "else": { "properties": { "currency": { "const": "SG" } } },
        },
        ...
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多