在task.json中“必需”可以有表达式吗?

问题描述

Task.json中,可以为每个输入字段保留必填字段。这将验证该字段是否为必填字段。

如果上述字段的值为“ ABCD”,我想创建一个字段required = true,如果上面的字段值为其他值,我想创建一个字段required = false。我该如何实现?在两种情况下,我都必须显示该字段。

我可以添加要评估的表达式

 {
    "name": "path","type": "string","label": "Path","required": "false","validation": {          
      "expression":"isMatch(value,'^$|\\.(?i:)(?:png|jpeg)$','IgnoreCase')","message": "Choose a valid file."
    },"helpMarkDown": "Provide a path"
  },

解决方法

我们需要在task.json文件中定义字段1的值,然后选择字段1的值来配置字段值,我们可以参考power shell task来做到这一点。

Task.json示例:

 "inputs": [
        {
            "name": "targetPath1","type": "radio","label": "targetPath","required": false,"defaultValue": "filePath","helpMarkDown": "Target script type: File Path or path","options": {
                "filePath": "File Path","path": "Path"
            }
        },{
            "name": "filePath","type": "filePath","label": "Script Path","visibleRule": "targetType = filePath","required": true,"defaultValue": "","helpMarkDown": ""
        },{
            "name": "path","label": "Path","helpMarkDown": ""
        }
        {
            "name": "path1","label": "Path1","helpMarkDown": ""
        }
]

然后我们可以在task.ts文件中使用它并设置表达式。

此外,如果您不想更改字段2的名称,我们可以在不同的json文件中定义相同的字段2,并在task.ts字段中使用它。

,

尚未将表达式添加到必填字段。可以吗?怎么可能。

我的替代解决方案是让两个不同的输入条目具有相同的标签并且彼此相对。因此,在代码中,在访问用户输入之前,我应该具有相同的visibleRule条件要检查。 示例:

"inputs": [
    {
        "name": "targetPath1","options": {
            "filePath": "File Path","path": "Path"
         }
    },{
        "name": "filePath1","helpMarkDown": "If the user selects the "File Path" above then Script Path will be a mandatory field"
    },{
        "name": "filePath2","visibleRule": "targetType = path","helpMarkDown": "If the user selects the "File Path" above then Script Path will be a optional field"         
    }
]

在代码中访问这些字段时。

var path;
var targetPath = tl.getInput("targetPath1");
If(targetPath == "filePath")
path = tl.getInput("filePath1");
If(targetPath == "path")
path = tl.getInput("filePath2");