- Declarative api inspired by
zod
- First class type-safety
- Awesome error trackings and messages
- Fast - up to 5x faster than
zod
- Small bundle size - 3kb
- Define a DTO(schema)
import { t } from 'metal-type'
const People = t.object({
name: t.string,
age: t.number,
isDeveloper: t.boolean,
'friends?': t.array(t.string),
'address?': t
.object({
street: t.string,
city: t.string,
zip: t.number,
})
.optional(),
})
- Parse unknown data
const data = {
name: 'John',
age: 42,
isDeveloper: true,
friends: ['Jane', 'Jack'],
address: {
street: '123 Main St',
city: 'New York',
zip: '12345', // <- Error: Expected number, got string
},
}
const parsed = People.parse(data)
- Check parsing errors
VALIDATION: [Err_1] object_value_error
› Expected:
{
name: string,
age: number,
isDeveloper: boolean,
friends: Array<string> | undefined,
address: {
street: string,
city: string,
zip: number
} | undefined
}
› Received:
{
"name": "John",
"age": 42,
"isDeveloper": true,
"friends": [
"Jane",
"Jack"
],
"address": {
"street": "123 Main St",
"city": "New York",
"zip": "12345"
}
}
› Check: [field "address"]
[Err_2] object_value_error
› Expected:
{
street: string,
city: string,
zip: number
} | undefined
› Received:
{
"street": "123 Main St",
"city": "New York",
"zip": "12345"
}
› Check: [field "zip"]
[Err_3] number_error
› Expected: number
› Received: 12345
› Check: 12345 is string
How easy is that?
- Infer schema types
import { Infer } from 'metal-type'
type People = Infer<typeof People>
/**
* type People = {
* name: string;
* age: number;
* isDeveloper: boolean;
* friends?: string[];
* address?: {
* street: string;
* city: string;
* zip: number;
* } | undefined;
* }
*/
npm install @metal-box/type
Will be available soon.
Check CONTRIBUTING.md for more information.
MIT