Test json files & schema within Microsoft devops build pipeline

We use several pure json file git repositories in devops for storing & tracking configuration.
Some of these files are directly used by underlying systems (not critical mission tools, but still painful when failing) by web services querying git json files through devops API

Beside taking care of what we commit, error is human, without an automatic check, you can't ensure that there was no typo or incorrect schema used for pushed json. Even worse, you realize later bringing un-necessary stress & confusion.

Addressing this point shouldn't be un-doable, plenty solutions for validating json & jsonschema exists, and pre-commit hook are supported by git, sounds good, as described in this good article.

Unfortunately, I try to bring json file edition to poweruser (and not IT people) and this solution seems overkill and server side hook in devops aren't supported.

stackoverflow suggests using branch policy and external validation service, but again overkill for my needs.

OK ! let's do it in devops build pipeline. Ok, what should we used for being as simple as possible to setup & maintains and where I had some experience.

  • nodejs/javascript ajv package, but will require extra work to create & build a project
  • C# NjsonSchema for C# ecosystem, but same situation as nodejs one

Seeing the amount of extra work & possible future maintenance, I tried another approach on something already supported by devops pipeline : typed json schema powershell in a search engine. And it was a pretty good idea.

Test-Json cmdlet introduced in powershel core 6.1 allows schema validation. As Microsoft devops supports inline powershell, seems the best no brainer approach .

My pre-requisites

  • All json files are in the same folder
  • Schema file (one for all) is located in the same folder
  • Validation should scan all json files per default & support exclusion
  • All errors should be printed out and not stopped on first one

In a devops build task, add a powershell task and ensure to select Use PowerShell Core. If you are managing yourself, your build agent, you might have to install powershell core binaries for supporting Test-Json cmdlet.

Below script supports 2 environment variables

  • schema : filename of json schema file
  • exclude_json (comma separated array) : json file excluded from validation

output if validation fails

pure json parsing error
schema error

msteams notification

using msteams devops connector on a channel, close to real time, you could notify of a build failure

Fabien Camous

Read more posts by this author.