Registry / type-stubs / joi-to-typescript

joi-to-typescript

JSON →
library4.15.0jsnpmunverified

joi-to-typescript is a utility library designed to automatically generate TypeScript interfaces from Joi validation schemas. Its primary purpose is to eliminate the redundancy of manually defining both Joi schemas for runtime validation and TypeScript interfaces for compile-time type checking, adhering to the DRY (Don't Repeat Yourself) principle. The current stable version is 4.15.0, with frequent minor and patch releases addressing dependency updates and feature enhancements. It is built to work seamlessly with Joi v17.x and is particularly useful in ecosystems like Hapi.js, offering integrations with tools like `joi-to-swagger` and `hapi-swagger` by leveraging Joi's `.meta()` functionality for interface naming and structure.

npm install joi-to-typescript
INSTALL
IMPORT
SIG · JOI-TO-TYPESCRIPT
J
joi-to-typescript
type-stubsjavascriptv4.15.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

convert
import { convert } from 'joi-to-typescript';
const { convert } = require('joi-to-typescript');
The primary function to initiate schema conversion. Supports ESM imports.
Joi
import Joi from 'joi';
const Joi = require('joi');
Used for defining the Joi schemas themselves. Ensure 'joi' is installed as a direct dependency.
ITypeInfo
import { ITypeInfo } from 'joi-to-typescript';
Interface describing the type information returned by the conversion process.

Demonstrates defining Joi schemas, including nested objects and unknown properties, and then using `joi-to-typescript`'s `convert` function to generate corresponding TypeScript interfaces.

import Joi from 'joi'; import { convert } from 'joi-to-typescript'; // Define your Joi schemas with .meta({ className: '...' }) for interface names export const JobSchema = Joi.object({ businessName: Joi.string().required(), jobTitle: Joi.string().required() }).meta({ className: 'Job' }); export const WalletSchema = Joi.object({ usd: Joi.number().required(), eur: Joi.number().required() }) .unknown() // Example with unknown keys .meta({ className: 'Wallet', unknownType: 'number' }); // Specify type for unknown keys export const PersonSchema = Joi.object({ firstName: Joi.string().required(), lastName: Joi.string().required().description('Last Name'), job: JobSchema, // Nested schema wallet: WalletSchema }).meta({ className: 'Person' }); // Use the convert function to generate TypeScript interfaces const { typescript: jobInterface } = convert(JobSchema); const { typescript: walletInterface } = convert(WalletSchema); const { typescript: personInterface } = convert(PersonSchema); console.log('// Job Interface'); console.log(jobInterface); console.log('\n// Wallet Interface'); console.log(walletInterface); console.log('\n// Person Interface'); console.log(personInterface); /* Expected output will resemble: // Job Interface export interface Job { businessName: string; jobTitle: string; } // Wallet Interface export interface Wallet { usd: number; eur: number; [x: string]: number; } // Person Interface export interface Person { firstName: string; lastName: string; job?: Job; wallet?: Wallet; } */
Debug
Known issues
gotchaThis package requires Joi version 17.x as a peer dependency. Using older or incompatible versions of Joi will likely result in conversion errors or incorrect type generation.
fix
Ensure `joi@17.x` is installed in your project's dependencies: `npm install joi@^17` or `yarn add joi@^17`.
affects: >=4.0.0
gotchajoi-to-typescript is intended as a development-time tool. It should be installed as a `devDependency`, while `joi` itself (the peer dependency) should be installed as a regular `dependency`.
fix
Install with `npm install --save-dev joi-to-typescript` and `npm install joi` (or `yarn add --dev joi-to-typescript` and `yarn add joi`).
affects: >=4.0.0
gotchaFor defining interface names, always use `.meta({ className: 'YourInterfaceName' })` on your Joi schemas. Using `.label()` for this purpose is discouraged as it interferes with Joi's intended use for error messages and might lead to unexpected behavior or conflicts with other tools like `hapi-swagger`.
fix
Replace `.label('...')` with `.meta({ className: '...' })` for naming interfaces.
affects: >=4.0.0
breakingThe library explicitly supports Node.js versions 18 and 20. While the `engines` field in `package.json` might indicate `>=14.0.0`, official support focuses on these newer LTS versions. Using older Node.js environments may lead to unexpected issues.
fix
Upgrade your Node.js environment to version 18 or 20 for optimal compatibility and support.
affects: >=4.0.0
gotchaWhen dealing with Joi objects that allow unknown keys via `.unknown()`, you can specify the type for these unknown keys using `.meta({ unknownType: 'your_type' })` to ensure the generated TypeScript interface accurately reflects this. Without it, the generated type for unknown keys might default to `any` or be less specific.
fix
Add `.meta({ unknownType: 'string' })` (or appropriate type) to Joi schemas using `.unknown()`.
affects: >=4.12.0
Errors
Common errors & fixes
TypeError: joi_1.default.validate is not a function
Incorrect Joi import statement (e.g., using `require` for a module that only exports ES modules) or a mismatch in Joi versions between runtime and type definitions.
fix
Ensure you are using `import Joi from 'joi';` for ESM projects and that `joi@17.x` is correctly installed.
Error: Cannot find module 'joi'
The `joi` package, a required peer dependency, is not installed in the project's dependencies.
fix
Install Joi: `npm install joi` or `yarn add joi`.
Generated TypeScript interface is empty or 'any'
The Joi schema might be missing `.meta({ className: 'InterfaceName' })`, or it's a complex schema feature not fully supported by `joi-to-typescript` yet, or Joi's `.label()` was used instead of `.meta()` for naming.
fix
Ensure all root-level schemas intended to become interfaces have `.meta({ className: 'YourInterfaceName' })`. Check the documentation for supported Joi features and consider breaking down very complex schemas if issues persist.
Upgrade
Version history
4.15.0latest on npm
Audit
Dependencies
joirequiredRequired peer dependency for defining Joi schemas which this library converts.
Agent activity
25 hits · last 30 days
node
20
OpenAI (training)
1
Resources
joi-to-typescript — npm install joi-to-typescript · libregistry