基于JSON架构中的另一个字段更新字段 [英] Update Field Based on another Field in Json Schema

查看:8
本文介绍了基于JSON架构中的另一个字段更新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{
    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',
                },
            },
        },
    },
}

推荐答案

伪代码,如果属性Country存在且其值为加拿大,则货币为加元;如果属性Country存在且其值为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" } } },
    },
    ...
  ]
}

这篇关于基于JSON架构中的另一个字段更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆