JsonSchema polymorphic object depends on other field - json

I have following JSONs:
[
{
"type": "message",
"payload": {
"message": "some message"
}
},
{
"type": "image",
"payload": {
"url": "http://example.com/foo/bar"
}
},
{
"type": "video",
"payload": {
"url": "http://example.com/foo/baz"
}
}
]
And I would like to validate:
If type == message then payload should have message
If type in image, video then payload should have url
I figure something like:
{
"type": "object",
"properties": {
"type": {
"enum": ["message", "image", "video"]
},
"payload": {
"type": "object",
"oneOf": [
{
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
},
{
"properties": {
"url": {
"type": "string"
}
},
"required": ["url"]
}
]
}
}
}
But this not valid well when e.g.: type is message and payload has url.

you should move the oneOf up to describe the whole message/image/video object, and replace the enum for the "type" property with individual const values across the oneOf schemas:
{
"type": "object",
"oneOf": [
{
"properties": {
"type": {
"const": "message",
},
"payload": {
(message payload schema)
}
}
},
{
"properties": {
"type": {
"const": "image",
},
"payload": {
(image payload schema)
}
},
}
]
}

Using oneOf for this type of thing is terrible for error messaging. Here's an alternative using if/then that's more verbose, but is better for error messaging and performance.
{
"type": "object",
"properties": {
"type": { "enum": ["message", "image", "video"] },
"payload": {
"message": { "type": "string" },
"url": { "type": "string", "format": "uri" }
}
},
"allOf": [
{
"if": {
"properties": {
"type": { "const": "message" }
},
"required": ["type"]
},
"then": {
"properties": {
"payload": { "required": ["message"] }
}
}
},
{
"if": {
"properties": {
"type": { "enum": ["image", "video"] }
},
"required": ["type"]
},
"then": {
"properties": {
"payload": { "required": ["url"] }
}
}
}
]
}

Related

Assistance needed to create JSON schema

As I am new to JSON schema creation, I have learnt the basics of JSON schema and now I am trying to create JSON schema for the below mentioned set of data,
{
"Result": [
{
"ResourceName": "Appointment",
"Sequence": "1",
"Data": {
"AppointmentID": "A1234",
"PatientName": "Test Patient",
"ClinicName": "Test Clinic"
}
},
{
"ResourceName": "EpisodeofCare",
"Sequence": "2",
"Data": {
"EpisodeID": "EP1234",
"LocationId": "L1234",
"AppointmentId": "A1234",
"TransactionStatus": "2",
"OPNumber": "OP523367"
}
},
{
"ResourceName": "Encounter",
"Sequence": "3",
"Data": {
"EncounterID": "E1234",
"PatientID": "P1234"
}
}
]
}
Can anybody please help me to create JSON schema for this kind of data set.
Thanks in advance for helping me out on this.
Below is the JSON schema i have drafted
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ResultType": {
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"anyOf": [
{
"$ref": "#/definitions/Appointment"
},
{
"$ref": "#/definitions/EpisodeofCare"
},
{
"$ref": "#/definitions/Encounter"
}
]
}
}
},
"Appointment": {
"type": "object",
"properties": {
"AppointmentID": {
"type": "boolean"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
}
},
"EpisodeofCare": {
"type": "object",
"properties": {
"EpisodeID": {
"type": "string"
},
"LocationId": {
"type": "string"
},
"AppointmentId": {
"type": "string"
},
"TransactionStatus": {
"type": "string"
},
"OPNumber": {
"type": "string"
}
}
},
"Encounter": {
"type": "object",
"properties": {
"EncounterID": {
"type": "string"
},
"PatientID": {
"type": "string"
}
}
}
},
"type": "object",
"properties": {
"Result": {
"type": "array",
"$ref": "#/definitions/ResultType"
}
}
}
Throw it in a schema validator like this one.
You will see errors in your schemas definition and in validation.
First of all check that your schema is valid json (the validator might help):
[
{
{"$ref": "#/definitions/Appointment"} <- unnecessary and invalid braces
},
{
{"$ref": "#/definitions/EpisodeofCare"} <- unnecessary and invalid braces
},
{
{"$ref": "#/definitions/Encounter"} <- unnecessary and invalid braces
}
]
Within the Result property you want each item of the array. You should use the item property instead of ref. As taken from the specification:
The value of "items" MUST be either a valid JSON Schema or an array of valid JSON Schemas.
[...]
If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema.
Thus you should use
{
"type": "object",
"properties": {
"Result": {
"type": "array",
"items": {
"$ref": "#/definitions/ResultType"
}
}
}
}
Since the schema should be as specific as possible you should constraint the content of the ResourceName properties to the actual data layout.
Use the const property to define a constant value for the ResourceName for each type of Resource. Take for example the Appointment type, which would change to:
"Appointment": {
"type": "object",
"properties": {
"ResourceName": {
"const": "Appointment"
},
"Data": {
"type": "object",
"properties": {
"AppointmentID": {
"type": "string"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
}
}
}
I also changed the type of AppointmentID to string.
Then your ResultType needs to be adjusted. Before you used anyOf to validate the data, now you need it to validate the complete object while still maintaining the overall structure:
"ResultType": {
"allOf": [
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": true
}
},
{
"anyOf": [
{
"$ref": "#/definitions/Appointment"
},
{
"$ref": "#/definitions/EpisodeofCare"
},
{
"$ref": "#/definitions/Encounter"
}
]
}
]
}
The allOf propertiy implies a and condition. The overall structure (the object) does not care what value ResourceName has or how Data is structured. The specific implementations (the anyOf part) take care of that.
Some final improvements. Use the required property to define the names of properties that must exist in your JSON file. Also add "additionalProperties": false to ensure that only the specified properties are used. And finally constraint your sequence values to be string representations of positive intergers by adding "pattern": "[1-9]\\d*".
Final result:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ResultType": {
"allOf": [
{
"type": "object",
"required": [
"ResourceName",
"Sequence",
"Data"
],
"additionalProperties": false,
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string",
"pattern": "[1-9]\\d*"
},
"Data": true
}
},
{
"anyOf": [
{
"$ref": "#/definitions/Appointment"
},
{
"$ref": "#/definitions/EpisodeofCare"
},
{
"$ref": "#/definitions/Encounter"
}
]
}
]
},
"Appointment": {
"type": "object",
"properties": {
"ResourceName": {
"const": "Appointment"
},
"Data": {
"type": "object",
"required": [
"AppointmentID",
"PatientName",
"ClinicName"
],
"additionalProperties": false,
"properties": {
"AppointmentID": {
"type": "string"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
}
}
}
},
"EpisodeofCare": {
"type": "object",
"properties": {
"ResourceName": {
"const": "EpisodeofCare"
},
"Data": {
"type": "object",
"required": [
"EpisodeID",
"LocationId",
"AppointmentId",
"TransactionStatus",
"OPNumber"
],
"additionalProperties": false,
"properties": {
"EpisodeID": {
"type": "string"
},
"LocationId": {
"type": "string"
},
"AppointmentId": {
"type": "string"
},
"TransactionStatus": {
"type": "string"
},
"OPNumber": {
"type": "string"
}
}
}
}
},
"Encounter": {
"type": "object",
"properties": {
"ResourceName": {
"const": "Encounter"
},
"Data": {
"type": "object",
"required": [
"EncounterID",
"PatientID"
],
"additionalProperties": false,
"properties": {
"EncounterID": {
"type": "string"
},
"PatientID": {
"type": "string"
}
}
}
}
}
},
"type": "object",
"required": [
"Result"
],
"additionalProperties": false,
"properties": {
"Result": {
"type": "array",
"items": {
"$ref": "#/definitions/ResultType"
}
}
}
}
you can create json schema by online tools which generates json schema from the json
like: https://www.liquid-technologies.com/online-json-to-schema-converter , it also checks if json is valid or not.
Json Schema that you need:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Result": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"properties": {
"AppointmentID": {
"type": "string"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
},
"required": [
"AppointmentID",
"PatientName",
"ClinicName"
]
}
},
"required": [
"ResourceName",
"Sequence",
"Data"
]
},
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"properties": {
"EpisodeID": {
"type": "string"
},
"LocationId": {
"type": "string"
},
"AppointmentId": {
"type": "string"
},
"TransactionStatus": {
"type": "string"
},
"OPNumber": {
"type": "string"
}
},
"required": [
"EpisodeID",
"LocationId",
"AppointmentId",
"TransactionStatus",
"OPNumber"
]
}
},
"required": [
"ResourceName",
"Sequence",
"Data"
]
},
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"properties": {
"EncounterID": {
"type": "string"
},
"PatientID": {
"type": "string"
}
},
"required": [
"EncounterID",
"PatientID"
]
}
},
"required": [
"ResourceName",
"Sequence",
"Data"
]
}
]
}
},
"required": [
"Result"
]
}

Error in Parse JSON step in Microsoft Flow

Sorry for the long question, but I wanted to include all the JSON to get the best help.
we are getting an error parsing JSON from an HTTP Get request in our flow.
Here is the schema we are using in our Parse JSON step:
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Alerts": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Groups": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Id": {
"type": "integer"
},
"IsHiddenInUI": {
"type": "boolean"
},
"LoginName": {
"type": "string"
},
"Title": {
"type": "string"
},
"PrincipalType": {
"type": "integer"
},
"Email": {
"type": "string"
},
"IsEmailAuthenticationGuestUser": {
"type": "boolean"
},
"IsShareByEmailGuestUser": {
"type": "boolean"
},
"IsSiteAdmin": {
"type": "boolean"
},
"UserId": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"NameId": {
"type": "string"
},
"NameIdIssuer": {
"type": "string"
}
}
}
},
"required": [
"__metadata",
"Alerts",
"Groups",
"Id",
"IsHiddenInUI",
"LoginName",
"Title",
"PrincipalType",
"Email",
"IsEmailAuthenticationGuestUser",
"IsShareByEmailGuestUser",
"IsSiteAdmin",
"UserId"
]
}
}
}
}
}
}
we are only interested in the Email property that we want to use to send approvals...
here are the INPUTS results of the Parse JSON step that fails:
{
"d": {
"results": [
{
"__metadata": {
"id": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)",
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)",
"type": "SP.User"
},
"Alerts": {
"__deferred": {
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)/Alerts"
}
},
"Groups": {
"__deferred": {
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)/Groups"
}
},
"Id": 1691,
"IsHiddenInUI": false,
"LoginName": "i:0#.f|membership|Surnamei#Company.org.uk",
"Title": "Firstname Surname",
"PrincipalType": 1,
"Email": "Surnamef#Company.org.uk",
"Expiration": "",
"IsEmailAuthenticationGuestUser": false,
"IsShareByEmailGuestUser": false,
"IsSiteAdmin": false,
"UserId": {
"__metadata": {
"type": "SP.UserIdInfo"
},
"NameId": "10033fff9fe67a30",
"NameIdIssuer": "urn:federation:microsoftonline"
},
"UserPrincipalName": "Surnamef#Company.org.uk"
},
}
]
}
here is the schema from the run results of the Parse JSON that fails:
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Alerts": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Groups": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Id": {
"type": "integer"
},
"IsHiddenInUI": {
"type": "boolean"
},
"LoginName": {
"type": "string"
},
"Title": {
"type": "string"
},
"PrincipalType": {
"type": "integer"
},
"Email": {
"type": "string"
},
"IsEmailAuthenticationGuestUser": {
"type": "boolean"
},
"IsShareByEmailGuestUser": {
"type": "boolean"
},
"IsSiteAdmin": {
"type": "boolean"
},
"UserId": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"NameId": {
"type": "string"
},
"NameIdIssuer": {
"type": "string"
}
}
}
},
"required": [
"__metadata",
"Alerts",
"Groups",
"Id",
"IsHiddenInUI",
"LoginName",
"Title",
"PrincipalType",
"Email",
"IsEmailAuthenticationGuestUser",
"IsShareByEmailGuestUser",
"IsSiteAdmin",
"UserId"
]
}
}
}
}
}
}
and here is the error in the step from the run history:
[
{
"message": "Invalid type. Expected Object but got Null.",
"lineNumber": 0,
"linePosition": 0,
"path": "d.results[3].UserId",
"schemaId": "#/properties/d/properties/results/items/properties/UserId",
"errorType": "type",
"childErrors": []
}
]
so how can I change the schema to successfully retrieve the email
The problem was caused by UserId being null in several elements of the data received from the HTTP Get, while that is set as required in the JSON Schema...
initially we solved it by using the below in the HTTP Get:
_api/web/sitegroups/getbyname('Legal Owners')/users?$filter=UserId+ne+null
Then we were given a better and more elegant solution on the Power Platform Community:
in the Apply to each step use:
body('Send_an_HTTP_request_to_SharePoint')?['d']?['results']
and within the loop use:
item()?['Email']

Validation JSON subschema definition based on ENUM value

I'm trying to validate and report errors of a JSON schema for a build process.
Based on the type enum I want to validate against a specific subschema and report errors for that schema.
If the type attribute is "weblogic" then I only want to validate against the "weblogic" subschema definition. And do the same if the type is tomcat
This is my current schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "#",
"type": "object",
"title": "The Root Schema",
"required": [
"middleware"
],
"properties": {
"middleware": {
"$ref": "#/definitions/middleware"
}
},
"definitions": {
"middleware":{
"type": "array",
"items": {
"oneOf":[
{"$ref": "#/definitions/weblogic"},
{"$ref": "#/definitions/tomcat"}
],
"required": ["type","buildInfo"]
}
},
"weblogic": {
"properties": {
"type": {"const": "weblogic"},
"buildInfo": {
"properties": {
"adminSslPort": {
"type": "integer"
}
},
"additionalProperties": false,
"required": ["adminSslPort"]
}
}
},
"tomcat":{
"properties": {
"type": {"const": "tomcat"},
"buildInfo":{
"properties": {
"classpath": {
"type": "string"
}
},
"additionalProperties": false,
"required": ["classpath"]
}
}
}
}
}
And my JSON payload
{
"middleware":[
{
"type": "weblogic",
"buildInfo":{
"adminSslPort": 7002
}
},
{
"type": "tomcat",
"buildInfo":{
}
}
]
}
Now I expect this to fail since the buildInfo object is missing the classpath attribute, and it does fail validation.
But the errors I get back include the validation errors against the "weblogic" definition.
[
{
"pointerToViolation": "#/middleware/1",
"causingExceptions": [
{
"schemaLocation": "#/definitions/weblogic",
"pointerToViolation": "#/middleware/1",
"causingExceptions": [
{
"schemaLocation": "#/definitions/weblogic/properties/buildInfo",
"pointerToViolation": "#/middleware/1/buildInfo",
"causingExceptions": [],
"keyword": "required",
"message": "required key [adminSslPort] not found"
},
{
"schemaLocation": "#/definitions/weblogic/properties/type",
"pointerToViolation": "#/middleware/1/type",
"causingExceptions": [],
"keyword": "const",
"message": ""
}
],
"message": "2 schema violations found"
},
{
"schemaLocation": "#/definitions/tomcat/properties/buildInfo",
"pointerToViolation": "#/middleware/1/buildInfo",
"causingExceptions": [],
"keyword": "required",
"message": "required key [classpath] not found"
}
],
"keyword": "oneOf",
"message": "#: 0 subschemas matched instead of one"
}
]
Is there a way to only validate against the subschema that the type matches against?
You can use if and then keywords as follows.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "#",
"type": "object",
"title": "The Root Schema",
"required": [
"middleware"
],
"properties": {
"middleware": {
"$ref": "#/definitions/middleware"
}
},
"definitions": {
"middleware":{
"type": "array",
"items": {
"type": "object",
"allOf": [
{"$ref": "#/definitions/weblogic"},
{"$ref": "#/definitions/tomcat"}
],
"required": ["type","buildInfo"]
}
},
"weblogic": {
"if": {
"properties": {
"type": {
"const": "weblogic"
}
}
},
"then": {
"properties": {
"buildInfo": {
"properties": {
"adminSslPort": {
"type": "integer"
}
},
"additionalProperties": false,
"required": ["adminSslPort"]
}
}
}
},
"tomcat":{
"if": {
"properties": {
"type": {
"const": "tomcat"
}
}
},
"then": {
"properties": {
"buildInfo":{
"properties": {
"classpath": {
"type": "string"
}
},
"additionalProperties": false,
"required": ["classpath"]
}
}
}
}
}
}

jsonschema Draft 0.7 required properties in nested object depending on a value

I have a json file with nested objects.
{
"apiVersion":"0.0.9b",
"apiDate":"18.01.19",
"general":{
"documentType": "invoice",
"references":{
"invoiceId":"123",
"invoiceDate":"01.01.1970",
"creditNoteId":"123",
"creditNoteDate":"01.01.1970"
}
}
}
Now I would like to define that invoiceId and invoiceDate should be required if documentType is invoice, and also the other way arraound (creditNoteId and Date are required if documentType is creditNote).
All other Properties should be optional.
Pseudo-Code:
documentType = invoice
- required: invoiceId, invoiceDate
- optional: creditNoteId, creditNoteDate
documentType = creditNote
- required: creditNoteId, creditNoteDate
- optional: invoiceId, invoiceDate
If i store all properties in the same object I found this working solution:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"apiVersion",
"apiDate"
],
"properties": {
"apiVersion": {
"type": "string",
"description": "The version of the json file"
},
"apiDate": {
"type": "string",
"description": "The date when the json version was published"
},
"general": {
"$ref": "#/definitions/general_identifiers"
}
},
"definitions" : {
"general_identifiers" : {
"type": "object",
"required": [
"documentType"
],
"properties": {
"documentType": {
"enum": [
"invoice",
"creditNote"
]
},
"invoiceId": {
"type": "string"
},
"invoiceDate": {
"type": "string"
},
"creditNoteId": {
"type": "string"
},
"creditNoteDate": {
"type": "string"
}
},
"oneOf": [
{
"$comment": "Invoice",
"properties": {
"documentType": { "enum": ["invoice"] }
},
"required": ["invoiceId", "invoiceDate"]
},
{
"$comment": "CreditNote",
"properties": {
"documentType": { "enum": ["creditNote"] }
},
"required": ["creditNoteId", "creditNoteDate"]
}
]
}
}
}
Is there a way to display this dependency with nested objects used in the above json?
What I tried already was:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"apiVersion",
"apiDate"
],
"properties": {
"apiVersion": {
"type": "string",
"description": "The version of the json file"
},
"apiDate": {
"type": "string",
"description": "The date when the json version was published"
},
"general": {
"$ref": "#/definitions/general_identifiers"
},
"references": {
"type": "object",
"properties": {
"invoiceId": {
"type": "string"
},
"invoiceDate": {
"type": "string"
},
"creditNoteId": {
"type": "string"
},
"creditNoteDate": {
"type": "string"
}
},
"oneOf": [
{
"$comment": "Invoice",
"properties": {
"documentType": { "enum": ["invoice"] }
},
"required": ["invoiceId", "invoiceDate"]
},
{
"$comment": "CreditNote",
"properties": {
"documentType": { "enum": ["creditNote"] }
},
"required": ["creditNoteId", "creditNoteDate"]
}
]
}
},
"definitions" : {
"general_identifiers" : {
"type": "object",
"required": [
"documentType"
],
"properties": {
"documentType": {
"enum": [
"invoice",
"creditNote"
]
}
}
}
}
}
But with this i get an Error from https://www.jsonschemavalidator.net
Message: JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1.
What have I missed?
You're very close. You just need to pull your oneOf up to the top level so you can reference #/properties/general and #/properties/references from the same schema.
Also, you almost always want to use anyOf instead of oneOf. oneOf enforces that one and only one schema in the list validates. When the schemas are mutually exclusive, oneOf is just asking the validator to do unnecessary work.
"anyOf": [
{
"properties": {
"general": {
"properties": {
"documentType": { "enum": ["invoice"] }
}
},
"references": {
"required": ["invoiceId", "invoiceDate"]
}
}
},
{
"properties": {
"general": {
"properties": {
"documentType": { "enum": ["creditNote"] }
}
},
"references": {
"required": ["creditNoteId", "creditNoteDate"]
}
}
}
]
With the help of Jason Desrosiers I finaly found a solution also for my nested json.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"apiVersion",
"apiDate"
],
"anyOf": [
{
"properties": {
"general": {
"properties": {
"documentType": { "enum": ["invoice"] },
"references": {
"required": ["invoiceId", "invoiceDate"]
}
}
}
}
},
{
"properties": {
"general": {
"properties": {
"documentType": { "enum": ["creditNote"] },
"references": {
"required": ["creditNoteId", "creditNoteDate"]
}
}
}
}
}
],
"properties": {
"apiVersion": {
"type": "string",
"description": "The version of the json file"
},
"apiDate": {
"type": "string",
"description": "The date when the json version was published"
},
"general": {
"$ref": "#/definitions/general_identifiers",
"references": {
"type": "object",
"properties": {
"invoiceId": {
"type": "string"
},
"invoiceDate": {
"type": "string"
},
"creditNoteId": {
"type": "string"
},
"creditNoteDate": {
"type": "string"
}
}
}
}
},
"definitions" : {
"general_identifiers" : {
"type": "object",
"required": [
"documentType"
],
"properties": {
"documentType": {
"enum": [
"invoice",
"creditNote"
]
}
}
}
}
}

Issue with JSON.net anyOf schema validation

I'm using the JSON.Net schema validation package and I've come across a very strange issue. I have tracked down the issue to a use of anyOf in the anyObject definition below:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/schemas/example/1.0/schema.json",
"anyOf": [
{
"$ref": "#/definitions/anyObject"
}
],
"definitions": {
"anyObject": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
},
"required": [
"type"
],
"anyOf": [
{
"if": {
"properties": {
"type": {
"const": "typeA"
}
}
},
"then": {
"$ref": "#/definitions/typeA"
},
"else": false
},
{
"if": {
"properties": {
"type": {
"const": "typeB"
}
}
},
"then": {
"$ref": "#/definitions/typeB"
},
"else": false
}
]
},
"bodyDefinition": {
"oneOf": [
{
"if": {
"properties": {
"$computed": {
"type": "string"
}
},
"required": [
"$computed"
]
},
"then": {
"$ref": "#/definitions/computedBody"
},
"else": {
"$ref": "#/definitions/wildcardBody"
}
},
{
"type": "string"
}
]
},
"wildcardBody": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/bodyDefinition"
}
},
"firstComputedValue": {
"type": "object",
"additionalProperties": false,
"properties": {
"$computed": {
"const": "first"
},
"values": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/bodyDefinition"
}
}
},
"required": [
"$computed",
"values"
]
},
"computedBody": {
"oneOf": [
{
"if": {
"properties": {
"$computed": {
"const": "first"
}
}
},
"then": {
"$ref": "#/definitions/firstComputedValue"
},
"else": false
}
]
},
"typeA": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"const": "typeA"
},
"body": {
"$ref": "#/definitions/bodyDefinition"
}
},
"required": [
"type"
]
},
"typeB": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"const": "typeB"
},
"body": {
"$ref": "#/definitions/bodyDefinition"
}
},
"required": [
"type"
]
}
}
}
When I test this json:
{
"type": "typeB",
"body":{
"$computed":"first",
"values":[]
}
}
It should be marked as invalid, because values is required to have at least one value. However it is valid. The below JSON should be considered valid, and the above schema does assert that correctly:
{
"type": "typeB",
"body":{
"$computed":"first",
"values":["foo"]
}
}
If I remove typeA from the anyObject definition, then the validation is performed correctly. Below is the schema that validates correctly:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/schemas/example/1.0/schema.json",
"anyOf": [
{
"$ref": "#/definitions/anyObject"
}
],
"definitions": {
"anyObject": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
},
"required": [
"type"
],
"anyOf": [
{
"if": {
"properties": {
"type": {
"const": "typeB"
}
}
},
"then": {
"$ref": "#/definitions/typeB"
},
"else": false
}
]
},
"bodyDefinition": {
"oneOf": [
{
"if": {
"properties": {
"$computed": {
"type": "string"
}
},
"required": [
"$computed"
]
},
"then": {
"$ref": "#/definitions/computedBody"
},
"else": {
"$ref": "#/definitions/wildcardBody"
}
},
{
"type": "string"
}
]
},
"wildcardBody": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/bodyDefinition"
}
},
"firstComputedValue": {
"type": "object",
"additionalProperties": false,
"properties": {
"$computed": {
"const": "first"
},
"values": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/bodyDefinition"
}
}
},
"required": [
"$computed",
"values"
]
},
"computedBody": {
"oneOf": [
{
"if": {
"properties": {
"$computed": {
"const": "first"
}
}
},
"then": {
"$ref": "#/definitions/firstComputedValue"
},
"else": false
}
]
},
"typeA": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"const": "typeA"
},
"body": {
"$ref": "#/definitions/bodyDefinition"
}
},
"required": [
"type"
]
},
"typeB": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"const": "typeB"
},
"body": {
"$ref": "#/definitions/bodyDefinition"
}
},
"required": [
"type"
]
}
}
}
Can anyone see if there is an issue with this definition, or is this an issue with the JSON.Net schema package?
This testing was done against the online version of the schema validator at https://www.jsonschemavalidator.net/
I think this is a bug. I'll speak to the library author about it!
To debug, I followed the validation process through the schema, setting $ref or then or else to false... when I reached computedBody, I changed it to the following...
"computedBody": {
"if": {
"properties": {
"$computed": {
"const": "first"
}
}
},
"then": false,
"else": false
}
Validation still comes back positive, which shouldn't be possible. I proved it reaches computedBody by setting that subschema to false and seeing validation come back negative.
(the oneOf wrapping the subschema of computedBody isn't needed. if is valid at the schema level just fine).