Search code examples
yamlspring-cloud-contract

How to specify optional request parameter in Yaml format of Spring Cloud Contracts?


I have defined a contract as

request:
  method: "POST"
  url: "/some/resource"
  headers:
    Content-Type: "application/json;charset=UTF-8"
  body:
    id: "id123"
    desc: "Some description"
  matchers:
    body:
    - path: "$.id"
      type: "by_regex"
      predefined: "non_blank"
response:
  status: 200
  headers:
    Content-Type: "application/json"
  body:    
    message: "Success"

I want to make desc field optional. In groovy dsl syntax I can define as desc: optional(regex(nonBlank())) What is the Yaml equivalent for the same?


Solution

  • You can't make a field optional. You can make a value of the given key optional. So what that groovy code does is that it wraps the regex in a 0 or 1 occurrence value. You can write such an expression yourself in yaml