
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
@jsonhero/schema-generator
Advanced tools
Generate a draft-07 JSON schema definitions from an example JSON document
Generate a draft-07 JSON schema definitions from an example JSON document
Install Schema Generator
$ npm install --save @jsonhero/schema-generator
generateSchema takes either a JSON object/array or JSON stringified and returns a valid draft-07 JSON Schema
const { generateSchema } = require("@jsonhero/schema-generator");
const exampleJSON = {
id: 1234,
name: "Eric",
image: {
url: "https://foo.com/image.png",
},
email: "eallam@icloud.com",
homepage: "http://github.com/ericallam",
createdAt: "2021-12-17T12:55:58.141Z",
tags: ["foo", "bar"],
isAdmin: true,
};
const schema = generateSchema(exampleJSON);
// Now validate the provided JSON using the generated schema using ajv
const Ajv = require("ajv");
const ajv = new Ajv();
const addFormats = require("ajv-formats");
addFormats(ajv);
const validate = ajv.compile(schema);
const valid = validate(exampleJSON); // valid == true
The Schema generated in the above example
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://jsonhero.io/schemas/root.json",
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"image": {
"type": "object",
"properties": {
"url": {
"type": "string",
"format": "uri"
}
},
"required": [],
"additionalProperties": true
},
"email": {
"type": "string",
"format": "email"
},
"homepage": {
"type": "string",
"format": "uri"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"friends": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
}
},
"required": [],
"additionalProperties": true
}
},
"isAdmin": {
"type": "boolean"
}
},
"required": [],
"additionalProperties": true
}
By default all properties are optional and additionalProperties are allowed. You can set the strict option to true if you'd like a more restrictive schema
const { generateSchema } = require("@jsonhero/schema-generator");
const exampleJSON = {
id: 1234,
name: "Eric",
image: {
url: "https://foo.com/image.png",
},
email: "eallam@icloud.com",
homepage: "http://github.com/ericallam",
createdAt: "2021-12-17T12:55:58.141Z",
tags: ["foo", "bar"],
isAdmin: true,
};
const schema = generateSchema(exampleJSON, { strict: true });
// Now validate the provided JSON using the generated schema using ajv
const Ajv = require("ajv");
const ajv = new Ajv();
const addFormats = require("ajv-formats");
addFormats(ajv);
const validate = ajv.compile(schema);
const valid = validate(exampleJSON); // valid == true
The Schema generated in the above example
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://jsonhero.io/schemas/root.json",
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"image": {
"type": "object",
"properties": {
"url": {
"type": "string",
"format": "image"
}
},
"required": ["url"],
"additionalProperties": false
},
"email": {
"type": "string",
"format": "email"
},
"homepage": {
"type": "string",
"format": "uri"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"friends": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"],
"additionalProperties": false
}
},
"isAdmin": {
"type": "boolean"
}
},
"required": [
"id",
"name",
"image",
"email",
"homepage",
"createdAt",
"tags",
"friends",
"isAdmin"
],
"additionalProperties": false
}
Generate Schema currently supports a few string formats
uri formatdate-time formatemail formatIf you are using ajv to use the generated schema, make sure to use the ajv-formats package
const addFormats = require("ajv-formats");
addFormats(ajv);
You can also set the baseURI of the schema (i.e. the top level $id property)
const { generateSchema } = require("@jsonhero/schema-generator");
const exampleJSON = {
foo: "bar",
};
const schema = generateSchema(exampleJSON, {
baseURI: "https://example.com/schemas/root.json",
});
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/schemas/root.json",
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"required": [],
"additionalProperties": true
}
FAQs
Generate a draft-07 JSON schema definitions from an example JSON document
We found that @jsonhero/schema-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.