ivangsa
ivangsa

Reputation: 147

Karate Tests: How to match contains each nested array response with just one schema

Is possible to match each element of a nested array response (using contains) using just one schema?

I have a set of yml files with request params and response schemas, like this one:

response:
  appId: '#string'
  attributes: '#array'
  login: '#string'
  permissions: '#array'
  metadata:
    roles: '##array'
  userData:
    description: '#string'
    employeeId: '#string'
    employeeNumber: '##string'
    id: '#string'
    login: '#string'
    mail: '#string'
    name: '#string'

and then, in a reusable feature:

* def req = read(<testDataFile>)
* match response contains req.response

I can match nested objects with just one schema but I'm not sure if it's possible use the schema to match nested arrays

maybe like:

response:
  appId: '##string'
  attributes: '##array'
  attributes[*]:
    key: '#string'

or any other expression

Thanks a lot

Upvotes: 3

Views: 1970

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

You can't do this with a single "schema" and you have to declare the repeating element separately, as a Karate variable: https://github.com/intuit/karate#schema-validation

* def foo = { a: '#number' }

* def bar = { baz: [{ a: 1 }, { a: 2 }, { a: 3 }] }

* match each bar.baz == foo
* match bar == { baz: '#[] foo' }

Upvotes: 2

Related Questions