Search code examples
azure-devopsazure-pipelinesazure-pipelines-build-taskazure-pipelines-tasks

Azdo custom task extension definition of string input with a regular expression doesn't work


I have an Azure custom task implemented with Typescript with a task.json containing a string input which is supposed to get a semantic version:

{
  "name": "version",
  "type": "string",
  "required": true,
  "label": "Version",
  "defaultValue": "",
  "helpMarkDown": "",
  "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
},

Even though the regex for the version is defined (and the regex itself is correct and taken from the semantic version's official docs), the user can still enter whatever string he wants with no limitation and no error message is shown.

How do I make the input show an error message when the user enters an input which does not match the regular expression?


Solution

  • You need to use the validation.expression and message, like in this example:

    https://github.com/microsoft/azure-pipelines-tasks/blob/b0e99b6d8c7d1b8eba65d9ec08c118832a5635e3/Tasks/KubernetesManifestV0/task.json#L90

    "validation": {
        "expression": "isMatch(value, '(^(([0-9]|[1-9][0-9]|100)(\\.\\d*)?)$)','Multiline')",
        "message": "Enter valid percentage value i.e between 0 to 100."
    }
    

    See also: