1

I am trying to validate the below conditions in the sample response

  1. Based on subjectType attribute if it is either Person or Organization.
  2. If subjectType is Person, check if it has a birthDate attribute

Sample response:

{ 
 "subject": "ABC",    
 "subjectType": "Person",    
 "birthDate": "1951-03-07"
}, 
{ 
 "subject": "ABC",    
 "subjectType": "Organization"    
}

I tried this, but no luck.

def expected = {subjectType: 'Person', birthDate: '#present'}

def schema = { subjectType: '#? _ == "Person" || _ == "Organization"', birthDate: '#($.subjectType == "Person" ? "#present" : "#present")' }

match expected == schema

Any help is appreciated!

Bharathi
  • 1,015
  • 13
  • 41
Nivetha G
  • 11
  • 2

1 Answers1

0

There are many answers that give you solutions, please start here:

https://stackoverflow.com/a/50350442/143475

Since I just came up with a new idea to solve this, I'm posting it below. Note that there is no right answer, and there are many, many ways to do what you want.

* def schemas = 
"""
{
  Person: { subject: '#string', subjectType: 'Person', birthDate: '#string' },
  Organization: { subject: '#string', subjectType: 'Organization' }
}
"""
* def response = { "subject": "ABC", "subjectType": "Person", "birthDate": "1951-03-07" }
* def expected = schemas[response.subjectType]
* match response == expected

Also search slack overflow if you want to validate dates later.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Great! The above one worked, but how can I assert the response dynamically.? Above u have defined the response – Nivetha G Dec 29 '21 at 07:45