data-to-jsonschema-to-ts
v2.0.0
Published
Convert plain javascript objects to JSON Schema and TypeScript typings
Maintainers
Readme
data-to-jsonschema-to-ts
Convert plain javascript objects to JSON Schema and TypeScript typings.
Requirements: Node.js 20 or newer. This package is ESM-only as of v2.0.0 —
importonly, norequire.
Installation
npm install data-to-jsonschema-to-tsExample
Convert data to JSON Schema
import {
convertJsonSchemaToTs,
createJsonSchemaValidator,
generateJsonSchemaFromData,
} from "data-to-jsonschema-to-ts";
const TEST_DATA = [
{
id: 1,
name: "John Doe",
age: 30,
email: "[email protected]",
},
{
id: 1,
name: "John Doe",
age: 30,
email: "[email protected]",
address: {
street: "123 Main St",
city: "Anytown",
state: "NY",
zip: "12345",
},
},
{
id: 1,
name: "John Doe",
age: 30,
email: null,
preferences: [{ name: "pref1", value: "value1" }],
},
];
const jsonSchema = generateJsonSchemaFromData(TEST_DATA, "TestData", {
additionalProperties: false,
});
console.log(JSON.stringify(jsonSchema, null, 2));Create a validator function from the generated schema
const validator = createJsonSchemaValidator(jsonSchema)
console.log(validator({id: 1, name: "John Doe", age: 30, email: "[email protected]"})); // valid
console.log(validator({id: 1, name: "John Doe", age: 30 })); // invalid, missing emailConvert the generated schema to TypeScript
const generatedTs = convertJsonSchemaToTs(jsonSchema);
console.log(generatedTs);
// export type TestData = { id: number; name: string; ... };convertJsonSchemaToTs is a thin wrapper over ajsc's TypescriptConverter. Pass the same options it accepts (inlineTypes, enumStyle, arrayItemNaming, depluralize, uncountableWords, jsdoc).
Subpath imports
You can import individual functions from per-feature subpaths if you only need one piece of the library:
import { generateJsonSchemaFromData } from "data-to-jsonschema-to-ts/generate-schema";
import { convertJsonSchemaToTs } from "data-to-jsonschema-to-ts/convert-to-ts";
import { createJsonSchemaValidator } from "data-to-jsonschema-to-ts/validator";
import { traverseObject } from "data-to-jsonschema-to-ts/traverse";
import type { JsonSchema, JsonType } from "data-to-jsonschema-to-ts/generate-schema";The bundled barrel (import { ... } from "data-to-jsonschema-to-ts") continues to work and re-exports everything.
Utilities
createJsonSchemaValidator(schema)
Generate a function that validates an object against a JSON Schema.
import { createJsonSchemaValidator } from "data-to-jsonschema-to-ts";
const validator = createJsonSchemaValidator({
type: "object",
properties: {
id: { type: "number" },
name: { type: "string" },
age: { type: "number" },
email: { type: "string" }
},
required: ["id", "name", "age", "email"]
});
validator({
id: 1,
name: "John Doe",
age: 30,
email: "myemail.com"
}); // { valid: true, errors: undefined }
validator({
id: 1,
age: null
}); // { valid: false, errors: [{...}] }traverseObject(obj, callback)
Walk a plain javascript object and call a callback for each primitive leaf value, with its JSON-dot/bracket path and parent metadata.
import { traverseObject } from "data-to-jsonschema-to-ts";
const obj = {
id: 1,
profile: {
name: "John Doe",
},
tags: ["admin", "owner"],
};
traverseObject(obj, (value, jsonPath, meta) => {
console.log(value, jsonPath, meta);
});
// console output (one line per primitive leaf):
// 1 'id' { key: 'id', parent: { path: '', type: 'object' } }
// 'John Doe' 'profile.name' { key: 'name', parent: { path: 'profile', type: 'object' } }
// 'admin' 'tags[0]' { key: 0, parent: { path: 'tags', type: 'array' } }
// 'owner' 'tags[1]' { key: 1, parent: { path: 'tags', type: 'array' } }The third callback argument meta is optional — drop it if you only need the value and path.
