How to create a json-schema out of a json object - javascript

Given a browser debugger output of root.value with two properties in javascript
root.value
__proto__: {...}
firstname: "my name"
age: 25
I want to parse it to a JSON string including the type like below. Or literally convert the above json object to the format below.
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "Basic Info",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"age": {
"type": "number"
}
}
}
Does any one know how to do that in javascript or any framework I can use to achieve such?
Note: I did not created the JSON myself it's an output of another framework. So the types of the fields are unknown until runtime.
My main concern is to embed the json object values for
{ "properties": {
"firstName": {
"type": "string"
},
"age": {
"type": "number"
}
}
JSON.stringify(root.value);
will only return
{
{
"firstname":" my name"
},
{
"age": 25
}
}

Related

Can we refer to the field of the object validating in JSON schema to minLength property of the declared JSON schema?

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Text",
"description": "Form Text ",
"type": "object",
"properties": {
"value": {
"type": "string",
"default": "",
"minLength":"this needs to point to fieldSpecific.minLength of the validating object"
}
}
can we do something like this the object we receive to validate will be as below
{
"fieldSpecific": {
"minLength": 4
},
"value": "asfasfasfafasfasF"
}
in the above-received object we receive minLength under fieldSpecific can we write a JSON schema to refer minLength in field specific to validate the minLength of the value?
JSON Schema doesn't support this out of the box, but you can do it with JsonSchema.Net and the extension Data Vocabulary package.
The docs can be found on json-everything.net.
Here's what your schema would look like with this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Text",
"description": "Form Text ",
"type": "object",
"properties": {
"value": {
"type": "string",
"default": "",
"data": {
"minLength": "/fieldSpecific/minLength"
}
}
}
Disclaimer: these libraries are mine.

ChaiJS jsonSchema check for multiple types

In my Chai-Test (using it for PostMan) I want to validate my API-response-design. For that I have written a Chai-Test:
pm.test("Check response schema", () => {
const schema = {
"type": "object",
"properties": {
"success": { "type": "boolean" },
"data": { "type": "object" },
"err": { "type": "object" },
"info": { "type": "string" }
},
"required": ["success", "data", "err", "info"]
}
pm.response.to.have.jsonSchema(schema)
})
My problem now is, that either the data or the err object is defined based on whether the request to the API was successful or not. I wanted to accomplish that by using two types for the data and the err: object AND null. So, how can I check for two types with the jsonSchema? Or is there an other and better way to do this?
You can do that:
"data": { "type": ["object", "null"] },
"err": { "type": ["object", "null"] }

How to verify if a value is of format and/or type double using ajv schema validation?

Im using Ajv version 07.
I am trying to validate that the value of a property, returned by the JSON response body is of type and format double in postman, using ajv validation, however, I'm not being able to do it. I've tried searching it online, but still did not find anything about it.
I've tried typing the following:
"type" : "double",
"format": "double"
"type": "Double",
"format": "Double"
"type":"number"
"format":"double"
All the above attempts were unsuccessful as they all come with an error message saying either:
Error: unknown type "double" is used in schema
or
Error: unknown format "double" is used in schema
Would anyone be able to help me with this please?
schema
var Ajv = require ('ajv'),
ajv = new Ajv ({logger:console}),
expectedResponseSchema =
{
"items": {
"required": [
"payments"
],
"properties": {
"payments": {
"items": {
"required": [
"amount"
]
"properties": {
"amount": {
"$id": "#/items/properties/payments/items/properties/amount",
"type": "number",
"format": "double"
}
}
}
}
}
}
Postman test
var currentSchPmExpTest;
try{
currentSchPmExpTest = ' expectedResponseSchema variable';
pm.expect(ajv.validate(expectedResponseSchema, jsonData)).to.be.true;
pm.test('Test 1 - PASSED - expectedResponseSchema variable data matches schema returned by body response!', () => true);
} catch(e){
pm.test('Test 1 - FAILED - Expected data does not match response body data!', () => {throw new Error(e.message + " in " + currentSchPmExpTest)});
}
body response
[
{
"payments": [
{
"amount": 2.200000045367898,
}
]
}
]
I'm not sure where you're getting the type and format from but as per the AJV docs (could be out of date) that isn't a valid type.
EDIT:
From your update, I would recommend changing the test script to something like this so that you correct part of the schema is getting checked:
let schema = {
"type": "array",
"items": {
"type": "object",
"required": [
"payments"
],
"properties": {
"payments": {
"type": "array",
"items": {
"type": "object",
"required": [
"amount"
],
"properties": {
"amount": {
"type": "number",
}
}
}
}
}
}
}
pm.test("Check Schema", () => {
pm.response.to.have.jsonSchema(schema)
})
The try/catch block can be added around this if you need too.

react-jsonschema-form giving error when using anyOf/oneOf property

The following JSON schema is giving the error "Invalid root object field configuration:Cannot convert undefined or null to object."
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {},
"id": "http://example.com/example.json",
"type": "object",
"oneOf": [
{
"properties": {
"foo": {
"id": "/properties/boo",
"type": "string"
}
}
}
]
}
Can someone resolve the error?

Automatically generate JavaScript from JSON Schema

After hourlong searching I have found no solution about my problem and now I hope someome could be able to help me here.
So basically what I am looking for is a way to generate a JavaScript file (*.js) from a JSON (Schema) file automatically using NodeJS. I know that there are things like fs.write, but this is surely no fitting way for my problem, I think. And so far I have found no other way to create my JavaScript file other than that.
Basically I want to translate:
{
"type":"object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"default":12
},
"favorite_color": {
"type": "string"
},
"gender": {
"type": "string",
"enum": [
"male",
"female"
]
}
}
}
Into JavaScript code like:
var data = function() {
data.baseConstructor.call(this);
this.name = ko.observable("");
this.age = ko.obseravble(12);
this.favorite_color = ko.observable();
this.gender = ko.observable(data.genderModes.male);
}
data.genderModes = {
male: "male",
female: "female"
}
Would someone be able to give me a hint to my problem?
I don't know how to convert your json to js function but if you want to create object from json schema, you could use json-schema-defaults.
After creating one object with json-schema-defaults you can create anothers with Object.create and you can add first level properties to new objects which is created by Object.create function.
var a = require('json-schema-defaults')({
"type":"object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"default":12
},
}
});
var b = Object.create(a,{
id:{ value:1 },
f:{ value:function() {
console.log('run lola run');
}
}
}
);

Categories