Schema validation: string from specific list of va

2019-07-26 07:38发布

问题:

I have an endpoint with the following response:

{
     "id": 1,
     "status": "ACTIVE"
   }

The possible values for status are the following: ACTIVE, INACTIVE, DELETED. To check the schema I tried the following:

* def statusValues = ["ACTIVE", "INACTIVE", "DELETED" ]
* def schema = 
"""
{
  "id" : #number,
  "status" : '#(^*statusValues)'
}
"""

And to validate I use the following sentence: Then match response == schema

But it doesn't work. This is the error

actual: 'ACTIVE', expected: ["DELETED","ACTIVE","INACTIVE"], reason: actual value is not list-like

Can you help me, please?

回答1:

This is probably the simplest option:

* def isValidStatus = function(x){ return statusValues.contains(x) }
* def schema = { id: '#number', status: '#? isValidStatus(_)' }


标签: karate