json-schemify
Converts any JSON structure to a valid JSON Schema object.
Getting started
Installation
npm install json-schemify --save-dev
Usage
const { schemify } = require('json-schemify');
or
import { schemify } from 'json-schemify';
API
The package exposes a single method:
schemify(json, options)
const schema = schemify(json, options);
Params
json
Any valid JSON.
options
Options object (all options are optional).
Option | Description |
---|
id | The $id property of the schema |
title | The title property of the schema |
Returns
A valid JSON Schema Object (draft-07)
Basic Example
This example returns a schema at the most basic level:
JSON
const json = {
firstName: 'John',
lastName: 'Doe',
age: 21,
};
API
const schema = generate(json, {
id: 'https://example.com/person.schema.json',
title: 'Person',
});
console.log(schema);
Result
{
$id: "https://example.com/person.schema.json",
$schema: "http://json-schema.org/draft-07/schema#",
title: "Person",
type: "object",
properties: {
firstName: { "type": "string" },
lastName: { "type": "string" },
age: { "type": "integer" }
}
}
Writing to file?
If the JSON schema is required to be written to file jsonfile does the job very well.