Can you put comments in Avro JSON schema files? - json

I'm writing my first Avro schema, which uses JSON as the schema language. I know you cannot put comments into plain JSON, but I'm wondering if the Avro tool allows comments. E.g. Perhaps it strips them (like a preprocessor) before parsing the JSON.
Edit: I'm using the C++ Avro toolchain

Yes, but it is limited. In the schema, Avro data types 'record', 'enum', and 'fixed' allow for a 'doc' field that contains an arbitrary documentation string. For example:
{"type": "record", "name": "test.Weather",
"doc": "A weather reading.",
"fields": [
{"name": "station", "type": "string", "order": "ignore"},
{"name": "time", "type": "long"},
{"name": "temp", "type": "int"}
]
}
From the official Avro spec:
doc: a JSON string providing documentation to the user of this schema (optional).
https://avro.apache.org/docs/current/spec.html#schema_record
An example:
https://github.com/apache/avro/blob/33d495840c896b693b7f37b5ec786ac1acacd3b4/share/test/schemas/weather.avsc#L2

Yes, you can use C comments in an Avro JSON schema : /* something */ or // something Avro tools ignores these expressions during the parsing.
EDIT: It only works with the Java API.

According to the current (1.9.2) Avro specification it's allowed to put in extra attributes, that are not defined, as metadata:
This allows you add comments like this:
{
"type": "record",
"name": "test",
"comment": "This is a comment",
"//": "This is also a comment",
"TODO": "As per this comment we should remember to fix this schema" ,
"fields" : [
{
"name": "a", "type": "long"
},
{
"name": "b", "type": "string"
}
]
}

No, it can't in the C++ nor the C# version (as of 1.7.5). If you look at the code they just shove the JSON into the JSON parser without any comment preprocessing - bizarre programming style. Documentation and language support appears to be pretty sloppy...

Related

Proper validation of a referenced file when editing OpenAPI json in IntelliJ

I'm editing an OpenAPI JSON spec in IntelliJ. The automatic validation and code completion work very nicely.
The OpenAPI version used is 3.0.3, which IntelliJ detects correctly. It seems that it uses "openapi30.json" internally for validation, and all is good.
However, the file is getting very large and it's time to move some commonly-used models out of it using $ref.
This is where things break. The main spec looks like this (snippet):
{
"openapi": "3.0.3",
"info": {
"title": "Cars REST API",
"description": "Calls, Responses and DTOs for REST",
"version": "1.0.0"
},
"components": {
"schemas": {
"car": {
"$ref": "car.json"
},
"car-group": {
"$ref": "car-group.json"
}
And when editing it, IntelliJ recognizes it as "openapi30".
However, the referenced documents are not recognized. For example, the car.json file looks like this:
{
"car": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
And it's recognized simply as a JSON document, not an OpenAPI one, so there is no proper validation and no code completion, etc.
How does one tell IntelliJ that the file is part of an OpenAPI specification, to be validated as such? One should think this could be inferred from begin $ref'ed from the main spec, but this doesn't work.
Trying to add a $schema value in the referenced file had no effect (and probably isn't in line with the OpenAPI spec anyway).
Manually selecting the OpenAPI 3.0 file type for car.json is not helpful, because then validation (rightly) fails - as it doesn't have the top-level structure required (info, openapi, paths).
Perhaps some specific JSON schema mapping needs to be added in IntelliJ preferences? If that's the case, it would be actually a sub-schema or some tag in the main OpenAPI spec, how can that be done?
IntelliJ version is: IntelliJ IDEA 2021.3.2 (Ultimate Edition)
Any help would be greatly appreciated.
Ron!
Such functionality is not yet supported. Please vote for https://youtrack.jetbrains.com/issue/IDEA-284305

Compare object property to validate json schema in same object

I have a very simple object like so:
{
"title": "A registration form",
"description": "A simple form example.",
"type": "object",
"required": [
"firstName",
"lastName"
],
"properties": {
"firstName": {
"type": "string",
"title": "First name"
},
"lastName": {
"type": "string",
"title": "Last name",
"minLength": "{'$ref': '/properties/firstName'}"
},
}
On the property of LastName I would like to compare a value with a value of the property next to it. I am actually comparing integers so in actuality my real world example is even easier, just minimum and maximum.
I looked at the json schema spec here and it seemed like this should be doable, and have tried using relative paths and the $ref object.
Is this not possible?
The reference is here: https://www.rfc-editor.org/rfc/rfc6901
I am using react-jsonschema-form but I don't see where that would effect this.
There are a few of reasons why this doesn't work. The primary reason is that $ref refers to the schema, not the data being validated. There was talk of adding a $data keyword to JSON Schema that would allow referencing the instance data, but I don't think that will even happen.

Seggregation of Json Schema Validation and Json Validation

I have a use case where I will take a json-schema as input, validate it, then keep in my system. Later I will get json data which I need to validate with above mentioned json-schema. given the scenario, I need to do two level of validations:
1. provided json-schema is valid or not.
2. Json is valid or not.
I am using json-schema-validator jar and could find only second level of validation, couldn't find json-schema validation in documentation. for example: lets say we have below sample json-schema:
{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
so how to validate this json-schema itself is valid or not?
There is a working example here with the everit-org/json-schema implementation (just in case you want to use a maintained library):
How to validate a json schema against the version spec it specifies in Java
You have to validate schema against meta-schema: http://json-schema.org/draft-04/schema

Use custom type in JSON Schema

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.

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.