Working with YAML can be a struggle. It does not matter how easy or complex the task is, an error is easily made. Errors might be an illegal character in a job name or incorrect formatting.
I work with YAML frequently to create or maintain CI/CD pipelines and for this post, I want to share how I validate my YAML files before these are added to the main branch. The validation can be done through the Azure DevOps web interface or via the CLI. The CLI can be a useful tool to automate this process in build validation.
Validation using the GUI
This is the YAML input we are going to work with:
pool: | |
vmImage: windows-2019 | |
jobs: | |
- job: Run PowerShell Write-Host | |
steps: | |
- task: PowerShell@2 | |
inputs: | |
targetType: "inline" | |
script: | | |
Write-Host "Hello World!" |
This input contains an error in the job name. It contains an illegal character, which is the “space” character. Job names only allow [A-Z, a-z, 0-9, and underscores]
characters.
Azure DevOps has a built-in validator that checks your YAML file for errors and invalidations. This validator also works with YAML templates.
To validate a pipeline follow these steps:
- Go to the pipeline you want to validate.
- Press on the Edit button on the top right.
- On the edit-page click on the three dots on the top right.
- (Optional) When using trunk based development you can choose the feature branch you are working in.
- In this menu, click on validate and a message will appear with either OK if the pipeline is validated correctly or an error message if the pipeline is invalid.


The example input we are working with is invalid so after doing step 5 we get an error message containing the location of the error:

If we replace the spaces and the dash (-) with underscores (_) in the job name the pipeline will throw a success message:

Validation using the CLI
There is a way to validate through the CLI. This is useful because it is the perfect way to automate these validations. The YAML must be validated against an existing pipeline. Microsoft provides a “hidden and undocumented” API to validate YAML via a REST call:
https://dev.azure.com/$OrganizationName/$ProjectName/_apis/pipelines/$PipelineId/runs?api-version=5.1-preview
This REST call requires some given segments to find your Azure Pipeline:
- OrganizationName (e.g. Contoso)
- ProjectName (e.g. ContosoTeamProject)
- PipelineId, also known as BuildDefinitionId (e.g. 150)
The BuildDefinitionId can be found in the URL of the pipeline: https://dev.azure.com/contoso/ContosoTeamProject/_build?definitionId=150
This REST call is a POST, so it also requires a body:
$Body = @{ "PreviewRun" = "true" "YamlOverride" = <INSERT_YAML> }
The success output is a bit vague because there is no OK statement. The output of the error message is clear and similar to the message shown in the GUI.
When the output is validated correctly this will be the output:

The full script with the YAML input provided from the last section:
$Body = @{ | |
"PreviewRun" = "true" | |
"YamlOverride" = | |
' | |
pool: | |
vmImage: windows-2019 | |
jobs: | |
- job: Run_PowerShell_Write_Host | |
steps: | |
- task: PowerShell@2 | |
inputs: | |
targetType: "inline" | |
script: | | |
Write-Host "Hello World" | |
' | |
} | |
$OrganizationName = "john-lokerse" | |
$ProjectName = "Blog" | |
$PipelineId = 11 | |
$Url = "https://dev.azure.com/$OrganizationName/$ProjectName/_apis/pipelines/$PipelineId/runs?api-version=5.1-preview" | |
$Arguments = @{ | |
Method = "POST" | |
Uri = $Url | |
Body = $Body | ConvertTo-Json | |
ContentType = "application/json" | |
Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":<YOUR_PAT_HERE>")) } | |
} | |
Invoke-RestMethod @Arguments |
Conclusion
This is how you can validate your YAML files using Azure DevOps and the CLI. This feature is useful when you are working with large YAML files or when you are using templated YAML files.
Update 10/06/2022:
I have written a PowerShell script that makes it easier to validate your YAML file. Also, I added an Azure Pipeline you can use as build validation. Check it out at my GitHub repo: https://github.com/johnlokerse/ado-yaml-validation
Hi John,
In case you dabble in C# I wrote a library that lets you use C# instead of YAML and all of the aforementioned validation happens on compile time – the library validates stage/job names, syntax is always correct and you get also some other interesting validations (comparing used/published artifacts, dependsOn and some more are coming).
The library is called Sharpliner (https://github.com/sharpliner/sharpliner). Please have a look and let me know what you think!
Thanks,
Premek
That looks awesome! Thanks for sharing. 👍🏻