Forum Discussion

Rakesh1988's avatar
Rakesh1988
New Member
5 years ago

Mandatory/Conditional/Optional Fields

HI, I need help to create json schema with mandatory/conditional/optional field. For mandatory i am taking it inside "required". Those fields which are not under "required" will consider as "optional" 

 

But question is, how should i mention the fileds which are conditional. Meaning, there would be feilds which are optional but mandatory in case of dependant feild. 

 

Example:

There is object "services" and string is "name"

"code" string should come in case of "name" 

 

how can i show "code" in json schema? 

1 Reply

  • There's no way to define conditional fields directly. What you can do is build a schema that's built of several schemas, using keywords like 'allOf', 'oneOf', 'anyOf' and 'not', to build sub-cases for the conditions. By doing that, you can say, for example, that if field X is defined, then fields Y and Z are required, whereas in other cases they are optional.