Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Zod | 20,459 | 855 | an hour ago | 244 | September 11, 2022 | 175 | mit | TypeScript | ||
TypeScript-first schema validation with static type inference | ||||||||||
Yup | 20,016 | 8,912 | 2,408 | a day ago | 115 | August 20, 2022 | 168 | mit | TypeScript | |
Dead simple Object schema validation | ||||||||||
Ajv | 12,308 | 801,606 | 7,985 | 5 days ago | 349 | March 22, 2022 | 252 | mit | TypeScript | |
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927) | ||||||||||
Formily | 9,334 | 119 | 6 days ago | 209 | September 20, 2022 | 11 | mit | TypeScript | ||
📱🚀 🧩 Cross Device & High Performance Normal Form/Dynamic(JSON Schema) Form/Form Builder -- Support React/React Native/Vue 2/Vue 3 | ||||||||||
Type Graphql | 7,694 | 277 | 286 | 3 days ago | 67 | October 06, 2021 | 107 | mit | TypeScript | |
Create GraphQL schema and resolvers with TypeScript, using classes and decorators! | ||||||||||
Marshmallow | 6,480 | 9,091 | 969 | a day ago | 168 | June 26, 2022 | 164 | mit | Python | |
A lightweight library for converting complex objects to and from simple Python datatypes. | ||||||||||
Superstruct | 6,417 | 348 | 296 | a month ago | 75 | September 22, 2022 | 75 | mit | TypeScript | |
A simple and composable way to validate data in JavaScript (and TypeScript). | ||||||||||
Jsonschema | 4,075 | 15,530 | 2,424 | 2 days ago | 74 | July 07, 2022 | 32 | mit | Python | |
An implementation of the JSON Schema specification for Python | ||||||||||
Json Schema | 3,344 | 8,501 | 489 | 3 months ago | 46 | April 13, 2022 | 96 | mit | PHP | |
PHP implementation of JSON schema. Fork of the http://jsonschemaphpv.sourceforge.net/ project | ||||||||||
Kubeval | 3,043 | 8 | 20 | 21 days ago | 13 | April 26, 2021 | 104 | other | Go | |
Validate your Kubernetes configuration files, supports multiple Kubernetes versions |
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.
Feel free to submit an issue or PR if you have any ideas, or found any bugs.
MIT