Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/typeofweb-org/schema

@typeofweb/schema: Lightweight validator with 100% TypeScript support and sane coercion rules.
https://github.com/typeofweb-org/schema

functional-programming hacktoberfest jsonschema schema ts typescript validation validation-library validations

Last synced: about 1 month ago
JSON representation

@typeofweb/schema: Lightweight validator with 100% TypeScript support and sane coercion rules.

Awesome Lists containing this project

README

        

# @typeofweb/schema

`@typeofweb/schema` is a lightweight and extensible library for data validation with full TypeScript support!

[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors-)

[![codecov](https://codecov.io/gh/typeofweb/schema/branch/main/graph/badge.svg?token=6DNCIHEEUO)](https://codecov.io/gh/typeofweb/schema)
[![npm](https://img.shields.io/npm/v/@typeofweb/schema.svg)](https://www.npmjs.com/package/@typeofweb/schema)
[![npm bundle size (minified + gzip)](https://badgen.net/bundlephobia/minzip/@typeofweb/schema)](https://bundlephobia.com/result?p=@typeofweb/schema)
[![no external dependencies](https://badgen.net/bundlephobia/dependency-count/@typeofweb/schema)](https://bundlephobia.com/result?p=@typeofweb/schema)
[![tree-shakeable](https://badgen.net/bundlephobia/tree-shaking/@typeofweb/schema)](https://bundlephobia.com/result?p=@typeofweb/schema)

## Docs

### 👉 [schema.typeofweb.com](https://schema.typeofweb.com/) 👈

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Michał Miszczyszyn

💻 🚧 📆 👀

Bartłomiej Wiśniewski

💻 👀 🐛 📖

Adam Siekierski

👀



Anthony Sottile

🛡️

Marek

📖

Olzhas Alexandrov

🐛



Bartek Słysz

🐛

Mateusz Stepaniuk

🤔

Dariusz Rzepka

🤔

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## Example

```ts
import { number, object, optional, string, validate } from '@typeofweb/schema';

const personSchema = object({
name: string(),
age: number(),
email: optional(string()),
})();

const mark = {
name: 'Mark',
age: 29,
};

const personValidator = validate(personSchema);

// If validation is successful returns data with correct type, throws ValidationError otherwise
const validatedPerson = personValidator(mark);
```

## Early benchmarks

Early benchmarks show some really promising performance of `@typeofweb/schema` when compared to other industry-leading solutions:

```
Platform info:
==============
Darwin 20.3.0 x64
Node.JS: 14.16.0
V8: 8.4.371.19-node.18
Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz × 8
```

| library | relative speed | operations per second | avg. operation time |
| --------------------- | -------------: | --------------------: | ------------------: |
| **@typeofweb/schema** | **ref** | **(1,934,098 rps)** | **(avg: 0.517μs)** |
| [email protected] | -7.21% | (1,794,594 rps) | (avg: 0.557μs) |
| [email protected] | -69.8% | (584,151 rps) | (avg: 1μs) |
| [email protected] | -76.16% | (461,088 rps) | (avg: 2μs) |
| [email protected] | -89.02% | (212,408 rps) | (avg: 4μs) |
| [email protected] | -92.2% | (150,791 rps) | (avg: 6μs) |
| [email protected] | -93.25% | (130,541 rps) | (avg: 7μs) |
| [email protected] | -96.99% | (58,197 rps) | (avg: 17μs) |
| [email protected] | -97.09% | (56,243 rps) | (avg: 17μs) |
| [email protected] | -98.99% | (19,492 rps) | (avg: 51μs) |
| [email protected] | -99.03% | (18,827 rps) | (avg: 53μs) |

### 👉 [schema.typeofweb.com](https://schema.typeofweb.com/) 👈