Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/musicglue/joi-ts-generator
Generate Typescript Types from Joi Schemae.
https://github.com/musicglue/joi-ts-generator
joi nodejs typescript
Last synced: 3 months ago
JSON representation
Generate Typescript Types from Joi Schemae.
- Host: GitHub
- URL: https://github.com/musicglue/joi-ts-generator
- Owner: musicglue
- License: mit
- Archived: true
- Created: 2017-05-09T15:32:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-09T18:03:44.000Z (almost 5 years ago)
- Last Synced: 2024-06-18T09:35:27.298Z (5 months ago)
- Topics: joi, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 314 KB
- Stars: 12
- Watchers: 9
- Forks: 3
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deprecated
This project is no longer maintained. If you wish to adopt the project, open an issue.
# joi-ts-generator
Generate Typescript Types from Joi Schemae
### Installing
```
yarn add --dev @musicglue/joi-ts-generator
```Then you should add a script to your package json that you can use to regenerate your types,
such as the following:```json
{
"scripts": {
"regenerate-joi-types": "joi-ts-gen"
},
"joiTsGenerator": {
"input": "./src/schemas/index.ts",
"nullableMode": "option",
"outputs": {
"library": "./src/coercion.generated.ts",
"optics": "./src/optics",
"types": "./src/schemas/types.ts",
"utils": "./src/schemas/utils.ts"
}
}
}
```You can then rebuild your types by running `yarn regenerate-joi-types`.
### Defining
If you define Joi schemae that look like the following:
```ts
import joi = require("joi");export const Country = joi.string().notes("type:Country");
export const Currency = joi.string().notes("type:Currency");
export const Package = joi.string().valid(["gold", "silver", "bronze"]).notes("type:Package");export const Purchase = joi.object().keys({
id: joi.string().guid().required(),
country: Country,
currency: Currency.required(),
cents: joi.number().required(),
notes: joi.array().items(joi.string()),
package: Package.required(),
}).notes("type:Purchase");
```Then this project will generate you Typescript typings as follows:
```ts
export type Uuid = string;
export type Country = string;
export type Currency = string;
export type Package = "gold" | "silver" | "bronze";export interface Purchase {
id: Uuid;
country?: Country;
currency: Currency;
cents: number;
notes?: string[];
package: Package;
}
```### Troubleshooting
If you are defining string types and find that you get unexpected results, please follow the following:
```ts
import * as joi from "joi";
import cloneSchema from "@musicglue/joi-ts-generator"export const Country = cloneSchema(joi.string());
export const Currency = cloneSchema(joi.string());
```This prevents our internals from overwriting themselves as we parse the schemae. Joi objects are immutable,
other than the entrypoint...