Skip to content
/ tsjoi Public

Tool to convert TypeScript interfaces to Joi schemas

License

Notifications You must be signed in to change notification settings

ianliu/tsjoi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
}

About

Tool to convert TypeScript interfaces to Joi schemas

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published