Tool to convert TypeScript interfaces to Joi schemas
Given the following TypeScript file:
// foo.ts
interface User {
name: string
age?: number
address: string | null
status: "opened" | "closed"
}
One can generate Joi schemas by running tsjoi foo.ts
to obtain
// Automatically generated by tsjoi
import Joi from 'joi'
import * as T from './foo'
export const User = Joi.object({
name: Joi.string().required(),
age: Joi.number(),
address: Joi.string().allow(null).required(),
status: Joi.string().valid(["opened", "closed"]).required()
})
export function isUser(obj: any): obj is T.User {
return User.validate(obj).error === null
}