Generate JSON payload based on JSON schema for API payload validation - json

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.

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?

JSON Schema Validation Error in MarkLogic - XDMP-VALIDATEERRORS

Using MarkLogic version 10.0-4.2, I am trying to validate a simple JSON record against a simple JSON schema.
JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"SourceSystemName": {
"type": "string"
},
"BatchDtTm": {
"type": "string"
},
"SubjectArea": {
"type": "string"
},
"DocumentType": {
"type": "string"
},
"LastUpdatedDt": {
"type": "string"
},
"required": [
"SourceSystemName",
"BatchDtTm",
"SubjectArea",
"DocumentType",
"LastUpdatedDt",
]
}
}
Code being run in Query Console:
let jsonRecord = {"SourceSystemName":"ODH","BatchDtTm":"09/17/21 08:51:48:472723","SubjectArea":"Customer","DocumentType":"Preference","LastUpdatedDt":"09/17/21 03:59:53:629707"};
xdmp.jsonValidate(jsonRecord, cts.doc('/schemas/NewSchema.json').toString());
When I run the above code, I get error
XDMP-JSVALIDATEBADSCHEMA: Invalid schema "": ""
I'm not really sure what is 'invalid' about my schema. Can someone offer some insight into what MarkLogic is viewing as 'invalid'?
The second parameter for $schema is supposed to be the URI of the schema document.
$schema URI of the JSON schema to use for validation.
You are attempting to pass in the stringified content.
Try:
xdmp.jsonValidate(jsonRecord, '/schemas/NewSchema.json');
And ensure that the schema document is inserted into the Schemas database, not the content database.

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.

how to extract the JSON schema (sub schema) of a property from complete object json schema

I need a help regarding schema extraction by property.
For example i have a JSON schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "A simple address format",
"type": "object",
"properties": {
"street-name": { "type": "string" },
"locality":{ "type": "string" },
"region": { "type": "string" },
"postal-code": { "type": "int" },
"country-name": { "type": "string"}
},
"required": ["locality", "region", "country-name"]
}
I have an use case, where i need to extract the schema corresponding to each property and send to another service, where it will do validation against the value and save in database. Here is the sample object i need to send to another service.
{
"propertyName": "street-name",
"value": "19, Canton street",
**"schema": { "type": "string" }**
}
The questions is,
how we extract the schema for a particular property from a give JSON schema??
Given the property path, Is there any nodejs module exists to do this schema extraction? or if there is any other solutions exists ?
Because this is very simple scenario, but if we have array, anyOf, OneOf type its getting complicated;
Thanks in advance ! Please let me know if the question is not clear !
sadish

Json Schema, strictProperties for date-time formatMinimum and formatMaximum

I'm having problems with Json Schema when I'm trying to validate date-time value between interval of time [2013-11-17T00:00Z, 2015-11-17T00:00Z] I have this Json Schema:
{
"strictProperties": true ,
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"exampleDate": {
"format": "date-time",
"formatMinimum": "2013-11-17T00:00Z",
"formatMaximum": "2015-11-17T00:00Z"
}
}
}
},
"required": [
"data"
]
}
And with this json always out is as valid json:
{
"data": {
"exampleDate": "2010-11-17T00:00:00Z"
}
}
But as you can see, this json is not valid because of the date, it is out of valid range.
I've tested in online tools http://www.jsonschemavalidator.net/, http://jsonschemalint.com/draft4/# and with com.github.fge:json-schema-validator:2.2.6. Could someone help me please? Maybe I missing some configuration besides strictProperties? some step?. Thanks in advance =)
formatMinimum and formatMaximum are not in JSON Schema v4 draft. They are proposed features for v5:
https://github.com/json-schema/json-schema/wiki/formatMinimum-(v5-proposal)
However, this library can validate formatMinimum and formatMaximum using the v5 option:
https://github.com/epoberezkin/ajv#features