How to perform validation of json files with cuelang?

Viewed 723

I am evaluating cuelang.org to define data constraints, such as type or value checks on untrusted json payloads submitted to azure devops pipelines, before further processing.

When testing my definitions against invalid data I get no errors returned.

I want to have both type and value checks in place:

rules.cue:


#LoggingLinuxVM: {
  resource_id:  #NonEmptyString
  version:      =~"2020-07-21"
  type:         =~"Linux"
  properties:   #LoggingPropertiesLinuxVM
}

#LoggingPropertiesLinuxVM: {
  CustomLogFiles: [...#NonEmptyString]
}

#NonEmptyString: string & !=""

invalid payload (missing type)


{
  "resource_id": "/subscription/.../...",
  "version": "2020-07-21",
  "properties": {
    "custom_log_files": [
      "/var/log/dmesg",
      "/var/log/messages"
    ]
  }
}

when attempting to validate this with: cue vet payload.json rules.cue I expect to see validation errors, however none are raised and the exit status is 0 . Has anyone used cuelang in a similar fashion and can see where I am going wrong?

1 Answers

Turns out you have to provide the --schema flag to operate in the intended way. For this example, if the json were in payload.json the full command would be:

cue vet --schema '#LoggingLinuxVM' payload.json rules.cue

Related