Validate a path against a json schema - json

I am using com.networknt.schema.JsonSchema
A sample schema look like below:
{
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
}
}
}
}
Now I have to validate in java code:
address.city -> valid path in json
address.pincode -> invalid path
I have tried the below approaches and both does not seem to be a good one:
Manually iterate through the schema
Generate a dummy payload and then validate the path. However, in actual schema I do have custom types as well and generating the dummy payload seems to be an overkill for this task.

Related

How to setup ResponseType (Net Core 6) based on JSON Schema

I have a large number of APIs, each without a strong class definition for output, like this
Return Ok(New With {
.ServerDecryptPass = ServerDecryptPass,
.VmConnectionAdminDecryptPass = VmConnectionAdminDecryptPass,
.DockerHubDecryptPass = DockerHubDecryptPass,
.DockerRegistryDecryptPass = DockerRegistryDecryptPass,
.VmConnectionUserDecryptPass = VmConnectionUserDecryptPass
})
Swagger, of course, doesn't see anything for response type in this case
The simplest way to formalize this JSON result is creating a JSON schema, for example in this case it looks like
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"serverDecryptPass": {
"type": "object",
"properties": {
"item1": {
"type": "array",
"items": {
"type": "number"
}
},
"item2": {
"type": "null"
}
}
},
"vmConnectionAdminDecryptPass": {
...
}
I have a hundreds of APIs and I am searching for a simple way to formalize the output of the APIs in order to generate a frontend automatically from this backend API. I searched for a simple way to setup the attribute, ProducesResponseType, based on XML schema or maybe even an example of an API result.
What is a possible solution?

How to convert JsonSchema Path to equivalent jsonPath within a document adhering to that schema?

What are the possible ways to convert a JSON path written for JsonSchema to another jsonPath to refer to the same node in the generated document using the same schema?
Consider the following document:
{
myObject: {
myKey: "myValue"
}
}
Which has the following schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"myObject": {
"type": "object",
"properties": {
"myKey": {
"type": "string"
}
},
"required": [
"myKey"
]
}
},
"required": [
"myObject"
]
}
Within the document, the path expression for myKey would be:
$.myObject.myKey
But within the schema, the path expression for myKey would be:
$.properties.myObject.properties.myKey
If I have a schema but want to generate a path expression for working with a document, how do we approach this problem?
At first glance, it might appear a simple string replacement would suffice. But once I have a property named "properties" or an array named "items", it starts to become interesting.

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.

Multiple schema JSON validation in Golang

I need to validate several JSON files against a schema in Golang.
I have been able to achieve it by using gojsonschema, that is really a straight forward library.
However, the problem I'm facing right now is that I have been given with schemas that have dependencies to another schemas and haven't found the way to load all the schemas that I need. Therefore, my validations always fail.
This is my main schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/List",
"definitions": {
"List": {
"type": "array",
"items": {
"$ref": "#/definitions/Item"
}
},
"Item": {
"description": "An item ....",
"type": "object",
"additionalProperties": false,
"properties": {
"property01": {
"description": "The property01 code.",
"$ref": "./CommonTypes.json#/definitions/Type01Definition"
}
},
"required": [
"property01"
]
}
}
}
And, I have another one with common types:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"Type01Definition": {
"description": "The definition for the type 01",
"type": "string",
"pattern": "^[A-Z0-9]{3}$"
}
}
}
Is there a way to load several schemas using that library? Or is there any other Golang library that allows to achieve that?
The way to refer to a file using $ref is to specify the absolute path of the file using a URL scheme. If you change the $ref to look like "$ref" : "file:///home/user/directory/CommonTypes.json#/definitions/Type01Definition, your example will work as expected.
If you need a bit more flexibility you can either try gojsonschema's NewReferenceLoaderFilesystem or switch to a different Golang library https://github.com/santhosh-tekuri/jsonschema. That library allows you to add custom resources so you can load several schemas at once.

Verifying JSON schema for uri and string together?

I am verifying below JSON schema for two separate JSON response which are same but only one difference is one returns 'system' as a string in format of uri and other returns plain string which cause failing of my second test case because for the second response it also look for a string in uri format.
How can i solve this issue in my schema?
{
"id": "/coding",
"type": "array",
"items": {
"type": "object",
"properties": {
"system": {
"type": "string"
"format": "uri"
}
}
}
}
You could just remove "format": "uri" from your schema.