typesafe-joi is a fork of @hapi/joi. More precisely, this is a fork of @types/hapi__joi because it has just redefined the essential APIs of joi. Almost all the APIs are the same as the original APIs, but limitations exists. That is why I create a new package.
typesafe-joi currently matches the API of @hapi/joi 15.x. And it requires TypeScript >=3.0.0 to work.
NOTE: typesafe-joi is still WIP. Sorry but I do not have enough time to write a unit test for it. Please feel free to open an issue when you find bugs or suggestions. I am glad to help!
The JavaScript APIs of typesafe-joi and joi are identical. However, its type definition lacks of the type information of the “value” behind the schema. That means, JavaScript knows what the validated object look like at runtime, but TypeScript does not know it at all at compile time. typesafe-joi records more information into schemas so that TypeScript is able to know what object you are validating at compile time.
Unfortunately not all joi APIs are able to be properly described in TypeScript. See Reference for more information.
Import and use joi from typesafe-joi
:
import * as Joi from 'typesafe-joi'
The TypeScript magic is shown when you call .validate
or .attempt
:
const data: any = dataFromAnywhere
// { foo?: string } | undefined
Joi.object({ foo: Joi.string() }).validate(data).value
// { id: number, email?: string }[]
Joi.attempt(data, Joi.array()
.items({
id: Joi.number().integer().required(),
email: Joi.string().email()
})
.required()
)
You can also use Literal
to pull the data type out of the schema:
const schema = Joi.array()
.items({
id: Joi.number().integer().required(),
email: Joi.string().email()
})
.required()
type T = Joi.Literal<typeof schema>
NOTE: I suggest to turn on
strict
option in the compiler options of TypeScript.
Not every API of typesafe-joi matches the original joi in types. typesafe-joi provides typecast helpers in case you have to define the resulting type manually.
// 'foo'
Joi.string().required() as Joi.Cast.String<'foo'>
If the typecast includes undefined
type, the key will be optional.
// { foo?: Foo }
Joi.object({ foo: number().required() as Joi.Cast.Object<Foo | undefined> })
Typecasting means you have to define everything by yourself. Schema attributes like allow
is discarded.
// number | 'foo' | true
Joi.number().allow('foo').default(true)
// 123
Joi.number().allow('foo').default(true) as Joi.Cast.Number<123>
TypeScript may complain about type mismatch. In this case assert the expression to any
firstly.
// Error
Joi.object({
foo: Joi.object().pattern(/\d+/, 1).allow(1) as Joi.Cast.Object<Foo>
})
// { map: Foo | 1 }
Joi.object({
foo: Joi.object().pattern(/\d+/, 1).allow(1) as any as Joi.Cast.Object<Foo | 1>
})
I recommend not to use the schema anymore after typecast. The behavior will be undefined.
Supported schema types to cast:
Cast.Alternatives
Cast.Any
Cast.Array
Cast.Binary
Cast.Boolean
Cast.Date
Cast.Function
Cast.Lazy
Cast.Number
Cast.Object
Cast.String
Here is a list of APIs of joi.
- ✅ - Fully supported
- ✔️ - Supported but might be incorrect (not well tested)
⚠️ - Partially supported (with limitations)- ❌ - Not supported (however you can use it without auto type generation)
- 🔘 - Supported but no type generation needed
Feel free to submit an issue or PR if you have any ideas, or found any bugs.
MIT