You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写适配Formly的JSON Schema按feature id匹配configurationData结构

JSON Schema匹配功能对应配置结构及Formly渲染问题

需求说明

现有存储功能列表的JSON对象,每个功能包含3个相同的公共属性,其中configurationData属性的结构随功能不同存在差异,示例如下:

{
  "features": [
    {
       "id": "feature-a",
       "enabled": true,
       "configurationData": {
          "featureAConfigPropertyA": {
             "someProperty": "whatever",
             "anotherProperty": true
          },
          "featureAConfigPropertyB": "some string"
       } 
    },
    {
       "id": "feature-b",
       "enabled": true,
       "configurationData": {
          "featureBConfigArrayPropertyA": ["some string"],
          "featureBConfigPropertyB": [
             {
               "id": "some string",
               "name": "some string",
               "description": "some string",
               "enabled": true
             }
          ]
       } 
    }      
  ]
}

核心诉求:通过JSON Schema实现每个功能的configurationData结构由其父级的feature id值决定。
补充说明:技术层面只要两种configurationData结构都属于feature schema中该属性的合法结构即可,无需严格依赖ID匹配;且configurationData的类型不是任意的,当前场景下给定功能的配置只会是两种类型之一。

应用场景

该Schema需要支持Formly解析生成表单,预期效果如下:

  • 生成由ObjectFieldTypes组成的数组,feature-a和feature-b各对应一项
  • 先渲染三个公共属性对应的Input输入框
  • 渲染到configurationData属性时,自动匹配对应结构的ObjectFieldType渲染

现存问题

  • 不清楚如何用JSON Schema表达上述依赖ID匹配配置结构的逻辑
  • 无法在Formly中使用patternProperties:Formly要求JSON Schema中明确定义所有属性才能渲染对应字段类型,虽然patternProperties是语法合法的Schema,但如果Schema未明确定义对应属性,FormGroup的valueChanges observable返回的模型会直接剔除这些属性,最终返回的结果如下:
{
   "features":[
     {
        "id": "feature-a",
        "enabled": true,
        "configurationData": { } 
     },
     {
        "id": "feature-b",
        "enabled": true,
        "configurationData": { } 
     }    
   ]
}

我已经尝试过if then else语法,但无法确定是Schema编写错误还是Formly暂不支持该能力。

内容的提问来源于stack exchange,提问作者Brandon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:45:10