Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 710 Bytes

README.md

File metadata and controls

34 lines (28 loc) · 710 Bytes

tsjoi

Tool to convert TypeScript interfaces to Joi schemas

Example

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
}