JSON Schema Transpiler (eth-json-schema-transpiler)
raw JSON → 0.0.0-development verified Fri May 01 auth: no javascript
A dev tool that converts JSON Schemas into type definitions for multiple languages including TypeScript, Go, Rust, and Python. Version 0.0.0-development is in early development with limited updates. It features automatic cycle detection, reference resolution, and deterministic title generation. Key differentiators: minimal dependencies, synchronous execution, and browser support via the web demo. The package generates one type alias per schema and includes documentation annotations.
Common errors
error TypeError: JsonSchemaTranspiler is not a constructor ↓
cause Importing as named export instead of default, or using CommonJS without .default
fix
Use import JsonSchemaTranspiler from 'eth-json-schema-transpiler' (ESM) or const JsonSchemaTranspiler = require('eth-json-schema-transpiler').default (CJS)
error Error: Invalid schema: schema must be an object ↓
cause Passing a JSON string instead of a parsed object to the constructor
fix
Parse the JSON string first: JSON.parse(schemaString)
error TypeError: transpiler.to is not a function ↓
cause Using an outdated version that does not have the .to() method, or importing incorrectly
fix
Update to latest version. If using CommonJS, ensure you used .default.
error Error: Unsupported language 'python3' ↓
cause Providing an unsupported language name to .to()
fix
Use one of: 'typescript', 'go', 'rust', 'python'
Warnings
breaking Upgrading from v0.0.1 to v0.0.2 changes default output format for Go and Rust outputs. TypeScript output now includes optional properties. ↓
fix Review generated types and adjust downstream code if needed.
deprecated The .to('language') shorthand is deprecated in favor of explicit language methods (toTypescript, toGo, toRust, toPython). ↓
fix Use toTypescript() instead of to('typescript').
gotcha JSON Schema $ref references may not be resolved in browser environments without bundler configuration for json-schema-ref-parser. ↓
fix Ensure your bundler (e.g., webpack) includes Node.js polyfills for path and url modules.
gotcha The package is case-sensitive about language names: 'typescript', 'go', 'rust', 'python' are supported. 'TypeScript' or 'py' will throw an error. ↓
fix Use exact lowercase names when calling .to(), or use the dedicated methods.
Install
npm install eth-json-schema-transpiler yarn add eth-json-schema-transpiler pnpm add eth-json-schema-transpiler Imports
- default wrong
import { JsonSchemaTranspiler } from 'eth-json-schema-transpiler'correctimport JsonSchemaTranspiler from 'eth-json-schema-transpiler' - JsonSchemaTranspiler wrong
const JsonSchemaTranspiler = require('eth-json-schema-transpiler')correctimport JsonSchemaTranspiler from 'eth-json-schema-transpiler' - toTypescript wrong
JsonSchemaTranspiler.toTypescript(schema)correctconst transpiler = new JsonSchemaTranspiler(schema); transpiler.toTypescript()
Quickstart
import JsonSchemaTranspiler from 'eth-json-schema-transpiler';
const mySchema = {
title: 'Person',
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' }
},
required: ['name']
};
const transpiler = new JsonSchemaTranspiler(mySchema);
console.log(transpiler.toTypescript());
// Output: type Person = { name: string; age?: number; };