Use custom type in JSON Schema - json

One have the following JSON object:
{
"index": 10,
"data": "<?xml version=\"1.0\"?>..."
}
the corresponding schema:
{
"title": "Example",
"type": "object",
"properties": {
"index": {
"type": "integer"
},
"data": {
"type": "string"
}
}
}
What I'm trying to achieve is to validate XML inside data property with XSD schema.
How to represent XML data type with xsd schema attribute correctly from the point of JSON Schema specs?

Short answer
You can't
Long answer
You really can't. No JSON processor in the history of mankind will be able to validate inline XML against an XSD.
The only thing you can do is include the XSD file as text and then the consumer of the JSON can then do the validation on their side. Or, even better, validate the XML before you place it in the JSON doc.

Related

Generate JSON payload based on JSON schema for API payload validation

I am looking for a way to generate random payloads in JSON based on the provided schema. Looking around I found something useful but with not a lot of github stars. LINK
Objective
To be able to validate multiple possible values for a field in the payload based on the schema provided.
Example
For schema -
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
Possible payloads -
{
"name": "ABCDEFGHIJKLMNOPQRSTUVWXYZA"
}
{
"name": "ERTERER"
}
I hope my request for suggestion was clear.

Correct way to set the properties of a JSON Schema Property to be the JSON Schema

I'm trying to write a schema for a JSON Schema document where I want one of the properties to be a JSON Schema document.
What I have so far is this (part of a larger schema doc), which I believe is wrong:
{
"type": "object",
"description": "Describe request/response",
"properties": {
"requestSchema": {
"$ref": "http://json-schema.org/draft-07/schema#"
},
"replySchema": {
"$ref": "http://json-schema.org/draft-07/schema#"
}
}
}
What is the correct way to say that the properties of the "requestSchema" object should be a JSON-Schema Object?
I've seen hyper schema mentioned but I'm not sure how this would be implemented in this case.

Inclusion of a JSON object between two seperate JSON files

I have an object definition in common.json file that I need to use in number of other JSON files in terms of reusability. Is there any way to include my common.json file into other JSON files?
Edit:
I came across JSON Pointer while searching which made me thought JSON alone can handle it. To be more clear:
common.json
{
"common":
{
"course":
{
"type": "object",
"properties":
{
"course_name": { "type": "string" },
"course_id": { "type": "integer" },
"course_room": { "type": "integer" }
}
}
}
}
other.json
{
"weekly_schedule":
{
"mathematics": { "$ref": "common.json#/course" },
"history": { "$ref": "common.json#/course" }
}
}
What I understand from here is I can refer to a common JSON object from elsewhere using its path and the $ref keyword. Is that correct or am I missing some point?
JSON is a very simple metaformat. If you take a look at its specification, you will find how simple it is. In particular, it doesn't define any means of aggregation, namespaces, schemata like they are available in XML.
If you want to manipulate JSON or compose different JSON-files, you either treat them as a whole (i.e. as text) and then apply text tools or you decode them, manipulate the received data and then encode the results again.
No, JSON is just text. It doesn't do anything on it's own.

Accepting an image as an input by reading a JSON schema?

In a JSON Schema we can specify what type of entity we are expecting such as a string like
"Name": {
"type": [
"string",
"null"
]
}
Can we do something same for expecting images as input?
JSON Hyper-Schema defines a media keyword that can allow you to specify an image as an input. Most JSON Schema validators don't support Hyper-Schema, but if you have one that does, this could be useful.
{
"type": "string",
"media": {
"binaryEncoding": "base64",
"type": "image/png"
}
}
http://json-schema.org/latest/json-schema-hypermedia.html#anchor10

Validate Swagger API Declaration using official Swagger API schema

The swagger-spec repository provides a JSON-schema describing a valid Swagger 2.0 API definition.
I would like to use this schema in order to validate if a given API definition file is valid before I try to interpret it.
I'm using the following code to load the schema using Json.NET:
JsonSchema swaggerApiSchema;
using (var textReader = new JsonTextReader(new StreamReader(#"C:\path\to\schema.json")))
{
swaggerApiSchema = JsonSchema.Read(textReader);
}
This throws an ArgumentException reporting "Can not convert Array to Boolean.".
Is there something wrong with the schema file, is this a bug with Json.NET, or am I just doing something wrong?
As per documentation, JSON.NET implements JSON Schema Draft 3. More here. But the Swagger schema you posted is created according to the JSON Schema Draft 4. One of the differences between the Draft 3 and Draft 4 of the JSON Schema is the required attribute, which in JSON Schema Draft 3 was an attribute of subschemas in properties. In JSON Schema Draft 4 is a first level keyword playing the same role, and has a string array as an argument.
Sample of JSON Schema Draft 3:
{
"properties": {
"Id": {
"required": true,
"type": "integer"
},
"FirstName": {
"required": true,
"type": "string",
},
"LastName": {
"required": true,
"type": "string
}
}
}
Sample of JSON Schema Draft 4:
{
"properties": {
"Id": {
"type": "integer"
},
"FirstName": {
"type": "string"
},
"LastName": {
"type": "string"
}
},
"required": [ "Id", "FirstName", "LastName" ]
}
Notice the difference in the two schemas, of how required properties are defined. That's why you are getting an error: "Can not convert Array to Boolean.".
And here is the first appearance of required property in the Swagger JSON Schema, that is causing the error:
"required": [ "swagger", "info", "paths" ]
I would suggest to validate with parser that implements JSON Schema Draft 4.