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-typescriptVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining Joi schemas, including nested objects and unknown properties, and then using `joi-to-typescript`'s `convert` function to generate corresponding TypeScript interfaces.
Ensure `joi@17.x` is installed in your project's dependencies: `npm install joi@^17` or `yarn add joi@^17`.
Install with `npm install --save-dev joi-to-typescript` and `npm install joi` (or `yarn add --dev joi-to-typescript` and `yarn add joi`).
Replace `.label('...')` with `.meta({ className: '...' })` for naming interfaces.Upgrade your Node.js environment to version 18 or 20 for optimal compatibility and support.
Add `.meta({ unknownType: 'string' })` (or appropriate type) to Joi schemas using `.unknown()`.Ensure you are using `import Joi from 'joi';` for ESM projects and that `joi@17.x` is correctly installed.
Install Joi: `npm install joi` or `yarn add joi`.
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.