Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danpacho/metal-type
Write typesafe schema with metal confidence 🦾
https://github.com/danpacho/metal-type
schema transformer typescript validation
Last synced: 2 days ago
JSON representation
Write typesafe schema with metal confidence 🦾
- Host: GitHub
- URL: https://github.com/danpacho/metal-type
- Owner: danpacho
- License: mit
- Created: 2023-11-22T15:45:52.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-02-25T13:37:34.000Z (9 months ago)
- Last Synced: 2024-02-26T13:26:07.604Z (9 months ago)
- Topics: schema, transformer, typescript, validation
- Language: TypeScript
- Homepage:
- Size: 799 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
## Write schema with confidence
1. **Declarative api** inspired by `zod`
2. First class **type-safety**
3. **Awesome error trackings and messages**
4. **Fast** - up to 5x faster than `zod`
5. **Small bundle size** - 3kb## In a nutshell
1. Define a DTO(schema)
```ts
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(),
})
```2. Parse unknown data
```ts
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)
```3. Check parsing errors
```
VALIDATION: [Err_1] object_value_error
› Expected:
{
name: string,
age: number,
isDeveloper: boolean,
friends: Array | 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?
4. Infer schema types
```ts
import { Infer } from 'metal-type'
type People = Infer
/**
* type People = {
* name: string;
* age: number;
* isDeveloper: boolean;
* friends?: string[];
* address?: {
* street: string;
* city: string;
* zip: number;
* } | undefined;
* }
*/
```## Installation
```bash
npm install @metal-box/type
```## Documentation
Will be available soon.
## Contributing
Check [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.
## License
MIT